Changes
2 changed files (+49/-26)
-
-
@@ -13,12 +13,12 @@ const NilLangRef LangRef = "-"type ( // LangRef is the type for a language reference code, should be an ISO639-1 language specifier. LangRef string LangVal string Content string // LangRefValue is a type for storing per language values LangRefValue struct { Ref LangRef Value LangVal Value Content } // NaturalLanguageValues is a mapping for multiple language values NaturalLanguageValues []LangRefValue
-
@@ -45,7 +45,7 @@ func (n NaturalLanguageValues) String() string {return s.String() } func (n NaturalLanguageValues) Get(ref LangRef) LangVal { func (n NaturalLanguageValues) Get(ref LangRef) Content { for _, val := range n { if val.Ref == ref { return val.Value
-
@@ -55,7 +55,7 @@ func (n NaturalLanguageValues) Get(ref LangRef) LangVal {} // Set sets a language, value pair in a NaturalLanguageValues array func (n *NaturalLanguageValues) Set(ref LangRef, v LangVal) error { func (n *NaturalLanguageValues) Set(ref LangRef, v Content) error { found := false for k, vv := range *n { if vv.Ref == ref {
-
@@ -80,7 +80,7 @@ func (n NaturalLanguageValues) MarshalJSON() ([]byte, error) {if l == 1 { v := n[0] if len(v.Value) > 0 { v.Value = LangVal(unescape([]byte(v.Value))) v.Value = Content(unescape([]byte(v.Value))) ll, err := b.WriteString(strconv.Quote(v.Value.String())) if err != nil { return nil, err
-
@@ -132,7 +132,7 @@ func (n NaturalLanguageValues) MarshalText() ([]byte, error) {// Append is syntactic sugar for resizing the NaturalLanguageValues map // and appending an element func (n *NaturalLanguageValues) Append(lang LangRef, value LangVal) error { func (n *NaturalLanguageValues) Append(lang LangRef, value Content) error { var t NaturalLanguageValues if len(*n) == 0 { t = make(NaturalLanguageValues, 0)
-
@@ -163,19 +163,19 @@ func (l *LangRefValue) UnmarshalJSON(data []byte) error {_, typ, _, err := jsonparser.Get(data) if err != nil { l.Ref = NilLangRef l.Value = LangVal(unescape(data)) l.Value = Content(unescape(data)) return nil } switch typ { case jsonparser.Object: jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error { l.Ref = LangRef(key) l.Value = LangVal(unescape(value)) l.Value = Content(unescape(value)) return err }) case jsonparser.String: l.Ref = NilLangRef l.Value = LangVal(unescape(data)) l.Value = Content(unescape(data)) } return nil
-
@@ -197,7 +197,7 @@ func (l LangRefValue) MarshalJSON() ([]byte, error) {buf.WriteString(l.Ref.String()) buf.Write([]byte{'"', ':'}) } l.Value = LangVal(unescape([]byte(l.Value))) l.Value = Content(unescape([]byte(l.Value))) buf.WriteString(strconv.Quote(l.Value.String())) return buf.Bytes(), nil }
-
@@ -224,7 +224,7 @@ func (l *LangRef) UnmarshalJSON(data []byte) error {// UnmarshalText implements the TextEncoder interface func (l *LangRef) UnmarshalText(data []byte) error { *l = LangRef("") *l = "" if len(data) == 0 { return nil }
-
@@ -242,15 +242,26 @@ func (l LangRef) String() string {return string(l) } func (l *LangVal) UnmarshalJSON(data []byte) error { return nil func (c *Content) UnmarshalJSON(data []byte) error { return c.UnmarshalText(data) } func (l *LangVal) UnmarshalText(data []byte) error { func (c *Content) UnmarshalText(data []byte) error { *c = "" if len(data) == 0 { return nil } if len(data) > 2 { if data[0] == '"' && data[len(data)-1] == '"' { *c = Content(data[1 : len(data)-1]) } } else { *c = Content(data) } return nil } func (l LangVal) String() string { return string(l) func (c Content) String() string { return string(c) } func unescape(b []byte) []byte {
-
@@ -272,17 +283,17 @@ func (n *NaturalLanguageValues) UnmarshalJSON(data []byte) error {val, typ, _, err := jsonparser.Get(data) if err != nil { // try our luck if data contains an unquoted string n.Append(NilLangRef, LangVal(unescape(data))) n.Append(NilLangRef, Content(unescape(data))) return nil } switch typ { case jsonparser.Object: jsonparser.ObjectEach(data, func(key []byte, val []byte, dataType jsonparser.ValueType, offset int) error { n.Append(LangRef(key), LangVal(unescape(val))) n.Append(LangRef(key), Content(unescape(val))) return err }) case jsonparser.String: n.Append(NilLangRef, LangVal(unescape(val))) n.Append(NilLangRef, Content(unescape(val))) case jsonparser.Array: jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) { l := LangRefValue{}
-
@@ -301,7 +312,7 @@ func (n *NaturalLanguageValues) UnmarshalText(data []byte) error {if data[len(data)-1] != '"' { return fmt.Errorf("invalid string value when unmarshaling %T value", n) } n.Append(LangRef(NilLangRef), LangVal(data[1:len(data)-1])) n.Append(LangRef(NilLangRef), Content(data[1:len(data)-1])) } return nil }
-
-
-
@@ -119,7 +119,7 @@ func TestLangRefValue_MarshalText(t *testing.T) {} func TestNaturalLanguageValue_Get(t *testing.T) { testVal := LangVal("test") testVal := Content("test") a := NaturalLanguageValues{{NilLangRef, testVal}} if a.Get(NilLangRef) != testVal { t.Errorf("Invalid Get result. Expected %s received %s", testVal, a.Get(NilLangRef))
-
@@ -127,7 +127,7 @@ func TestNaturalLanguageValue_Get(t *testing.T) {} func TestNaturalLanguageValue_Set(t *testing.T) { testVal := LangVal("test") testVal := Content("test") a := NaturalLanguageValues{{NilLangRef, "ana are mere"}} err := a.Set(LangRef("en"), testVal) if err != nil {
-
@@ -142,7 +142,7 @@ func TestNaturalLanguageValue_Append(t *testing.T) {t.Errorf("Invalid initialization of %T. Size %d > 0 ", a, len(a)) } langEn := LangRef("en") valEn := LangVal("random value") valEn := Content("random value") a.Append(langEn, valEn) if len(a) != 1 {
-
@@ -152,7 +152,7 @@ func TestNaturalLanguageValue_Append(t *testing.T) {t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langEn, valEn, a.Get(langEn)) } langDe := LangRef("de") valDe := LangVal("randomisch") valDe := Content("randomisch") a.Append(langDe, valDe) if len(a) != 2 {
-
@@ -180,9 +180,9 @@ func TestLangRef_UnmarshalJSON(t *testing.T) {func TestNaturalLanguageValue_UnmarshalFullObjectJSON(t *testing.T) { langEn := "en-US" valEn := LangVal("random") valEn := Content("random") langDe := "de-DE" valDe := LangVal("zufällig\n") valDe := Content("zufällig\n") //m := make(map[string]string) //m[langEn] = valEn
-
@@ -527,3 +527,15 @@ func TestNaturalLanguageValues_Equals(t *testing.T) {}) } } func TestContent_String(t *testing.T) { t.Skip("TODO") } func TestContent_UnmarshalJSON(t *testing.T) { t.Skip("TODO") } func TestContent_UnmarshalText(t *testing.T) { t.Skip("TODO") }
-