Skip to content

Commit

Permalink
chore(server, web): add map valueType to property (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyshx authored Oct 25, 2023
1 parent c8a1752 commit db515bf
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 46 deletions.
1 change: 1 addition & 0 deletions server/gql/_shared.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ scalar FileSize
scalar TranslatedString
scalar Cursor
scalar JSON
scalar Map

# Meta Type

Expand Down
1 change: 1 addition & 0 deletions server/internal/adapter/gql/generated.go

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

2 changes: 1 addition & 1 deletion server/pkg/builtin/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ extensions:
title: Background Color
ui: color
- id: showLayers
type: string
type: map
title: Show Layers
ui: layer
choices:
1 change: 1 addition & 0 deletions server/pkg/property/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
ValueTypeCoordinates = ValueType(value.TypeCoordinates)
ValueTypePolygon = ValueType(value.TypePolygon)
ValueTypeRect = ValueType(value.TypeRect)
ValueTypeMap = ValueType(value.TypeMap)
)

var types = value.TypePropertyMap{
Expand Down
61 changes: 61 additions & 0 deletions server/pkg/value/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package value

import (
"encoding/json"
)

var TypeMap Type = "map"

type propertyMap struct{}

func (p *propertyMap) I2V(i interface{}) (interface{}, bool) {
switch v := i.(type) {
case map[string]interface{}:
return v, true
case *map[string]interface{}:
if v != nil {
return *v, true
}
case string:
var result map[string]interface{}
if err := json.Unmarshal([]byte(v), &result); err == nil {
return result, true
}
}
return nil, false
}

func (*propertyMap) V2I(v interface{}) (interface{}, bool) {
_, ok := v.(map[string]interface{})
return v, ok
}

func (*propertyMap) Validate(i interface{}) bool {
_, ok := i.(map[string]interface{})
return ok
}

func (p *propertyMap) String(i any) string {
if validMap, ok := i.(map[string]interface{}); ok {
str, err := json.Marshal(validMap)
if err == nil {
return string(str)
}
}
return ""
}

func (p *propertyMap) JSONSchema() map[string]any {
return map[string]any{
"type": "object",
"additionalProperties": true,
}
}

func (v *Value) ValueMap() (vv map[string]interface{}, ok bool) {
if v == nil {
return
}
vv, ok = v.v.(map[string]interface{})
return
}
69 changes: 69 additions & 0 deletions server/pkg/value/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package value

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_propertyMap_I2V(t *testing.T) {
validMap := map[string]interface{}{"key": "value"}
invalidMap := "not a map"
validJSONString := `{"key": "value"}`
invalidJSONString := `{"key": "value"` // missing closing brace
nilMapPointer := (*map[string]interface{})(nil)
nonNilMapPointer := &validMap

tests := []struct {
name string
arg interface{}
want1 interface{}
want2 bool
}{
{
name: "Valid map",
arg: validMap,
want1: validMap,
want2: true,
},
{
name: "Invalid type",
arg: invalidMap,
want1: nil,
want2: false,
},
{
name: "Valid JSON string",
arg: validJSONString,
want1: validMap, // it should parse the JSON string into a map
want2: true,
},
{
name: "Invalid JSON string",
arg: invalidJSONString,
want1: nil,
want2: false,
},
{
name: "Nil map pointer",
arg: nilMapPointer,
want1: nil,
want2: false,
},
{
name: "Non-nil map pointer",
arg: nonNilMapPointer,
want1: validMap,
want2: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &propertyMap{}
got1, got2 := p.I2V(tt.arg)
assert.Equal(t, tt.want1, got1)
assert.Equal(t, tt.want2, got2)
})
}
}
1 change: 1 addition & 0 deletions server/pkg/value/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var defaultTypes = TypePropertyMap{
TypeRef: &propertyRef{},
TypeString: &propertyString{},
TypeURL: &propertyURL{},
TypeMap: &propertyMap{},
}

func (t Type) Default() bool {
Expand Down
3 changes: 2 additions & 1 deletion server/schemas/plugin_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"polygon",
"rect",
"ref",
"spacing"
"spacing",
"map"
]
},
"propertyPointer": {
Expand Down
34 changes: 17 additions & 17 deletions web/src/services/gql/__gen__/gql.ts

Large diffs are not rendered by default.

55 changes: 28 additions & 27 deletions web/src/services/gql/__gen__/graphql.ts

Large diffs are not rendered by default.

0 comments on commit db515bf

Please sign in to comment.