Skip to content

Commit

Permalink
Correctly handle a list of type geo in json (dgraph-io#2482) (dgraph-…
Browse files Browse the repository at this point in the history
…io#2485)

* Correctly handle list of type geo in json (dgraph-io#2482)

* Add handleGeoType function for handling a geotype or list of geotype (dgraph-io#2482)
  • Loading branch information
commesan authored and dna2github committed Jul 19, 2019
1 parent 9957e22 commit ae97d17
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
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

0 comments on commit ae97d17

Please sign in to comment.