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"
)
var Ctx *Context
var Ctx interface{}
// Ref basic type
type Ref string

View file

@ -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

View file

@ -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)
}
}

View file

@ -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)