generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
normalise.go
49 lines (39 loc) 路 867 Bytes
/
normalise.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package schema
import (
"github.com/TBD54566975/ftl/internal/reflect"
)
// Normalise clones and normalises (zeroes) positional information in schema Nodes.
func Normalise[T Node](n T) T {
ni := reflect.DeepCopy(n)
_ = Visit(ni, func(n Node, next func() error) error {
switch n := n.(type) {
case *Bool:
n.Bool = false
case *Float:
n.Float = false
case *Int:
n.Int = false
case *String:
n.Str = false
case *Any:
n.Any = false
case *Unit:
n.Unit = true
case *Time:
n.Time = false
default: // Normally we don't default for sum types, but this is just for tests and will be immediately obvious.
}
return next()
})
return ni //nolint:forcetypeassert
}
func normaliseSlice[T Node](in []T) []T {
if in == nil {
return nil
}
var out []T
for _, n := range in {
out = append(out, Normalise(n))
}
return out
}