Converted the jsonld context to be an empty interface

This allows us to support passing it just as a string or URI
This commit is contained in:
Marius Orcsik 2018-07-16 13:26:26 +02:00
parent 327128b67f
commit 8308c6fb22
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
4 changed files with 15 additions and 12 deletions

View file

@ -8,7 +8,7 @@ import (
ap "github.com/mariusor/activitypub.go/activitypub" ap "github.com/mariusor/activitypub.go/activitypub"
) )
var Ctx *Context var Ctx interface{}
// Ref basic type // Ref basic type
type Ref string type Ref string

View file

@ -35,8 +35,8 @@ const (
) )
type payloadWithContext struct { type payloadWithContext struct {
Context *Context `jsonld:"@context,collapsible"` Context interface{} `jsonld:"@context,collapsible"`
Obj *interface{} Obj interface{}
} }
type jsonCapableValue struct { type jsonCapableValue struct {
@ -131,7 +131,7 @@ func (p *payloadWithContext) MarshalJSON() ([]byte, error) {
} }
} }
if p.Obj != nil { if p.Obj != nil {
oMap := reflectToJSONValue(*p.Obj) oMap := reflectToJSONValue(p.Obj)
if oMap.isScalar && a.isScalar { if oMap.isScalar && a.isScalar {
a.isScalar = true a.isScalar = true
a.scalar = oMap.scalar a.scalar = oMap.scalar

View file

@ -127,7 +127,7 @@ func TestPayloadWithContext_MarshalJSON(t *testing.T) {
var a interface{} var a interface{}
a = 1 a = 1
p := payloadWithContext{Obj: &a} p := payloadWithContext{Obj: a}
pData, pErr := p.MarshalJSON() pData, pErr := p.MarshalJSON()
if pErr != nil { if pErr != nil {
@ -135,6 +135,6 @@ func TestPayloadWithContext_MarshalJSON(t *testing.T) {
} }
av, _ := Marshal(a) av, _ := Marshal(a)
if bytes.Compare(pData, av) != 0 { if bytes.Compare(pData, av) != 0 {
t.Errorf("Empty payload should resolve to value '%#v', received '%s'", av, pData) t.Errorf("Empty payload should resolve to value '%s', received '%s'", av, pData)
} }
} }

View file

@ -15,14 +15,16 @@ func TestAcceptSerialization(t *testing.T) {
obj.Name["en"] = "test" obj.Name["en"] = "test"
obj.Name["fr"] = "teste" obj.Name["fr"] = "teste"
jsonld.Ctx = &jsonld.Context{URL: "https://www.w3.org/ns/activitystreams"} jsonld.Ctx = jsonld.Context{URL: "https://www.w3.org/ns/activitystreams"}
data, err := jsonld.Marshal(obj) data, err := jsonld.Marshal(obj)
if err != nil { if err != nil {
t.Errorf("Error: %v", err) t.Errorf("Error: %v", err)
} }
if !strings.Contains(string(data), string(jsonld.Ctx.URL)) {
t.Errorf("Could not find context url %#v in output %s", jsonld.Ctx.URL, data) ctxt := jsonld.Ctx.(jsonld.Context)
if !strings.Contains(string(data), string(ctxt.URL)) {
t.Errorf("Could not find context url %#v in output %s", ctxt.URL, data)
} }
if !strings.Contains(string(data), string(obj.ID)) { if !strings.Contains(string(data), string(obj.ID)) {
t.Errorf("Could not find id %#v in output %s", string(obj.ID), data) t.Errorf("Could not find id %#v in output %s", string(obj.ID), data)
@ -44,15 +46,16 @@ func TestCreateActivityHTTPSerialization(t *testing.T) {
obj.Name["en"] = "Accept New" obj.Name["en"] = "Accept New"
baseURI := string(activitypub.ActivityBaseURI) baseURI := string(activitypub.ActivityBaseURI)
jsonld.Ctx = &jsonld.Context{ jsonld.Ctx = jsonld.Context{
URL: jsonld.Ref(baseURI + string(obj.Type)), URL: jsonld.Ref(baseURI + string(obj.Type)),
} }
data, err := jsonld.Marshal(obj) data, err := jsonld.Marshal(obj)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if !strings.Contains(string(data), string(jsonld.Ctx.URL)) { ctxt := jsonld.Ctx.(jsonld.Context)
t.Errorf("Could not find context url %#v in output %s", jsonld.Ctx.URL, data) if !strings.Contains(string(data), string(ctxt.URL)) {
t.Errorf("Could not find context url %#v in output %s", ctxt.URL, data)
} }
if !strings.Contains(string(data), string(obj.ID)) { if !strings.Contains(string(data), string(obj.ID)) {
t.Errorf("Could not find id %#v in output %s", string(obj.ID), data) t.Errorf("Could not find id %#v in output %s", string(obj.ID), data)