-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_types.go
49 lines (45 loc) · 1.43 KB
/
check_types.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 models
import (
"fmt"
"github.com/specgen-io/specgen/v2/goven/spec"
)
func equalsExpression(typ *spec.TypeDef, left string, right string) string {
return _equalsExpression(typ, left, right, true)
}
func _equalsExpression(typ *spec.TypeDef, left string, right string, canBePrimitiveType bool) string {
switch typ.Node {
case spec.PlainType:
return equalsExpressionPlainType(typ.Plain, left, right, canBePrimitiveType)
case spec.NullableType:
return _equalsExpression(typ.Child, left, right, false)
case spec.ArrayType, spec.MapType:
return fmt.Sprintf(`Objects.equals(%s, that.%s)`, left, right)
default:
panic(fmt.Sprintf("Unknown type: %v", typ))
}
}
func equalsExpressionPlainType(typ string, left string, right string, canBePrimitiveType bool) string {
switch typ {
case spec.TypeInt32,
spec.TypeInt64,
spec.TypeBoolean:
if canBePrimitiveType {
return fmt.Sprintf(`%s == that.%s`, left, right)
} else {
return fmt.Sprintf(`Objects.equals(%s, that.%s)`, left, right)
}
case spec.TypeFloat:
return fmt.Sprintf(`Float.compare(that.%s, %s) == 0`, left, right)
case spec.TypeDouble:
return fmt.Sprintf(`Double.compare(that.%s, %s) == 0`, left, right)
case spec.TypeDecimal,
spec.TypeString,
spec.TypeUuid,
spec.TypeDate,
spec.TypeDateTime,
spec.TypeJson:
return fmt.Sprintf(`Objects.equals(%s, that.%s)`, left, right)
default:
return fmt.Sprintf(`Objects.equals(%s, that.%s)`, left, right)
}
}