From c946ab3783929187755cc3323b66aee7cfd179a2 Mon Sep 17 00:00:00 2001 From: commesan Date: Thu, 12 Jul 2018 21:53:40 +0200 Subject: [PATCH 1/2] Correctly handle list of type geo in json (#2482) --- edgraph/nquads_from_json.go | 20 ++++++++++++++++++++ edgraph/server_test.go | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/edgraph/nquads_from_json.go b/edgraph/nquads_from_json.go index a6bb551f480..3b3c727d059 100644 --- a/edgraph/nquads_from_json.go +++ b/edgraph/nquads_from_json.go @@ -323,6 +323,26 @@ 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. + val := item.(map[string]interface{}) + _, 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 + } + } + cr, err := mapToNquads(iv, idx, op, pred) if err != nil { return mr, err diff --git a/edgraph/server_test.go b/edgraph/server_test.go index 827ff2462f7..70092028501 100644 --- a/edgraph/server_test.go +++ b/edgraph/server_test.go @@ -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) { From 5cca366f3a29d114a58b58d3f0c6d705c83ed1e6 Mon Sep 17 00:00:00 2001 From: commesan Date: Mon, 16 Jul 2018 22:59:56 +0200 Subject: [PATCH 2/2] Add handleGeoType function for handling a geotype or list of geotype (#2482) --- edgraph/nquads_from_json.go | 68 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/edgraph/nquads_from_json.go b/edgraph/nquads_from_json.go index 3b3c727d059..0419ebd0262 100644 --- a/edgraph/nquads_from_json.go +++ b/edgraph/nquads_from_json.go @@ -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) @@ -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) @@ -324,23 +334,13 @@ 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. - val := item.(map[string]interface{}) - _, 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(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)