Changes
3 changed files (+85/-85)
-
-
@@ -58,14 +58,22 @@ const (GraphKw Term = "@graph" ) // Ref basic type type ( // Ref basic type LangRef string Term string IRI string Terms []Term // Term represents the JSON-LD term for @context maps Term string // IRI is a International Resource Identificator IRI string // Terms is an array of Term values Terms []Term ) // Nillable type Nillable interface { IsNil() bool } type IRILike interface { IsCompact() bool IsAbsolute() bool
-
@@ -98,14 +106,16 @@ var keywords = Terms{VocabKw, } const NullTerm Term = "-" const NilTerm Term = "-" const NilLangRef LangRef = "-" type ContextObject struct { ID interface{} `jsonld:"@id,omitempty,collapsible"` Type interface{} `jsonld:"@type,omitempty,collapsible"` } // Context is the basic JSON-LD element. It is used to map terms to IRIs or JSON objects. // Context is of of the basic JSON-LD elements. // It is used to map terms to IRIs or JSON objects. // Terms are case sensitive and any valid string that is not a reserved JSON-LD // keyword can be used as a term. type Context map[Term]IRI
-
@@ -123,11 +133,15 @@ type Collapsible interface {// Collapse returns the plain text collapsed value of the current Context object func (c Context) Collapse() interface{} { if len(c) == 1 { for _, iri := range c { for term, iri := range c { if len(c) == 1 { return iri } if term == NilTerm { } } return c }
-
@@ -142,15 +156,46 @@ func (i IRI) MarshalText() ([]byte, error) {} // MarshalJSON returns the JSON document represented by the current Context func (c *Context) MarshalJSON() ([]byte, error) { a := reflectToJSONValue(c) if a.isScalar { return json.Marshal(a.scalar) func (c Context) MarshalJSON() ([]byte, error) { v := func(t Term, i IRI) interface{} { if t.IsNil() && i.IsNil() { return nil } if t.IsNil() && !i.IsNil() { return i } return map[Term]IRI{t: i} } arr := make([]interface{}, len(c)) i := 0 for term, iri := range c { el := v(term, iri) if len(c) == 1 { return json.Marshal(el) } arr[i] = el i += 1 } return json.Marshal(a.object) return json.Marshal(arr) } // UnmarshalJSON tries to load the Context from the incoming json value func (c *Context) UnmarshalJSON(data []byte) error { return nil } // IsNil returns if current LangRef is equal to empty string or to its nil value func (l LangRef) IsNil() bool { return len(l) == 0 || l == NilLangRef } // IsNil returns if current IRI is equal to empty string func (i IRI) IsNil() bool { return len(i) == 0 } // IsNil returns if current Term is equal to empty string or to its nil value func (i Term) IsNil() bool { return len(i) == 0 || i == NilTerm }
-
-
-
@@ -2,6 +2,7 @@ package jsonldimport ( "bytes" "encoding/json" "strings" "testing" )
-
@@ -21,14 +22,34 @@ func TestRef_MarshalText(t *testing.T) {func TestContext_MarshalJSON(t *testing.T) { url := "test" c := Context{NullTerm: IRI(url)} //c.Language = "en-GB" c := Context{NilTerm: IRI(url)} out, err := c.MarshalJSON() if err != nil { t.Errorf("%s", err) } if !strings.Contains(string(out), url) { t.Errorf("Json doesn't contain %#v, %#v", url, string(out)) t.Errorf("Json doesn't contain %s, %s", url, string(out)) } jUrl, _ := json.Marshal(url) if !bytes.Equal(jUrl, out) { t.Errorf("Strings should be equal %s, %s", jUrl, out) } asTerm := "testingTerm##" asUrl := "https://activitipubrocks.com" c2 := Context{NilTerm: IRI(url), Term(asTerm): IRI(asUrl)} out, err = c2.MarshalJSON() if err != nil { t.Errorf("%s", err) } if !strings.Contains(string(out), url) { t.Errorf("Json doesn't contain URL %s, %s", url, string(out)) } if !strings.Contains(string(out), asUrl) { t.Errorf("Json doesn't contain URL %s, %s", asUrl, string(out)) } if !strings.Contains(string(out), asTerm) { t.Errorf("Json doesn't contain Term %s, %s", asTerm, string(out)) } }
-
-
-
@@ -43,75 +43,6 @@ type payloadWithContext struct {Obj interface{} } type jsonCapableValue struct { isScalar bool scalar interface{} object map[string]interface{} } func reflectToJSONValue(v interface{}) jsonCapableValue { a := jsonCapableValue{} a.object = make(map[string]interface{}) typ := reflect.TypeOf(v) val := reflect.ValueOf(v) if typ.Kind() == reflect.Ptr { typ = typ.Elem() val = val.Elem() } switch typ.Kind() { case reflect.Array, reflect.Map, reflect.Slice, reflect.String: a.scalar = val.Interface() a.isScalar = true return a case reflect.Bool: a.scalar = val.Bool() a.isScalar = true return a case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: a.scalar = val.Int() a.isScalar = true return a case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: a.scalar = val.Uint() a.isScalar = true return a case reflect.Float32, reflect.Float64: a.scalar = val.Float() a.isScalar = true return a case reflect.Interface, reflect.Ptr: a.isScalar = true return a } for i := 0; i < typ.NumField(); i++ { cField := typ.Field(i) cValue := val.Field(i) cTag := cField.Tag jsonLdTag, ok := LoadJSONLdTag(cTag) omitEmpty := ok && jsonLdTag.OmitEmpty if jsonLdTag.Ignore { continue } if cField.Anonymous { anonJSONVal := reflectToJSONValue(cValue.Interface()) if anonJSONVal.isScalar { continue } for k, v := range anonJSONVal.object { a.object[k] = v } } empty := isEmptyValue(cValue) if !empty || empty && !omitEmpty { a.object[JSONLdName(cField.Name, jsonLdTag)] = cValue.Interface() } } return a } func (p payloadWithContext) Collapse() interface{} { return p }
-
@@ -132,6 +63,7 @@ func (p payloadWithContext) Marshal(v interface{}) ([]byte, error) {return Marshal(p) } // Tag used by structs from the ActivityPub package to Marshal and Unmarshal to/from JSON-LD type Tag struct { Name string Ignore bool
-
@@ -139,6 +71,7 @@ type Tag struct {Collapsible bool } // LoadJSONLdTag used by structs from the ActivityPub package to Marshal and Unmarshal to/from JSON-LD func LoadJSONLdTag(tag reflect.StructTag) (Tag, bool) { jlTag, ok := tag.Lookup(tagLabel) if !ok {
-
@@ -168,6 +101,7 @@ func LoadJSONLdTag(tag reflect.StructTag) (Tag, bool) {return t, true } // LoadJSONLdTag used by structs from the ActivityPub package to Marshal and Unmarshal to/from JSON-LD func JSONLdName(n string, tag Tag) string { if len(tag.Name) > 0 { return tag.Name
-