This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/src/jsonld/context.go

46 lines
1 KiB
Go
Raw Normal View History

package jsonld
import (
"encoding/json"
"activitypub"
)
2018-03-25 18:54:51 +00:00
// Ref basic type
type Ref string
2018-03-25 18:54:51 +00:00
// Context is the basic JSON-LD element. It is used to map terms to IRIs.
// Terms are case sensitive and any valid string that is not a reserved JSON-LD
// keyword can be used as a term.
type Context struct {
URL Ref `jsonld:"@url"`
Language activitypub.NaturalLanguageValue `jsonld:"@language,omitempty,collapsible"`
}
2018-03-26 21:12:04 +00:00
type Collapsible interface {
Collapse() []byte
}
2018-03-25 18:54:51 +00:00
// Ref returns a new Ref object based on Context URL
func (c *Context) Ref() Ref {
return Ref(c.URL)
}
2018-03-26 21:12:04 +00:00
func (c *Context) Collapse() []byte {
return []byte(c.URL)
}
2018-03-25 18:54:51 +00:00
// MarshalText basic stringify function
func (r *Ref) MarshalText() ([]byte, error) {
return []byte(*r), nil
}
2018-03-25 18:54:51 +00:00
// MarshalJSON returns the JSON document represented by the current Context
func (c *Context) MarshalJSON() ([]byte, error) {
2018-03-25 17:35:10 +00:00
a := reflectToJSONValue(c)
if a.isScalar {
return json.Marshal(a.scalar)
}
2018-03-25 18:54:51 +00:00
return json.Marshal(a.object)
}