Skip to content

Commit 8d3fefc

Browse files
author
Fabrice Vaillant
committed
Favor strings.Cut to strings.SplitN
It is more efficient since it does not allocate an array and just return s if nothing found For the same reason we don't need to use strings.Cut at all if empty
1 parent 8508981 commit 8d3fefc

1 file changed

Lines changed: 59 additions & 59 deletions

File tree

mapstructure.go

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,84 +9,84 @@
99
//
1010
// The simplest function to start with is Decode.
1111
//
12-
// Field Tags
12+
// # Field Tags
1313
//
1414
// When decoding to a struct, mapstructure will use the field name by
1515
// default to perform the mapping. For example, if a struct has a field
1616
// "Username" then mapstructure will look for a key in the source value
1717
// of "username" (case insensitive).
1818
//
19-
// type User struct {
20-
// Username string
21-
// }
19+
// type User struct {
20+
// Username string
21+
// }
2222
//
2323
// You can change the behavior of mapstructure by using struct tags.
2424
// The default struct tag that mapstructure looks for is "mapstructure"
2525
// but you can customize it using DecoderConfig.
2626
//
27-
// Renaming Fields
27+
// # Renaming Fields
2828
//
2929
// To rename the key that mapstructure looks for, use the "mapstructure"
3030
// tag and set a value directly. For example, to change the "username" example
3131
// above to "user":
3232
//
33-
// type User struct {
34-
// Username string `mapstructure:"user"`
35-
// }
33+
// type User struct {
34+
// Username string `mapstructure:"user"`
35+
// }
3636
//
37-
// Embedded Structs and Squashing
37+
// # Embedded Structs and Squashing
3838
//
3939
// Embedded structs are treated as if they're another field with that name.
4040
// By default, the two structs below are equivalent when decoding with
4141
// mapstructure:
4242
//
43-
// type Person struct {
44-
// Name string
45-
// }
43+
// type Person struct {
44+
// Name string
45+
// }
4646
//
47-
// type Friend struct {
48-
// Person
49-
// }
47+
// type Friend struct {
48+
// Person
49+
// }
5050
//
51-
// type Friend struct {
52-
// Person Person
53-
// }
51+
// type Friend struct {
52+
// Person Person
53+
// }
5454
//
5555
// This would require an input that looks like below:
5656
//
57-
// map[string]interface{}{
58-
// "person": map[string]interface{}{"name": "alice"},
59-
// }
57+
// map[string]interface{}{
58+
// "person": map[string]interface{}{"name": "alice"},
59+
// }
6060
//
6161
// If your "person" value is NOT nested, then you can append ",squash" to
6262
// your tag value and mapstructure will treat it as if the embedded struct
6363
// were part of the struct directly. Example:
6464
//
65-
// type Friend struct {
66-
// Person `mapstructure:",squash"`
67-
// }
65+
// type Friend struct {
66+
// Person `mapstructure:",squash"`
67+
// }
6868
//
6969
// Now the following input would be accepted:
7070
//
71-
// map[string]interface{}{
72-
// "name": "alice",
73-
// }
71+
// map[string]interface{}{
72+
// "name": "alice",
73+
// }
7474
//
7575
// When decoding from a struct to a map, the squash tag squashes the struct
7676
// fields into a single map. Using the example structs from above:
7777
//
78-
// Friend{Person: Person{Name: "alice"}}
78+
// Friend{Person: Person{Name: "alice"}}
7979
//
8080
// Will be decoded into a map:
8181
//
82-
// map[string]interface{}{
83-
// "name": "alice",
84-
// }
82+
// map[string]interface{}{
83+
// "name": "alice",
84+
// }
8585
//
8686
// DecoderConfig has a field that changes the behavior of mapstructure
8787
// to always squash embedded structs.
8888
//
89-
// Remainder Values
89+
// # Remainder Values
9090
//
9191
// If there are any unmapped keys in the source value, mapstructure by
9292
// default will silently ignore them. You can error by setting ErrorUnused
@@ -98,20 +98,20 @@
9898
// probably be a "map[string]interface{}" or "map[interface{}]interface{}".
9999
// See example below:
100100
//
101-
// type Friend struct {
102-
// Name string
103-
// Other map[string]interface{} `mapstructure:",remain"`
104-
// }
101+
// type Friend struct {
102+
// Name string
103+
// Other map[string]interface{} `mapstructure:",remain"`
104+
// }
105105
//
106106
// Given the input below, Other would be populated with the other
107107
// values that weren't used (everything but "name"):
108108
//
109-
// map[string]interface{}{
110-
// "name": "bob",
111-
// "address": "123 Maple St.",
112-
// }
109+
// map[string]interface{}{
110+
// "name": "bob",
111+
// "address": "123 Maple St.",
112+
// }
113113
//
114-
// Omit Empty Values
114+
// # Omit Empty Values
115115
//
116116
// When decoding from a struct to any other value, you may use the
117117
// ",omitempty" suffix on your tag to omit that value if it equates to
@@ -122,37 +122,37 @@
122122
// field value is zero and a numeric type, the field is empty, and it won't
123123
// be encoded into the destination type.
124124
//
125-
// type Source struct {
126-
// Age int `mapstructure:",omitempty"`
127-
// }
125+
// type Source struct {
126+
// Age int `mapstructure:",omitempty"`
127+
// }
128128
//
129-
// Unexported fields
129+
// # Unexported fields
130130
//
131131
// Since unexported (private) struct fields cannot be set outside the package
132132
// where they are defined, the decoder will simply skip them.
133133
//
134134
// For this output type definition:
135135
//
136-
// type Exported struct {
137-
// private string // this unexported field will be skipped
138-
// Public string
139-
// }
136+
// type Exported struct {
137+
// private string // this unexported field will be skipped
138+
// Public string
139+
// }
140140
//
141141
// Using this map as input:
142142
//
143-
// map[string]interface{}{
144-
// "private": "I will be ignored",
145-
// "Public": "I made it through!",
146-
// }
143+
// map[string]interface{}{
144+
// "private": "I will be ignored",
145+
// "Public": "I made it through!",
146+
// }
147147
//
148148
// The following struct will be decoded:
149149
//
150-
// type Exported struct {
151-
// private: "" // field is left with an empty string (zero value)
152-
// Public: "I made it through!"
153-
// }
150+
// type Exported struct {
151+
// private: "" // field is left with an empty string (zero value)
152+
// Public: "I made it through!"
153+
// }
154154
//
155-
// Other Configuration
155+
// # Other Configuration
156156
//
157157
// mapstructure is highly configurable. See the DecoderConfig struct
158158
// for other features and options that are supported.
@@ -1358,8 +1358,8 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
13581358
fieldName := field.Name
13591359

13601360
tagValue := field.Tag.Get(d.config.TagName)
1361-
tagValue = strings.SplitN(tagValue, ",", 2)[0]
13621361
if tagValue != "" {
1362+
tagValue, _, _ = strings.Cut(tagValue, ",")
13631363
fieldName = tagValue
13641364
}
13651365

0 commit comments

Comments
 (0)