From 8308c6fb22916dd42978677c410535e42cbd3b73 Mon Sep 17 00:00:00 2001 From: Marius Orcsik Date: Mon, 16 Jul 2018 13:26:26 +0200 Subject: [PATCH] Converted the jsonld context to be an empty interface This allows us to support passing it just as a string or URI --- jsonld/context.go | 2 +- jsonld/encode.go | 6 +++--- jsonld/encode_test.go | 4 ++-- tests/integration_test.go | 15 +++++++++------ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/jsonld/context.go b/jsonld/context.go index 0930188..18c6a3f 100644 --- a/jsonld/context.go +++ b/jsonld/context.go @@ -8,7 +8,7 @@ import ( ap "github.com/mariusor/activitypub.go/activitypub" ) -var Ctx *Context +var Ctx interface{} // Ref basic type type Ref string diff --git a/jsonld/encode.go b/jsonld/encode.go index f4c8512..c84d807 100644 --- a/jsonld/encode.go +++ b/jsonld/encode.go @@ -35,8 +35,8 @@ const ( ) type payloadWithContext struct { - Context *Context `jsonld:"@context,collapsible"` - Obj *interface{} + Context interface{} `jsonld:"@context,collapsible"` + Obj interface{} } type jsonCapableValue struct { @@ -131,7 +131,7 @@ func (p *payloadWithContext) MarshalJSON() ([]byte, error) { } } if p.Obj != nil { - oMap := reflectToJSONValue(*p.Obj) + oMap := reflectToJSONValue(p.Obj) if oMap.isScalar && a.isScalar { a.isScalar = true a.scalar = oMap.scalar diff --git a/jsonld/encode_test.go b/jsonld/encode_test.go index d2af72b..2154128 100644 --- a/jsonld/encode_test.go +++ b/jsonld/encode_test.go @@ -127,7 +127,7 @@ func TestPayloadWithContext_MarshalJSON(t *testing.T) { var a interface{} a = 1 - p := payloadWithContext{Obj: &a} + p := payloadWithContext{Obj: a} pData, pErr := p.MarshalJSON() if pErr != nil { @@ -135,6 +135,6 @@ func TestPayloadWithContext_MarshalJSON(t *testing.T) { } av, _ := Marshal(a) 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) } } diff --git a/tests/integration_test.go b/tests/integration_test.go index c3e4e6d..90e9647 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -15,14 +15,16 @@ func TestAcceptSerialization(t *testing.T) { obj.Name["en"] = "test" 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) if err != nil { 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)) { 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" baseURI := string(activitypub.ActivityBaseURI) - jsonld.Ctx = &jsonld.Context{ + jsonld.Ctx = jsonld.Context{ URL: jsonld.Ref(baseURI + string(obj.Type)), } data, err := jsonld.Marshal(obj) if err != nil { t.Error(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)) { t.Errorf("Could not find id %#v in output %s", string(obj.ID), data)