Skip to content

Commit

Permalink
feat export item fields according to schema order
Browse files Browse the repository at this point in the history
  • Loading branch information
yk-eukarya committed Feb 19, 2025
1 parent cd53a76 commit 866fdb9
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 97 deletions.
6 changes: 5 additions & 1 deletion server/e2e/integration_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ func TestIntegrationItemsAsGeoJSON(t *testing.T) {
sId, _, _ := getModel(e, mId)
i1Id, _ := createItem(e, mId, sId, nil, []map[string]any{
{"schemaFieldId": fids.textFId, "value": "test1", "type": "Text"},
{"schemaFieldId": fids.numberFId, "value": 1, "type": "Number"},
{"schemaFieldId": fids.geometryObjectFid, "value": "{\"coordinates\":[139.28179282584915,36.58570985749664],\"type\":\"Point\"}", "type": "GeometryObject"},
})

Expand All @@ -1368,7 +1369,10 @@ func TestIntegrationItemsAsGeoJSON(t *testing.T) {
f := features.Value(0).Object()
f.Value("id").String().IsEqual(i1Id)
f.Value("type").String().IsEqual("Feature")
f.Value("properties").Object().Value("text").String().IsEqual("test1")
f.Value("properties").Object().IsEqual(map[string]any{
"number": 1,
"text": "test1",
})
g := f.Value("geometry").Object()
g.Value("type").String().IsEqual("Point")
g.Value("coordinates").Array().IsEqual([]float64{139.28179282584915, 36.58570985749664})
Expand Down
148 changes: 74 additions & 74 deletions server/internal/adapter/integration/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions server/internal/adapter/publicapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path"
"reflect"

"github.com/iancoleman/orderedmap"
"github.com/reearth/reearth-cms/server/pkg/asset"
"github.com/reearth/reearth-cms/server/pkg/item"
"github.com/reearth/reearth-cms/server/pkg/schema"
Expand Down Expand Up @@ -249,10 +250,10 @@ type FeatureType string
const FeatureTypeFeature FeatureType = "Feature"

type Feature struct {
Geometry *Geometry `json:"geometry,omitempty"`
Id *string `json:"id,omitempty"`
Properties *map[string]interface{} `json:"properties,omitempty"`
Type *FeatureType `json:"type,omitempty"`
Geometry *Geometry `json:"geometry,omitempty"`
Id *string `json:"id,omitempty"`
Properties *orderedmap.OrderedMap `json:"properties,omitempty"`
Type *FeatureType `json:"type,omitempty"`
}

type GeometryCollectionType string
Expand Down
31 changes: 17 additions & 14 deletions server/pkg/exporters/geojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"time"

"github.com/iancoleman/orderedmap"
"github.com/reearth/reearth-cms/server/pkg/item"
"github.com/reearth/reearth-cms/server/pkg/schema"
"github.com/reearth/reearth-cms/server/pkg/value"
Expand Down Expand Up @@ -32,10 +33,10 @@ type FeatureType string
const FeatureTypeFeature FeatureType = "Feature"

type Feature struct {
Geometry *Geometry `json:"geometry,omitempty"`
Id *string `json:"id,omitempty"`
Properties *map[string]interface{} `json:"properties,omitempty"`
Type *FeatureType `json:"type,omitempty"`
Geometry *Geometry `json:"geometry,omitempty"`
Id *string `json:"id,omitempty"`
Properties *orderedmap.OrderedMap `json:"properties,omitempty"`
Type *FeatureType `json:"type,omitempty"`
}

type GeometryCollectionType string
Expand Down Expand Up @@ -162,21 +163,23 @@ func FeatureFromItem(ver item.Versioned, s *schema.Schema) (Feature, bool) {
}, true
}

func extractProperties(itm *item.Item, s *schema.Schema) *map[string]any {
func extractProperties(itm *item.Item, s *schema.Schema) *orderedmap.OrderedMap {
if itm == nil || s == nil {
return nil
}
properties := make(map[string]any)
for _, field := range s.Fields() {
if field.Type() != value.TypeGeometryObject && field.Type() != value.TypeGeometryEditor {
key := field.Name()
itmField := itm.Field(field.ID())
if val, ok := toGeoJSONProp(itmField); ok {
properties[key] = val
}
properties := orderedmap.New()
for _, field := range s.Fields().Ordered() {
if field.Type() == value.TypeGeometryObject || field.Type() == value.TypeGeometryEditor {
continue
}

key := field.Name()
itmField := itm.Field(field.ID())
if val, ok := toGeoJSONProp(itmField); ok {
properties.Set(key, val)
}
}
return &properties
return properties
}

func extractGeometry(field *item.Field) (*Geometry, bool) {
Expand Down
9 changes: 5 additions & 4 deletions server/pkg/integrationapi/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/schemas/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,7 @@ components:
$ref: '#/components/schemas/Geometry'
properties:
type: object
x-go-type: orderedmap.OrderedMap
Geometry:
type: object
properties:
Expand Down

0 comments on commit 866fdb9

Please sign in to comment.