-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(server, web): add map valueType to property (#759)
- Loading branch information
Showing
10 changed files
with
182 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ scalar FileSize | |
scalar TranslatedString | ||
scalar Cursor | ||
scalar JSON | ||
scalar Map | ||
|
||
# Meta Type | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,8 @@ | |
"polygon", | ||
"rect", | ||
"ref", | ||
"spacing" | ||
"spacing", | ||
"map" | ||
] | ||
}, | ||
"propertyPointer": { | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.