Skip to content

Commit

Permalink
Always parse language tag. (hypermodeinc#3243)
Browse files Browse the repository at this point in the history
Currently, the language tag is only being parsed if the value is a
string, which lead to issues in a different PR.
This change makes it so that the language tag is always parsed.
  • Loading branch information
martinmr authored and dna2github committed Jul 19, 2019
1 parent ae1ac0e commit 37ded59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
23 changes: 10 additions & 13 deletions chunker/json/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error {
nq.ObjectValue = &api.Value{Val: &api.Value_IntVal{IntVal: i}}

case string:
// Here we split predicate and lang directive (ex: "name@en"), if needed. With JSON
// mutations that's the only way to send language for a value.
nq.Predicate, nq.Lang = x.PredicateLang(k)

// Default value is considered as S P * deletion.
if v == "" && op == DeleteNquads {
nq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}}
Expand Down Expand Up @@ -261,11 +257,16 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string)
if op == DeleteNquads {
// This corresponds to edge deletion.
if v == nil {
mr.nquads = append(mr.nquads, &api.NQuad{
nq := &api.NQuad{
Subject: mr.uid,
Predicate: pred,
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}},
})
}
// Here we split predicate and lang directive (ex: "name@en"), if needed. With JSON
// mutations that's the only way to send language for a value.
nq.Predicate, nq.Lang = x.PredicateLang(nq.Predicate)

mr.nquads = append(mr.nquads, nq)
continue
}
}
Expand All @@ -284,13 +285,9 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string)
Facets: fts,
}

if v == nil {
if op == DeleteNquads {
nq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}}
mr.nquads = append(mr.nquads, &nq)
}
continue
}
// Here we split predicate and lang directive (ex: "name@en"), if needed. With JSON
// mutations that's the only way to send language for a value.
nq.Predicate, nq.Lang = x.PredicateLang(nq.Predicate)

switch v := v.(type) {
case string, json.Number, bool:
Expand Down
35 changes: 35 additions & 0 deletions chunker/json/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,38 @@ func TestNquadsFromJsonDelete(t *testing.T) {
require.NoError(t, err)
require.Equal(t, nq[0], makeNquadEdge("1000", "friend", "1001"))
}

func TestNquadsFromJsonDeleteStar(t *testing.T) {
json := `{"uid":1000,"name": null}`

nq, err := Parse([]byte(json), DeleteNquads)
require.NoError(t, err)
expected := &api.NQuad{
Subject: "1000",
Predicate: "name",
ObjectValue: &api.Value{
Val: &api.Value_DefaultVal{
DefaultVal: "_STAR_ALL",
},
},
}
require.Equal(t, expected, nq[0])
}

func TestNquadsFromJsonDeleteStarLang(t *testing.T) {
json := `{"uid":1000,"name@es": null}`

nq, err := Parse([]byte(json), DeleteNquads)
require.NoError(t, err)
expected := &api.NQuad{
Subject: "1000",
Predicate: "name",
ObjectValue: &api.Value{
Val: &api.Value_DefaultVal{
DefaultVal: "_STAR_ALL",
},
},
Lang: "es",
}
require.Equal(t, expected, nq[0])
}

0 comments on commit 37ded59

Please sign in to comment.