Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle a list of type geo in json (#2482) #2485

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions edgraph/nquads_from_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ func checkForDeletion(mr *mapResponse, m map[string]interface{}, op int) {
}
}

func handleGeoType(val map[string]interface{}, nq *api.NQuad) (bool, error) {
_, hasType := val["type"]
_, hasCoordinates := val["coordinates"]
if len(val) == 2 && hasType && hasCoordinates {
b, err := json.Marshal(val)
if err != nil {
return false, x.Errorf("Error while trying to parse "+
"value: %+v as geo val", val)
}
ok, err := tryParseAsGeo(b, nq)
if err != nil && ok {
return true, err
}
if ok {
return true, nil
}
}
return false, nil
}

func tryParseAsGeo(b []byte, nq *api.NQuad) (bool, error) {
var g geom.T
err := geojson.Unmarshal(b, &g)
Expand Down Expand Up @@ -279,23 +299,13 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string)
continue
}

// Geojson geometry should have type and coordinates.
_, hasType := val["type"]
_, hasCoordinates := val["coordinates"]
if len(val) == 2 && hasType && hasCoordinates {
b, err := json.Marshal(val)
if err != nil {
return mr, x.Errorf("Error while trying to parse "+
"value: %+v as geo val", val)
}
ok, err := tryParseAsGeo(b, &nq)
if err != nil {
return mr, err
}
if ok {
mr.nquads = append(mr.nquads, &nq)
continue
}
ok, err := handleGeoType(val, &nq)
if err != nil {
return mr, err
}
if ok {
mr.nquads = append(mr.nquads, &nq)
continue
}

cr, err := mapToNquads(v.(map[string]interface{}), idx, op, pred)
Expand Down Expand Up @@ -323,6 +333,16 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string)
}
mr.nquads = append(mr.nquads, &nq)
case map[string]interface{}:
// map[string]interface{} can mean geojson or a connecting entity.
ok, err := handleGeoType(item.(map[string]interface{}), &nq)
if err != nil {
return mr, err
}
if ok {
mr.nquads = append(mr.nquads, &nq)
continue
}

cr, err := mapToNquads(iv, idx, op, pred)
if err != nil {
return mr, err
Expand Down
4 changes: 2 additions & 2 deletions edgraph/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ func TestNquadsFromJsonError1(t *testing.T) {
}

func TestNquadsFromJsonList(t *testing.T) {
json := `{"address":["Riley Street","Redfern"],"phone_number":[123,9876]}`
json := `{"address":["Riley Street","Redfern"],"phone_number":[123,9876],"points":[{"type":"Point", "coordinates":[1.1,2.0]},{"type":"Point", "coordinates":[2.0,1.1]}]}`

nq, err := nquadsFromJson([]byte(json), set)
require.NoError(t, err)
require.Equal(t, 4, len(nq))
require.Equal(t, 6, len(nq))
}

func TestNquadsFromJsonDelete(t *testing.T) {
Expand Down