Skip to content

Commit 7a68898

Browse files
authored
feat(encoding/ghtml): add parameter validation for function SpecialCharsMapOrStruct (#3841)
1 parent a72a9ff commit 7a68898

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

encoding/ghtml/ghtml.go

+39-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"reflect"
1313
"strings"
1414

15+
"github.com/gogf/gf/v2/errors/gcode"
16+
"github.com/gogf/gf/v2/errors/gerror"
1517
strip "github.com/grokify/html-strip-tags-go"
1618
)
1719

@@ -60,15 +62,26 @@ func SpecialCharsDecode(s string) string {
6062
}
6163

6264
// SpecialCharsMapOrStruct automatically encodes string values/attributes for map/struct.
65+
//
66+
// Note that, if operation on struct, the given parameter `mapOrStruct` should be type of pointer to struct.
67+
//
68+
// For example:
69+
// var m = map{}
70+
// var s = struct{}{}
71+
// OK: SpecialCharsMapOrStruct(m)
72+
// OK: SpecialCharsMapOrStruct(&s)
73+
// Error: SpecialCharsMapOrStruct(s)
6374
func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
6475
var (
6576
reflectValue = reflect.ValueOf(mapOrStruct)
6677
reflectKind = reflectValue.Kind()
78+
originalKind = reflectKind
6779
)
6880
for reflectValue.IsValid() && (reflectKind == reflect.Ptr || reflectKind == reflect.Interface) {
6981
reflectValue = reflectValue.Elem()
7082
reflectKind = reflectValue.Kind()
7183
}
84+
7285
switch reflectKind {
7386
case reflect.Map:
7487
var (
@@ -82,22 +95,43 @@ func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
8295
reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.String())))
8396
case reflect.Interface:
8497
if mapValue.Elem().Kind() == reflect.String {
85-
reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.Elem().String())))
98+
reflectValue.SetMapIndex(
99+
key,
100+
reflect.ValueOf(SpecialChars(mapValue.Elem().String())),
101+
)
86102
}
103+
default:
87104
}
88105
}
89106

90107
case reflect.Struct:
91-
var (
92-
fieldValue reflect.Value
93-
)
108+
if originalKind != reflect.Ptr {
109+
return gerror.NewCodef(
110+
gcode.CodeInvalidParameter,
111+
`invalid input parameter type "%s", should be type of pointer to struct`,
112+
reflect.TypeOf(mapOrStruct).String(),
113+
)
114+
}
115+
var fieldValue reflect.Value
94116
for i := 0; i < reflectValue.NumField(); i++ {
95117
fieldValue = reflectValue.Field(i)
96118
switch fieldValue.Kind() {
97119
case reflect.String:
98-
fieldValue.Set(reflect.ValueOf(SpecialChars(fieldValue.String())))
120+
fieldValue.Set(
121+
reflect.ValueOf(
122+
SpecialChars(fieldValue.String()),
123+
),
124+
)
125+
default:
99126
}
100127
}
128+
129+
default:
130+
return gerror.NewCodef(
131+
gcode.CodeInvalidParameter,
132+
`invalid input parameter type "%s"`,
133+
reflect.TypeOf(mapOrStruct).String(),
134+
)
101135
}
102136
return nil
103137
}

encoding/ghtml/ghtml_z_unit_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@ func Test_SpecialCharsMapOrStruct_Struct(t *testing.T) {
7878
t.Assert(a.Title, `&lt;h1&gt;T&lt;/h1&gt;`)
7979
t.Assert(a.Content, `&lt;div&gt;C&lt;/div&gt;`)
8080
})
81+
82+
// should error
83+
gtest.C(t, func(t *gtest.T) {
84+
a := A{
85+
Title: "<h1>T</h1>",
86+
Content: "<div>C</div>",
87+
}
88+
err := ghtml.SpecialCharsMapOrStruct(a)
89+
t.AssertNE(err, nil)
90+
})
8191
}

0 commit comments

Comments
 (0)