Skip to content

Commit ddddfb4

Browse files
committed
fix: set value back to nil, if it doesn't match the regex
1 parent 37461d4 commit ddddfb4

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

templates/go/model_oneof.mustache

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,12 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
6666
regex := `{{.}}`
6767
regex = regexp.MustCompile("^\\/|\\/$").ReplaceAllString(regex, "$1") // Remove beginning slash and ending slash
6868
regex = regexp.MustCompile("\\\\(.)").ReplaceAllString(regex, "$1") // Remove duplicate escaping char for dots
69-
rawString := strings.Trim(*dst.String, "\"")
69+
rawString := regexp.MustCompile(`^"|"$`).ReplaceAllString(*dst.String, "$1") // Remove quotes
70+
isMatched, _ := regexp.MatchString(regex, rawString)
7071
{{/pattern}}
71-
if string(json{{{.}}}) == "{}" { // empty struct
72+
if string(json{{{.}}}) == "{}" {{#pattern}}|| !isMatched {{/pattern}} { // empty struct
7273
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
73-
{{#pattern}}
74-
} else if matched, _ := regexp.MatchString(regex, rawString); matched {
75-
{{/pattern}}
76-
{{^pattern}}
7774
} else {
78-
{{/pattern}}
7975
match++
8076
}
8177
} else {

0 commit comments

Comments
 (0)