Move the context to a package variable

This commit is contained in:
Marius Orcsik 2018-07-06 22:33:47 +02:00
parent 978985644e
commit 8bd5130d0f
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
4 changed files with 23 additions and 25 deletions

View file

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

View file

@ -324,8 +324,9 @@ func JSONLdName(n string, tag jsonLdTag) string {
// handle them. Passing cyclic structures to Marshal will result in
// an infinite recursion.
//
func Marshal(v interface{}, ctx *Context) ([]byte, error) {
func Marshal(v interface{}) ([]byte, error) {
e := &encodeState{}
ctx := Ctx
err := e.marshal(v, encOpts{escapeHTML: true})
if err != nil {
return nil, err
@ -343,12 +344,6 @@ func Marshal(v interface{}, ctx *Context) ([]byte, error) {
}
// Marshaler is the interface implemented by types that
// can marshal themselves into valid JSON.
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
// An UnsupportedTypeError is returned by Marshal when attempting
// to encode an unsupported value type.
type UnsupportedTypeError struct {
@ -510,7 +505,7 @@ func typeEncoder(t reflect.Type) encoderFunc {
}
var (
marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
marshalerType = reflect.TypeOf(new(json.Marshaler)).Elem()
textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
)
@ -574,7 +569,7 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.WriteString("null")
return
}
m, ok := v.Interface().(Marshaler)
m, ok := v.Interface().(json.Marshaler)
if !ok {
e.WriteString("null")
return
@ -595,7 +590,7 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, _ encOpts) {
e.WriteString("null")
return
}
m := va.Interface().(Marshaler)
m := va.Interface().(json.Marshaler)
b, err := m.MarshalJSON()
if err == nil {
// copy JSON into buffer, checking validity.
@ -730,7 +725,7 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
return
}
if opts.quoted {
sb, err := Marshal(v.String(), nil)
sb, err := Marshal(v.String())
if err != nil {
e.error(err)
}

View file

@ -24,11 +24,11 @@ func TestMarshal(t *testing.T) {
b := mockTypeA{}
url := "http://www.habarnam.ro"
c := Context{URL: Ref(url)}
Ctx = &Context{URL: Ref(url)}
var err error
var out []byte
out, err = Marshal(a, &c)
out, err = Marshal(a)
if err != nil {
t.Errorf("%s", err)
}
@ -62,11 +62,11 @@ func TestMarshalNullContext(t *testing.T) {
PropB float64
}{"test", 0.0004}
outL, errL := Marshal(a, nil)
outL, errL := Marshal(a)
if errL != nil {
t.Errorf("%s", errL)
}
outJ, errJ := Marshal(a, nil)
outJ, errJ := Marshal(a)
if errJ != nil {
t.Errorf("%s", errJ)
}
@ -116,7 +116,8 @@ func TestPayloadWithContext_MarshalJSON(t *testing.T) {
if eErr != nil {
t.Errorf("Error: %s", eErr)
}
n, _ := Marshal(nil, nil)
Ctx = nil
n, _ := Marshal(nil)
if bytes.Compare(eData, n) != 0 {
t.Errorf("Empty payload should resolve to null json value '%s', received '%s'", n, eData)
}
@ -129,7 +130,7 @@ func TestPayloadWithContext_MarshalJSON(t *testing.T) {
if pErr != nil {
t.Errorf("Error: %s", pErr)
}
av, _ := Marshal(a, nil)
av, _ := Marshal(a)
if bytes.Compare(pData, av) != 0 {
t.Errorf("Empty payload should resolve to value '%#v', received '%s'", av, pData)
}

View file

@ -15,14 +15,14 @@ func TestAcceptSerialization(t *testing.T) {
obj.Name["en"] = "test"
obj.Name["fr"] = "teste"
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, &ctx)
data, err := jsonld.Marshal(obj)
if err != nil {
t.Errorf("Error: %v", err)
}
if !strings.Contains(string(data), string(ctx.URL)) {
t.Errorf("Could not find context url %#v in output %s", ctx.URL, data)
if !strings.Contains(string(data), string(jsonld.Ctx.URL)) {
t.Errorf("Could not find context url %#v in output %s", jsonld.Ctx.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 +44,15 @@ func TestCreateActivityHTTPSerialization(t *testing.T) {
obj.Name["en"] = "Accept New"
baseURI := string(activitypub.ActivityBaseURI)
ctx := jsonld.Context{
jsonld.Ctx = &jsonld.Context{
URL: jsonld.Ref(baseURI + string(obj.Type)),
}
data, err := jsonld.Marshal(obj, &ctx)
data, err := jsonld.Marshal(obj)
if err != nil {
t.Error(err)
}
if !strings.Contains(string(data), string(ctx.URL)) {
t.Errorf("Could not find context url %#v in output %s", ctx.URL, data)
if !strings.Contains(string(data), string(jsonld.Ctx.URL)) {
t.Errorf("Could not find context url %#v in output %s", jsonld.Ctx.URL, data)
}
if !strings.Contains(string(data), string(obj.ID)) {
t.Errorf("Could not find id %#v in output %s", string(obj.ID), data)