Going deeper and deeper down the jsonld rabbit hole.

The current approach doesn't look too good
This commit is contained in:
Marius Orcsik 2018-08-01 10:45:21 +02:00
parent 5a2c4b51dd
commit f40d875d04
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
5 changed files with 135 additions and 149 deletions

View file

@ -108,6 +108,10 @@ type ContextObject struct {
// keyword can be used as a term.
type Context map[Term]IRI
func GetContext() Context {
return Context{}
}
//type Context Collapsible
// Collapsible is an interface used by the JSON-LD marshaller to collapse a struct to one single value
@ -116,9 +120,9 @@ 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 {
func (c Context) Collapse() interface{} {
if len(c) == 1 {
for _, iri := range c {
return iri
}
}

View file

@ -1,6 +1,7 @@
package jsonld
import (
"strconv"
"testing"
)
@ -32,135 +33,117 @@ type mockWithContext struct {
Context Context `jsonld:"@context"`
}
//func TestUnmarshalWithEmptyJsonObjectWithStringContext(t *testing.T) {
// obj := mockWithContext{}
// url := "http://www.habarnam.ro"
// data := []byte(`{"@context": "` + url + `" }`)
// err := Unmarshal(data, &obj)
// if err != nil {
// t.Error(err)
// }
// if obj.Context.Ref() != Ref(url) {
// t.Errorf("@context should have been %q, found %q", url, obj.Context.Collapse())
// }
// if obj.Id != "" {
// t.Errorf("Id should have been an empty string, found %s", obj.Id)
// }
// if obj.Name != "" {
// t.Errorf("Name should have been an empty string, found %s", obj.Name)
// }
// if obj.Type != "" {
// t.Errorf("Type should have been an empty string, found %s", obj.Type)
// }
// if obj.PropA != "" {
// t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
// }
// if obj.PropB != 0 {
// t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
// }
//}
func TestUnmarshalWithEmptyJsonObjectWithStringContext(t *testing.T) {
obj := mockWithContext{}
url := "http://www.habarnam.ro"
data := []byte(`{"@context": "` + url + `" }`)
err := Unmarshal(data, &obj)
if err != nil {
t.Error(err)
}
if obj.Id != "" {
t.Errorf("Id should have been an empty string, found %s", obj.Id)
}
if obj.Name != "" {
t.Errorf("Name should have been an empty string, found %s", obj.Name)
}
if obj.Type != "" {
t.Errorf("Type should have been an empty string, found %s", obj.Type)
}
if obj.PropA != "" {
t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
}
if obj.PropB != 0 {
t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
}
}
//func TestUnmarshalWithEmptyJsonObjectWithObjectContext(t *testing.T) {
// obj := mockWithContext{}
// url := "http://www.habarnam.ro"
// data := []byte(`{"@context": { "@url": "` + url + `"} }`)
// err := Unmarshal(data, &obj)
// if err != nil {
// t.Error(err)
// }
// if obj.Context.Ref() != Ref(url) {
// t.Errorf("@context should have been %q, found %q", url, obj.Context.Collapse())
// }
// if obj.Id != "" {
// t.Errorf("Id should have been an empty string, found %s", obj.Id)
// }
// if obj.Name != "" {
// t.Errorf("Name should have been an empty string, found %s", obj.Name)
// }
// if obj.Type != "" {
// t.Errorf("Type should have been an empty string, found %s", obj.Type)
// }
// if obj.PropA != "" {
// t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
// }
// if obj.PropB != 0 {
// t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
// }
//}
func TestUnmarshalWithEmptyJsonObjectWithObjectContext(t *testing.T) {
obj := mockWithContext{}
url := "http://www.habarnam.ro"
data := []byte(`{"@context": { "@url": "` + url + `"} }`)
err := Unmarshal(data, &obj)
if err != nil {
t.Error(err)
}
if obj.Id != "" {
t.Errorf("Id should have been an empty string, found %s", obj.Id)
}
if obj.Name != "" {
t.Errorf("Name should have been an empty string, found %s", obj.Name)
}
if obj.Type != "" {
t.Errorf("Type should have been an empty string, found %s", obj.Type)
}
if obj.PropA != "" {
t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
}
if obj.PropB != 0 {
t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
}
}
//func TestUnmarshalWithEmptyJsonObjectWithOneLanguageContext(t *testing.T) {
// obj := mockWithContext{}
// url := "http://www.habarnam.ro"
// langEn := "en-US"
//
// data := []byte(`{"@context": { "@url": "` + url + `", "@language": "` + langEn + `"} }`)
// err := Unmarshal(data, &obj)
// if err != nil {
// t.Error(err)
// }
// if obj.Context.Ref() != Ref(url) {
// t.Errorf("@context should have been %q, found %q", url, obj.Context.Collapse())
// }
// if obj.Context.Language != LangRef(langEn) {
// t.Errorf("@context should have been %q, found %q", url, obj.Context.Collapse())
// }
// if obj.Id != "" {
// t.Errorf("Id should have been an empty string, found %s", obj.Id)
// }
// if obj.Name != "" {
// t.Errorf("Name should have been an empty string, found %s", obj.Name)
// }
// if obj.Type != "" {
// t.Errorf("Type should have been an empty string, found %s", obj.Type)
// }
// if obj.PropA != "" {
// t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
// }
// if obj.PropB != 0 {
// t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
// }
//}
//func TestUnmarshalWithEmptyJsonObjectWithFullObject(t *testing.T) {
// obj := mockWithContext{}
// url := "http://www.habarnam.ro"
// langEn := "en-US"
// propA := "ana"
// var propB float32 = 6.66
// typ := "test"
// name := "test object #1"
// id := "777sdad"
//
// data := []byte(`{
// "@context": { "@url": "` + url + `", "@language": "` + langEn + `"},
// "PropA": "` + propA + `",
// "PropB": ` + strconv.FormatFloat(float64(propB), 'f', 2, 32) + `,
// "Id" : "` + id + `",
// "Name" : "` + name + `",
// "Type" : "` + typ + `"
// }`)
// err := Unmarshal(data, &obj)
// if err != nil {
// t.Error(err)
// }
// if obj.Context.Ref() != Ref(url) {
// t.Errorf("@context should have been %q, found %q", url, obj.Context.Collapse())
// }
// if obj.Context.Language != LangRef(langEn) {
// t.Errorf("@context should have been %q, found %q", url, obj.Context.Collapse())
// }
// if obj.Id != id {
// t.Errorf("Id should have been %q, found %q", id, obj.Id)
// }
// if obj.Name != name {
// t.Errorf("Name should have been %q, found %q", name, obj.Name)
// }
// if obj.Type != typ {
// t.Errorf("Type should have been %q, found %q", typ, obj.Type)
// }
// if obj.PropA != propA {
// t.Errorf("PropA should have been %q, found %q", propA, obj.PropA)
// }
// if obj.PropB != propB {
// t.Errorf("PropB should have been %f, found %f", propB, obj.PropB)
// }
//}
func TestUnmarshalWithEmptyJsonObjectWithOneLanguageContext(t *testing.T) {
obj := mockWithContext{}
url := "http://www.habarnam.ro"
langEn := "en-US"
data := []byte(`{"@context": { "@url": "` + url + `", "@language": "` + langEn + `"} }`)
err := Unmarshal(data, &obj)
if err != nil {
t.Error(err)
}
if obj.Id != "" {
t.Errorf("Id should have been an empty string, found %s", obj.Id)
}
if obj.Name != "" {
t.Errorf("Name should have been an empty string, found %s", obj.Name)
}
if obj.Type != "" {
t.Errorf("Type should have been an empty string, found %s", obj.Type)
}
if obj.PropA != "" {
t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
}
if obj.PropB != 0 {
t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
}
}
func TestUnmarshalWithEmptyJsonObjectWithFullObject(t *testing.T) {
obj := mockWithContext{}
url := "http://www.habarnam.ro"
langEn := "en-US"
propA := "ana"
var propB float32 = 6.66
typ := "test"
name := "test object #1"
id := "777sdad"
data := []byte(`{
"@context": { "@url": "` + url + `", "@language": "` + langEn + `"},
"PropA": "` + propA + `",
"PropB": ` + strconv.FormatFloat(float64(propB), 'f', 2, 32) + `,
"Id" : "` + id + `",
"Name" : "` + name + `",
"Type" : "` + typ + `"
}`)
err := Unmarshal(data, &obj)
if err != nil {
t.Error(err)
}
if obj.Id != id {
t.Errorf("Id should have been %q, found %q", id, obj.Id)
}
if obj.Name != name {
t.Errorf("Name should have been %q, found %q", name, obj.Name)
}
if obj.Type != typ {
t.Errorf("Type should have been %q, found %q", typ, obj.Type)
}
if obj.PropA != propA {
t.Errorf("PropA should have been %q, found %q", propA, obj.PropA)
}
if obj.PropB != propB {
t.Errorf("PropB should have been %f, found %f", propB, obj.PropB)
}
}

View file

@ -28,7 +28,7 @@ import (
"unicode/utf8"
)
var Ctxt Context
var Ctxt Collapsible
const (
tagLabel = "jsonld"
@ -118,6 +118,7 @@ func (p payloadWithContext) Collapse() interface{} {
// WithContext
func WithContext(c Collapsible) payloadWithContext {
Ctxt = c
return payloadWithContext{
Context: c,
}

View file

@ -15,16 +15,16 @@ func TestAcceptSerialization(t *testing.T) {
obj.Name["en"] = "test"
obj.Name["fr"] = "teste"
j.Ctx = j.Context{URL: "https://www.w3.org/ns/activitystreams"}
uri := "https://www.w3.org/ns/activitystreams"
p := j.WithContext(j.IRI(uri))
data, err := j.Marshal(obj)
data, err := p.Marshal(obj)
if err != nil {
t.Errorf("Error: %v", err)
}
ctxt := j.Ctx.(j.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), uri) {
t.Errorf("Could not find context url %#v in output %s", p.Context, data)
}
if !strings.Contains(string(data), string(obj.ID)) {
t.Errorf("Could not find id %#v in output %s", string(obj.ID), data)
@ -45,17 +45,15 @@ func TestCreateActivityHTTPSerialization(t *testing.T) {
obj := a.AcceptNew(id, nil)
obj.Name["en"] = "Accept New"
baseURI := string(a.ActivityBaseURI)
j.Ctx = j.Context{
URL: j.Ref(baseURI + string(obj.Type)),
}
data, err := j.Marshal(obj)
uri := string(a.ActivityBaseURI)
data, err := j.WithContext(j.IRI(uri)).Marshal(obj)
if err != nil {
t.Error(err)
t.Errorf("Error: %v", err)
}
ctxt := j.Ctx.(j.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), uri) {
t.Errorf("Could not find context url %#v in output %s", j.GetContext(), data)
}
if !strings.Contains(string(data), string(obj.ID)) {
t.Errorf("Could not find id %#v in output %s", string(obj.ID), data)

View file

@ -79,7 +79,7 @@ var allTests = tests{
// a.LangRef("-"): "Sally",
// },
// }),
// Object: a.Object{
// payloadWithContext: a.payloadWithContext{
// Type: a.NoteType,
// Name: a.NaturalLanguageValue{
// a.LangRef("-"): "A Note",