Changes
2 changed files (+96/-61)
-
-
@@ -115,10 +115,16 @@ type ContextObject struct {} // Context is of of the basic JSON-LD elements. // It is used to map terms to IRIs or JSON objects. // It represents an array of ContextElements type Context []ContextElement // ContextElement 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 type ContextElement struct { Term Term IRI IRI } func GetContext() Context { return Context{}
-
@@ -133,11 +139,11 @@ type Collapsible interface {// Collapse returns the plain text collapsed value of the current Context object func (c Context) Collapse() interface{} { for term, iri := range c { if len(c) == 1 { return iri } if term == NilTerm { if len(c) == 1 && len(c[0].IRI) > 0 { return c[0].IRI } for _, el := range c { if el.Term == NilTerm { } }
-
@@ -162,23 +168,25 @@ func (i IRI) MarshalText() ([]byte, error) {// If multiple elements where at least one doesn't have a Term and one has a Term -> json marshaled array // If multiple elements where all have Terms -> json marshaled object func (c Context) MarshalJSON() ([]byte, error) { mapIRI := make(map[Term]IRI, 0) arr := make([]interface{}, 0) i := 0 for t, iri := range c { if len(c) == 1 && len(c[0].IRI) > 0 { return json.Marshal(c[0].IRI) } for _, el := range c { t := el.Term iri := el.IRI if t.IsNil() { if len(c) == 1 { return json.Marshal(iri) } arr = append(arr, iri) delete(c, t) i += 1 } else { if len(iri) > 0 { mapIRI[t] = iri } } } if len(c) > 0 { mapIRI := make(map[Term]IRI, len(c)) for t, iri := range c { mapIRI[t] = iri } if len(mapIRI) > 0 { if len(arr) == 0 { return json.Marshal(mapIRI) }
-
-
-
@@ -21,54 +21,81 @@ func TestRef_MarshalText(t *testing.T) {} func TestContext_MarshalJSON(t *testing.T) { url := "test" c := Context{NilTerm: IRI(url)} { url := "test" 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 %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) out, err := c.MarshalJSON() if err != nil { t.Errorf("%s", err) } if !strings.Contains(string(out), url) { 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) } } if !strings.Contains(string(out), url) { t.Errorf("Json doesn't contain URL %s, %s", url, string(out)) { url := "example.com" 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)) } } 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)) { url := "test" testTerm := "test_term" asTerm := "testingTerm##" asUrl := "https://activitipubrocks.com" c3 := Context{{Term(testTerm), IRI(url)}, {Term(asTerm), IRI(asUrl)}} out, err := c3.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)) } if !strings.Contains(string(out), testTerm) { t.Errorf("Json doesn't contain Term %s, %s", testTerm, string(out)) } } { url1 := "test" url2 := "http://example.com" c := Context{ {IRI: IRI(url1)}, {IRI: IRI(url2)}, } testTerm := "test_term" c3 := Context{Term(testTerm): IRI(url), Term(asTerm): IRI(asUrl)} out, err = c3.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)) } if !strings.Contains(string(out), testTerm) { t.Errorf("Json doesn't contain Term %s, %s", testTerm, string(out)) out, err := c.MarshalJSON() if err != nil { t.Errorf("%s", err) } if !strings.Contains(string(out), url1) { t.Errorf("Json doesn't contain %s, %s", url1, string(out)) } if !strings.Contains(string(out), url2) { t.Errorf("Json doesn't contain %s, %s", url1, string(out)) } } }
-