Skip to content

Commit c72da7b

Browse files
committed
Drop types.StrictMap and add types.Extra to define additional keys
1 parent 5e660e7 commit c72da7b

File tree

5 files changed

+18
-64
lines changed

5 files changed

+18
-64
lines changed

checker/checker_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,14 +1072,16 @@ func TestCheck_builtin_without_call(t *testing.T) {
10721072

10731073
func TestCheck_types(t *testing.T) {
10741074
env := types.Map{
1075-
"foo": types.StrictMap{
1075+
"foo": types.Map{
10761076
"bar": types.Map{
1077-
"baz": types.String,
1077+
"baz": types.String,
1078+
types.Extra: types.String,
10781079
},
10791080
},
1080-
"arr": types.Array(types.StrictMap{
1081+
"arr": types.Array(types.Map{
10811082
"value": types.String,
10821083
}),
1084+
types.Extra: types.Any,
10831085
}
10841086

10851087
noerr := "no error"
@@ -1102,7 +1104,8 @@ func TestCheck_types(t *testing.T) {
11021104
tree, err := parser.Parse(test.code)
11031105
require.NoError(t, err)
11041106

1105-
_, err = checker.Check(tree, conf.New(env))
1107+
config := conf.New(env)
1108+
_, err = checker.Check(tree, config)
11061109
if test.err == noerr {
11071110
require.NoError(t, err)
11081111
} else {

conf/env.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ func Env(env any) Nature {
2020
switch env := env.(type) {
2121
case types.Map:
2222
return env.Nature()
23-
24-
case types.StrictMap:
25-
return env.Nature()
2623
}
2724

2825
v := reflect.ValueOf(env)
@@ -53,9 +50,6 @@ func Env(env any) Nature {
5350
case types.Map:
5451
n.Fields[key.String()] = face.(types.Map).Nature()
5552

56-
case types.StrictMap:
57-
n.Fields[key.String()] = face.(types.StrictMap).Nature()
58-
5953
default:
6054
if face == nil {
6155
n.Fields[key.String()] = Nature{Nil: true}

expr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,7 +2704,7 @@ func TestExpr_nil_op_str(t *testing.T) {
27042704

27052705
func TestExpr_env_types_map(t *testing.T) {
27062706
envTypes := types.Map{
2707-
"foo": types.StrictMap{
2707+
"foo": types.Map{
27082708
"bar": types.String,
27092709
},
27102710
}
@@ -2725,7 +2725,7 @@ func TestExpr_env_types_map(t *testing.T) {
27252725

27262726
func TestExpr_env_types_map_error(t *testing.T) {
27272727
envTypes := types.Map{
2728-
"foo": types.StrictMap{
2728+
"foo": types.Map{
27292729
"bar": types.String,
27302730
},
27312731
}

types/types.go

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,22 @@ func (r rtype) String() string {
9494
return r.t.String()
9595
}
9696

97-
// Map returns a type that represents a map of the given type.
98-
// The map is not strict, meaning that it can contain keys not defined in the map.
97+
// Map represents a map[string]any type with defined keys.
9998
type Map map[string]Type
10099

100+
const Extra = "[[__extra_keys__]]"
101+
101102
func (m Map) Nature() Nature {
102103
nt := Nature{
103104
Type: reflect.TypeOf(map[string]any{}),
104105
Fields: make(map[string]Nature, len(m)),
106+
Strict: true,
105107
}
106108
for k, v := range m {
109+
if k == Extra {
110+
nt.Strict = false
111+
continue
112+
}
107113
nt.Fields[k] = v.Nature()
108114
}
109115
return nt
@@ -136,49 +142,6 @@ func (m Map) String() string {
136142
return fmt.Sprintf("Map{%s}", strings.Join(pairs, ", "))
137143
}
138144

139-
// StrictMap returns a type that represents a map of the given type.
140-
// The map is strict, meaning that it can only contain keys defined in the map.
141-
type StrictMap map[string]Type
142-
143-
func (m StrictMap) Nature() Nature {
144-
nt := Nature{
145-
Type: reflect.TypeOf(map[string]any{}),
146-
Fields: make(map[string]Nature, len(m)),
147-
Strict: true,
148-
}
149-
for k, v := range m {
150-
nt.Fields[k] = v.Nature()
151-
}
152-
return nt
153-
}
154-
155-
func (m StrictMap) Equal(t Type) bool {
156-
if t == Any {
157-
return true
158-
}
159-
mt, ok := t.(StrictMap)
160-
if !ok {
161-
return false
162-
}
163-
if len(m) != len(mt) {
164-
return false
165-
}
166-
for k, v := range m {
167-
if !v.Equal(mt[k]) {
168-
return false
169-
}
170-
}
171-
return true
172-
}
173-
174-
func (m StrictMap) String() string {
175-
pairs := make([]string, 0, len(m))
176-
for k, v := range m {
177-
pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
178-
}
179-
return fmt.Sprintf("StrictMap{%s}", strings.Join(pairs, ", "))
180-
}
181-
182145
// Array returns a type that represents an array of the given type.
183146
func Array(of Type) Type {
184147
return array{of}

types/types_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestType_Equal(t *testing.T) {
1111
tests := []struct {
12-
index string
12+
index string // Index added for IDEA to show green test marker per test.
1313
a, b Type
1414
want bool
1515
}{
@@ -22,24 +22,18 @@ func TestType_Equal(t *testing.T) {
2222
{"7", Int, Nil, false},
2323
{"8", Int, Array(Int), false},
2424
{"9", Int, Map{"foo": Int}, false},
25-
{"10", Int, StrictMap{"foo": Int}, false},
2625
{"11", Int, Array(Int), false},
2726
{"12", Array(Int), Array(Int), true},
2827
{"13", Array(Int), Array(Float), false},
2928
{"14", Map{"foo": Int}, Map{"foo": Int}, true},
3029
{"15", Map{"foo": Int}, Map{"foo": Float}, false},
31-
{"16", Map{"foo": Int}, StrictMap{"foo": Int}, false},
32-
{"17", StrictMap{"foo": Int}, StrictMap{"foo": Int}, true},
33-
{"18", StrictMap{"foo": Int}, StrictMap{"foo": Float}, false},
3430
{"19", Map{"foo": Map{"bar": Int}}, Map{"foo": Map{"bar": Int}}, true},
3531
{"20", Map{"foo": Map{"bar": Int}}, Map{"foo": Map{"bar": Float}}, false},
3632
{"21", Any, Any, true},
3733
{"22", Any, Int, true},
3834
{"23", Int, Any, true},
3935
{"24", Any, Map{"foo": Int}, true},
4036
{"25", Map{"foo": Int}, Any, true},
41-
{"26", Any, StrictMap{"foo": Int}, true},
42-
{"27", StrictMap{"foo": Int}, Any, true},
4337
{"28", Any, Array(Int), true},
4438
{"29", Array(Int), Any, true},
4539
}

0 commit comments

Comments
 (0)