From 8bd5130d0f129ae8cde5b52693d4d7e863dcc640 Mon Sep 17 00:00:00 2001 From: Marius Orcsik Date: Fri, 6 Jul 2018 22:33:47 +0200 Subject: [PATCH] Move the context to a package variable --- jsonld/context.go | 2 ++ jsonld/encode.go | 17 ++++++----------- jsonld/encode_test.go | 13 +++++++------ tests/integration_test.go | 16 ++++++++-------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/jsonld/context.go b/jsonld/context.go index fa927ab..f1f28f6 100644 --- a/jsonld/context.go +++ b/jsonld/context.go @@ -8,6 +8,8 @@ import ( ap "github.com/mariusor/activitypub.go/activitypub" ) +var Ctx *Context + // Ref basic type type Ref string diff --git a/jsonld/encode.go b/jsonld/encode.go index cde495e..fb6e60c 100644 --- a/jsonld/encode.go +++ b/jsonld/encode.go @@ -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) } diff --git a/jsonld/encode_test.go b/jsonld/encode_test.go index 209599e..57a24b0 100644 --- a/jsonld/encode_test.go +++ b/jsonld/encode_test.go @@ -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) } diff --git a/tests/integration_test.go b/tests/integration_test.go index 3c7621f..c3e4e6d 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -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)