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

Add JSONB column support #275

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions database/postgis/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func init() {
"int64": &simpleColumnType{"BIGINT"},
"float32": &simpleColumnType{"REAL"},
"hstore_string": &simpleColumnType{"HSTORE"},
"jsonb_string": &simpleColumnType{"JSONB"},
"geometry": &geometryType{"GEOMETRY"},
"validated_geometry": &validatedGeometryType{geometryType{"GEOMETRY"}},
}
Expand Down
30 changes: 30 additions & 0 deletions mapping/columns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mapping

import (
"encoding/json"
"math"
"regexp"
"strconv"
Expand Down Expand Up @@ -33,6 +34,7 @@ func init() {
"geometry": {"geometry", "geometry", Geometry, nil, nil, false},
"validated_geometry": {"validated_geometry", "validated_geometry", Geometry, nil, nil, false},
"hstore_tags": {"hstore_tags", "hstore_string", nil, MakeHStoreString, nil, false},
"jsonb_tags": {"jsonb_tags", "jsonb_string", nil, MakeJSONBString, nil, false},
"wayzorder": {"wayzorder", "int32", nil, MakeWayZOrder, nil, false},
"pseudoarea": {"pseudoarea", "float32", nil, MakePseudoArea, nil, false},
"area": {"area", "float32", Area, nil, nil, false},
Expand Down Expand Up @@ -195,6 +197,34 @@ func MakeHStoreString(columnName string, columnType ColumnType, column config.Co
return hstoreString, nil
}


func MakeJSONBString(columnName string, columnType ColumnType, column config.Column) (MakeValue, error) {
var includeAll bool
var err error
var include map[string]int
if _, ok := column.Args["include"]; !ok {
includeAll = true
} else {
include, err = decodeEnumArg(column, "include")
if err != nil {
return nil, err
}

}

jsonbString := func(val string, elem *osm.Element, geom *geom.Geometry, match Match) interface{} {
tags := make(map[string]string)
for k, v := range elem.Tags {
if includeAll || include[k] != 0 {
tags[k] = v
}
}
json, _ := json.Marshal(tags)
return string(json);
}
return jsonbString, nil
}

func MakeWayZOrder(columnName string, columnType ColumnType, column config.Column) (MakeValue, error) {
if _, ok := column.Args["ranks"]; !ok {
return DefaultWayZOrder, nil
Expand Down
48 changes: 48 additions & 0 deletions mapping/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,51 @@ func TestHstoreString(t *testing.T) {
}

}

func TestJSONBString(t *testing.T) {
column := config.Column{
Name: "tags",
Type: "jsonb_tags",
}
jsonbAll, err := MakeJSONBString("tags", ColumnType{}, column)
if err != nil {
t.Fatal(err)
}

column = config.Column{
Name: "tags",
Type: "jsonb_tags",
Args: map[string]interface{}{"include": []interface{}{"key1", "key2"}},
}
jsonbInclude, err := MakeJSONBString("tags", ColumnType{}, column)
if err != nil {
t.Fatal(err)
}

for _, test := range []struct {
column MakeValue
tags osm.Tags
expected interface{}
}{
{jsonbAll, osm.Tags{}, `{}`},
{jsonbAll, osm.Tags{"key": "value"}, `{"key":"value"}`},
{jsonbAll, osm.Tags{`"key"`: `'"value"'`}, `{"\"key\"":"'\"value\"'"}`},
{jsonbAll, osm.Tags{`\`: `\\\\`}, `{"\\":"\\\\\\\\"}`},
{jsonbAll, osm.Tags{"Ümlåütê:": ""}, `{"Ümlåütê:":""}`},
{jsonbInclude, osm.Tags{"key": "value"}, `{}`},
{jsonbInclude, osm.Tags{"key1": "value"}, `{"key1":"value"}`},
{jsonbInclude, osm.Tags{"key": "value", "key2": "value"}, `{"key2":"value"}`},
} {
actual := test.column("", &osm.Element{Tags: test.tags}, nil, Match{})
if actual.(string) != test.expected {
t.Errorf("%#v != %#v for %#v", actual, test.expected, test.tags)
}
}

actual := jsonbAll("{}", &osm.Element{Tags: osm.Tags{"key1": "value", "key2": "value"}}, nil, Match{})
// check mutliple tags, can be in any order
if actual.(string) != `{"key1":"value","key2":"value"}` && actual.(string) != `{"key2":"value","key1":"value"}` {
t.Error("unexpected value", actual)
}

}