Skip to content

Commit

Permalink
Add default map value type
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 28, 2024
1 parent c72da7b commit da04c55
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,9 +1090,11 @@ func TestCheck_types(t *testing.T) {
err string
}{
{`unknown`, noerr},
{`[unknown + 42, another_unknown + "foo"]`, noerr},
{`foo.bar.baz > 0`, `invalid operation: > (mismatched types string and int)`},
{`foo.unknown.baz`, `unknown field unknown (1:5)`},
{`foo.bar.unknown`, noerr},
{`foo.bar.unknown + 42`, `invalid operation: + (mismatched types string and int)`},
{`[foo] | map(.unknown)`, `unknown field unknown`},
{`[foo] | map(.bar) | filter(.baz)`, `predicate should return boolean (got string)`},
{`arr | filter(.value > 0)`, `invalid operation: > (mismatched types string and int)`},
Expand Down
28 changes: 17 additions & 11 deletions checker/nature/nature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ var (
)

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

func (n Nature) String() string {
Expand Down Expand Up @@ -54,7 +55,12 @@ func (n Nature) Key() Nature {

func (n Nature) Elem() Nature {
switch n.Kind() {
case reflect.Map, reflect.Ptr:
case reflect.Ptr:
return Nature{Type: n.Type.Elem()}
case reflect.Map:
if n.DefaultMapValue != nil {
return *n.DefaultMapValue
}
return Nature{Type: n.Type.Elem()}
case reflect.Array, reflect.Slice:
if n.ArrayOf != nil {
Expand Down
2 changes: 2 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (m Map) Nature() Nature {
for k, v := range m {
if k == Extra {
nt.Strict = false
natureOfDefaultValue := v.Nature()
nt.DefaultMapValue = &natureOfDefaultValue
continue
}
nt.Fields[k] = v.Nature()
Expand Down

0 comments on commit da04c55

Please sign in to comment.