Skip to content

Commit

Permalink
fix decoding of arrays and nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
mscno committed Mar 7, 2024
1 parent a802949 commit f7372a2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
5 changes: 5 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,17 @@ func TestDecodeFeatureStringId(t *testing.T) {
p.Properties["neg_int"] = -1
p.Properties["string"] = "string"
p.Properties["bool"] = true
p.Properties["null"] = nil
p.Properties["array"] = []interface{}{1.0, 2.0, 3.0}
p.Properties["object"] = map[string]interface{}{"key": "value"}
encoded, err := Encode(p)
require.NoError(t, err)

decoded, err := Decode(encoded)
require.NoError(t, err)

require.Equal(t, p, decoded)

if !reflect.DeepEqual(p, decoded) {
t.Errorf("Expected %+v, got %+v", p, decoded)
}
Expand Down
17 changes: 17 additions & 0 deletions geobufpb/buf.doc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: v1
plugins:
# Documentation
- name: doc
out: gen/doc
opt: html,index.html
strategy: all

- name: doc
out: gen/doc
opt: markdown,index.md
strategy: all

- name: doc
out: gen/doc
opt: json,doc.json
strategy: all
6 changes: 6 additions & 0 deletions geobufpb/buf.elixir.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
plugins:
# Documentation
- plugin: elixir
out: ./elixir
opt: paths=source_relative
19 changes: 0 additions & 19 deletions geobufpb/buf.go.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
version: v1
plugins:
# Documentation
- name: doc
out: gen/doc
opt: html,index.html
strategy: all

- name: doc
out: gen/doc
opt: markdown,index.md
strategy: all

- name: doc
out: gen/doc
opt: json,doc.json
strategy: all

# Go
- plugin: go
out: .
opt: paths=source_relative
- plugin: elixir
out: ./elixir
opt: paths=source_relative
- name: go-vtproto
out: .
opt:
Expand Down
28 changes: 23 additions & 5 deletions internal/decode/feature.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decode

import (
"bytes"
"encoding/json"
"github.com/mscno/go-geobuf/geobufpb"
"github.com/paulmach/orb"
Expand Down Expand Up @@ -39,12 +40,29 @@ func DecodeFeature(msg *geobufpb.Data, feature *geobufpb.Data_Feature, precision
case *geobufpb.Data_Value_NegIntValue:
geoFeature.Properties[msg.Keys[keyIdx]] = int(actualVal.NegIntValue) * -1
case *geobufpb.Data_Value_JsonValue:
var m map[string]interface{}
err := json.Unmarshal(actualVal.JsonValue, &m)
if err != nil {
panic(err)
if bytes.Equal(actualVal.JsonValue, []byte("null")) {
geoFeature.Properties[msg.Keys[keyIdx]] = nil
continue
}
if actualVal.JsonValue == nil {
geoFeature.Properties[msg.Keys[keyIdx]] = nil
continue
}
if actualVal.JsonValue[0] == '{' {
var m map[string]interface{}
err := json.Unmarshal(actualVal.JsonValue, &m)
if err != nil {
panic(err)
}
geoFeature.Properties[msg.Keys[keyIdx]] = m
} else {
var a []interface{}
err := json.Unmarshal(actualVal.JsonValue, &a)
if err != nil {
panic(err)
}
geoFeature.Properties[msg.Keys[keyIdx]] = a
}
geoFeature.Properties[msg.Keys[keyIdx]] = m
}
}
switch id := feature.IdType.(type) {
Expand Down

0 comments on commit f7372a2

Please sign in to comment.