Skip to content

Commit da04c55

Browse files
committed
Add default map value type
1 parent c72da7b commit da04c55

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

checker/checker_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,9 +1090,11 @@ func TestCheck_types(t *testing.T) {
10901090
err string
10911091
}{
10921092
{`unknown`, noerr},
1093+
{`[unknown + 42, another_unknown + "foo"]`, noerr},
10931094
{`foo.bar.baz > 0`, `invalid operation: > (mismatched types string and int)`},
10941095
{`foo.unknown.baz`, `unknown field unknown (1:5)`},
10951096
{`foo.bar.unknown`, noerr},
1097+
{`foo.bar.unknown + 42`, `invalid operation: + (mismatched types string and int)`},
10961098
{`[foo] | map(.unknown)`, `unknown field unknown`},
10971099
{`[foo] | map(.bar) | filter(.baz)`, `predicate should return boolean (got string)`},
10981100
{`arr | filter(.value > 0)`, `invalid operation: > (mismatched types string and int)`},

checker/nature/nature.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ var (
1212
)
1313

1414
type Nature struct {
15-
Type reflect.Type // Type of the value. If nil, then value is unknown.
16-
Func *builtin.Function // Used to pass function type from callee to CallNode.
17-
ArrayOf *Nature // Elem nature of array type (usually Type is []any, but ArrayOf can be any nature).
18-
PredicateOut *Nature // Out nature of predicate.
19-
Fields map[string]Nature // Fields of map type.
20-
Strict bool // If map is types.StrictMap.
21-
Nil bool // If value is nil.
22-
Method bool // If value retrieved from method. Usually used to determine amount of in arguments.
23-
MethodIndex int // Index of method in type.
24-
FieldIndex []int // Index of field in type.
15+
Type reflect.Type // Type of the value. If nil, then value is unknown.
16+
Func *builtin.Function // Used to pass function type from callee to CallNode.
17+
ArrayOf *Nature // Elem nature of array type (usually Type is []any, but ArrayOf can be any nature).
18+
PredicateOut *Nature // Out nature of predicate.
19+
Fields map[string]Nature // Fields of map type.
20+
DefaultMapValue *Nature // Default value of map type.
21+
Strict bool // If map is types.StrictMap.
22+
Nil bool // If value is nil.
23+
Method bool // If value retrieved from method. Usually used to determine amount of in arguments.
24+
MethodIndex int // Index of method in type.
25+
FieldIndex []int // Index of field in type.
2526
}
2627

2728
func (n Nature) String() string {
@@ -54,7 +55,12 @@ func (n Nature) Key() Nature {
5455

5556
func (n Nature) Elem() Nature {
5657
switch n.Kind() {
57-
case reflect.Map, reflect.Ptr:
58+
case reflect.Ptr:
59+
return Nature{Type: n.Type.Elem()}
60+
case reflect.Map:
61+
if n.DefaultMapValue != nil {
62+
return *n.DefaultMapValue
63+
}
5864
return Nature{Type: n.Type.Elem()}
5965
case reflect.Array, reflect.Slice:
6066
if n.ArrayOf != nil {

types/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ func (m Map) Nature() Nature {
108108
for k, v := range m {
109109
if k == Extra {
110110
nt.Strict = false
111+
natureOfDefaultValue := v.Nature()
112+
nt.DefaultMapValue = &natureOfDefaultValue
111113
continue
112114
}
113115
nt.Fields[k] = v.Nature()

0 commit comments

Comments
 (0)