Skip to content

Commit beb2f81

Browse files
blackb1rdPrachya Saechua
authored andcommitted
feat: implement SurveyVarDefaultValue for flexible JSON handling in SurveyVar
Signed-off-by: blackb1rd <blackb1rd.mov@gmail.com>
1 parent a5de6d3 commit beb2f81

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

db/Template.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package db
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"fmt"
57
)
68

79
type TemplateType string
@@ -65,6 +67,57 @@ const (
6567
SurveyVarSelect TemplateType = "select"
6668
)
6769

70+
// SurveyVarDefaultValue supports both a single string or an array of strings in JSON.
71+
// It preserves whether the original JSON was an array so encoding will keep the
72+
// original shape when possible (single value -> string, multiple -> array).
73+
type SurveyVarDefaultValue struct {
74+
Values []string `json:"-"`
75+
originalWasArray bool `json:"-"`
76+
}
77+
78+
func (d *SurveyVarDefaultValue) UnmarshalJSON(b []byte) error {
79+
if len(bytes.TrimSpace(b)) == 0 || bytes.Equal(bytes.TrimSpace(b), []byte("null")) {
80+
d.Values = nil
81+
d.originalWasArray = false
82+
return nil
83+
}
84+
85+
// try string
86+
var s string
87+
if err := json.Unmarshal(b, &s); err == nil {
88+
d.Values = []string{s}
89+
d.originalWasArray = false
90+
return nil
91+
}
92+
93+
// try []string
94+
var arr []string
95+
if err := json.Unmarshal(b, &arr); err == nil {
96+
d.Values = arr
97+
d.originalWasArray = true
98+
return nil
99+
}
100+
101+
return fmt.Errorf("invalid default_value: must be string or []string")
102+
}
103+
104+
func (d SurveyVarDefaultValue) MarshalJSON() ([]byte, error) {
105+
if d.Values == nil {
106+
return []byte("null"), nil
107+
}
108+
if len(d.Values) == 1 && !d.originalWasArray {
109+
return json.Marshal(d.Values[0])
110+
}
111+
return json.Marshal(d.Values)
112+
}
113+
114+
func (d SurveyVarDefaultValue) String() string {
115+
if len(d.Values) == 0 {
116+
return ""
117+
}
118+
return d.Values[0]
119+
}
120+
68121
type AnsibleTemplateParams struct {
69122
AllowDebug bool `json:"allow_debug"`
70123
AllowOverrideInventory bool `json:"allow_override_inventory"`
@@ -90,13 +143,13 @@ type SurveyVarEnumValue struct {
90143
}
91144

92145
type SurveyVar struct {
93-
Name string `json:"name" backup:"name"`
94-
Title string `json:"title" backup:"title"`
95-
Required bool `json:"required,omitempty" backup:"required"`
96-
Type SurveyVarType `json:"type,omitempty" backup:"type"`
97-
Description string `json:"description,omitempty" backup:"description"`
98-
Values []SurveyVarEnumValue `json:"values,omitempty" backup:"values"`
99-
DefaultValue string `json:"default_value,omitempty" backup:"default_value"`
146+
Name string `json:"name" backup:"name"`
147+
Title string `json:"title" backup:"title"`
148+
Required bool `json:"required,omitempty" backup:"required"`
149+
Type SurveyVarType `json:"type,omitempty" backup:"type"`
150+
Description string `json:"description,omitempty" backup:"description"`
151+
Values []SurveyVarEnumValue `json:"values,omitempty" backup:"values"`
152+
DefaultValue *SurveyVarDefaultValue `json:"default_value,omitempty" backup:"default_value"`
100153
}
101154

102155
type TemplateFilter struct {

0 commit comments

Comments
 (0)