-
Notifications
You must be signed in to change notification settings - Fork 0
/
named_arg.go
77 lines (69 loc) · 1.7 KB
/
named_arg.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sqlnt
// ArgInfo is the info about a named arg returned from NamedTemplate.GetArgsInfo
type ArgInfo struct {
// Tag is the final arg tag used for the named arg
Tag string
// Positions is the final arg positions for the named arg
Positions []int
// Omissible denotes whether the named arg is omissible
Omissible bool
// DefaultValue is the DefaultValueFunc provider function
DefaultValue DefaultValueFunc
// NullableString denotes whether the named arg is a nullable string
// (i.e. if the supplied value is an empty, then nil is used)
NullableString bool
}
type namedArg struct {
tag string
positions []int
omissible bool
defValue DefaultValueFunc
nullableString bool
}
func (a *namedArg) toInfo() ArgInfo {
return ArgInfo{
Tag: a.tag,
Positions: a.positions,
Omissible: a.omissible,
DefaultValue: a.defValue,
NullableString: a.nullableString,
}
}
func (a *namedArg) copyOptionsTo(r *namedArg) {
r.omissible = a.omissible
r.defValue = a.defValue
r.nullableString = a.nullableString
}
func (a *namedArg) clone() *namedArg {
return &namedArg{
tag: a.tag,
positions: a.positions,
omissible: a.omissible,
defValue: a.defValue,
nullableString: a.nullableString,
}
}
func (a *namedArg) setOmissible(omissible bool) {
if !a.omissible {
// can only be set when not yet omissible
a.omissible = omissible
}
}
func (a *namedArg) defaultedValue(name string) any {
return a.value(a.defValue(name))
}
func (a *namedArg) value(v any) any {
if a.nullableString {
switch vt := v.(type) {
case string:
if vt == "" {
return nil
}
case *string:
if *vt == "" {
return nil
}
}
}
return v
}