fields.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package json
  5. import (
  6. "cmp"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "reflect"
  11. "slices"
  12. "strconv"
  13. "strings"
  14. "unicode"
  15. "unicode/utf8"
  16. "github.com/go-json-experiment/json/internal/jsonflags"
  17. "github.com/go-json-experiment/json/internal/jsonwire"
  18. )
  19. type isZeroer interface {
  20. IsZero() bool
  21. }
  22. var isZeroerType = reflect.TypeFor[isZeroer]()
  23. type structFields struct {
  24. flattened []structField // listed in depth-first ordering
  25. byActualName map[string]*structField
  26. byFoldedName map[string][]*structField
  27. inlinedFallback *structField
  28. }
  29. // reindex recomputes index to avoid bounds check during runtime.
  30. //
  31. // During the construction of each [structField] in [makeStructFields],
  32. // the index field is 0-indexed. However, before it returns,
  33. // the 0th field is stored in index0 and index stores the remainder.
  34. func (sf *structFields) reindex() {
  35. reindex := func(f *structField) {
  36. f.index0 = f.index[0]
  37. f.index = f.index[1:]
  38. if len(f.index) == 0 {
  39. f.index = nil // avoid pinning the backing slice
  40. }
  41. }
  42. for i := range sf.flattened {
  43. reindex(&sf.flattened[i])
  44. }
  45. if sf.inlinedFallback != nil {
  46. reindex(sf.inlinedFallback)
  47. }
  48. }
  49. // lookupByFoldedName looks up name by a case-insensitive match
  50. // that also ignores the presence of dashes and underscores.
  51. func (fs *structFields) lookupByFoldedName(name []byte) []*structField {
  52. return fs.byFoldedName[string(foldName(name))]
  53. }
  54. type structField struct {
  55. id int // unique numeric ID in breadth-first ordering
  56. index0 int // 0th index into a struct according to [reflect.Type.FieldByIndex]
  57. index []int // 1st index and remainder according to [reflect.Type.FieldByIndex]
  58. typ reflect.Type
  59. fncs *arshaler
  60. isZero func(addressableValue) bool
  61. isEmpty func(addressableValue) bool
  62. fieldOptions
  63. }
  64. var errNoExportedFields = errors.New("Go struct has no exported fields")
  65. func makeStructFields(root reflect.Type) (fs structFields, serr *SemanticError) {
  66. orErrorf := func(serr *SemanticError, t reflect.Type, f string, a ...any) *SemanticError {
  67. return cmp.Or(serr, &SemanticError{GoType: t, Err: fmt.Errorf(f, a...)})
  68. }
  69. // Setup a queue for a breath-first search.
  70. var queueIndex int
  71. type queueEntry struct {
  72. typ reflect.Type
  73. index []int
  74. visitChildren bool // whether to recursively visit inlined field in this struct
  75. }
  76. queue := []queueEntry{{root, nil, true}}
  77. seen := map[reflect.Type]bool{root: true}
  78. // Perform a breadth-first search over all reachable fields.
  79. // This ensures that len(f.index) will be monotonically increasing.
  80. var allFields, inlinedFallbacks []structField
  81. for queueIndex < len(queue) {
  82. qe := queue[queueIndex]
  83. queueIndex++
  84. t := qe.typ
  85. inlinedFallbackIndex := -1 // index of last inlined fallback field in current struct
  86. namesIndex := make(map[string]int) // index of each field with a given JSON object name in current struct
  87. var hasAnyJSONTag bool // whether any Go struct field has a `json` tag
  88. var hasAnyJSONField bool // whether any JSON serializable fields exist in current struct
  89. for i := range t.NumField() {
  90. sf := t.Field(i)
  91. _, hasTag := sf.Tag.Lookup("json")
  92. hasAnyJSONTag = hasAnyJSONTag || hasTag
  93. options, ignored, err := parseFieldOptions(sf)
  94. if err != nil {
  95. serr = cmp.Or(serr, &SemanticError{GoType: t, Err: err})
  96. }
  97. if ignored {
  98. continue
  99. }
  100. hasAnyJSONField = true
  101. f := structField{
  102. // Allocate a new slice (len=N+1) to hold both
  103. // the parent index (len=N) and the current index (len=1).
  104. // Do this to avoid clobbering the memory of the parent index.
  105. index: append(append(make([]int, 0, len(qe.index)+1), qe.index...), i),
  106. typ: sf.Type,
  107. fieldOptions: options,
  108. }
  109. if sf.Anonymous && !f.hasName {
  110. if indirectType(f.typ).Kind() != reflect.Struct {
  111. serr = orErrorf(serr, t, "embedded Go struct field %s of non-struct type must be explicitly given a JSON name", sf.Name)
  112. } else {
  113. f.inline = true // implied by use of Go embedding without an explicit name
  114. }
  115. }
  116. if f.inline || f.unknown {
  117. // Handle an inlined field that serializes to/from
  118. // zero or more JSON object members.
  119. switch f.fieldOptions {
  120. case fieldOptions{name: f.name, quotedName: f.quotedName, inline: true}:
  121. case fieldOptions{name: f.name, quotedName: f.quotedName, unknown: true}:
  122. case fieldOptions{name: f.name, quotedName: f.quotedName, inline: true, unknown: true}:
  123. serr = orErrorf(serr, t, "Go struct field %s cannot have both `inline` and `unknown` specified", sf.Name)
  124. f.inline = false // let `unknown` take precedence
  125. default:
  126. serr = orErrorf(serr, t, "Go struct field %s cannot have any options other than `inline` or `unknown` specified", sf.Name)
  127. if f.hasName {
  128. continue // invalid inlined field; treat as ignored
  129. }
  130. f.fieldOptions = fieldOptions{name: f.name, quotedName: f.quotedName, inline: f.inline, unknown: f.unknown}
  131. if f.inline && f.unknown {
  132. f.inline = false // let `unknown` take precedence
  133. }
  134. }
  135. // Reject any types with custom serialization otherwise
  136. // it becomes impossible to know what sub-fields to inline.
  137. tf := indirectType(f.typ)
  138. if implementsAny(tf, allMethodTypes...) && tf != jsontextValueType {
  139. serr = orErrorf(serr, t, "inlined Go struct field %s of type %s must not implement marshal or unmarshal methods", sf.Name, tf)
  140. }
  141. // Handle an inlined field that serializes to/from
  142. // a finite number of JSON object members backed by a Go struct.
  143. if tf.Kind() == reflect.Struct {
  144. if f.unknown {
  145. serr = orErrorf(serr, t, "inlined Go struct field %s of type %s with `unknown` tag must be a Go map of string key or a jsontext.Value", sf.Name, tf)
  146. continue // invalid inlined field; treat as ignored
  147. }
  148. if qe.visitChildren {
  149. queue = append(queue, queueEntry{tf, f.index, !seen[tf]})
  150. }
  151. seen[tf] = true
  152. continue
  153. } else if !sf.IsExported() {
  154. serr = orErrorf(serr, t, "inlined Go struct field %s is not exported", sf.Name)
  155. continue // invalid inlined field; treat as ignored
  156. }
  157. // Handle an inlined field that serializes to/from any number of
  158. // JSON object members back by a Go map or jsontext.Value.
  159. switch {
  160. case tf == jsontextValueType:
  161. f.fncs = nil // specially handled in arshal_inlined.go
  162. case tf.Kind() == reflect.Map && tf.Key().Kind() == reflect.String:
  163. if implementsAny(tf.Key(), allMethodTypes...) {
  164. serr = orErrorf(serr, t, "inlined map field %s of type %s must have a string key that does not implement marshal or unmarshal methods", sf.Name, tf)
  165. continue // invalid inlined field; treat as ignored
  166. }
  167. f.fncs = lookupArshaler(tf.Elem())
  168. default:
  169. serr = orErrorf(serr, t, "inlined Go struct field %s of type %s must be a Go struct, Go map of string key, or jsontext.Value", sf.Name, tf)
  170. continue // invalid inlined field; treat as ignored
  171. }
  172. // Reject multiple inlined fallback fields within the same struct.
  173. if inlinedFallbackIndex >= 0 {
  174. serr = orErrorf(serr, t, "inlined Go struct fields %s and %s cannot both be a Go map or jsontext.Value", t.Field(inlinedFallbackIndex).Name, sf.Name)
  175. // Still append f to inlinedFallbacks as there is still a
  176. // check for a dominant inlined fallback before returning.
  177. }
  178. inlinedFallbackIndex = i
  179. inlinedFallbacks = append(inlinedFallbacks, f)
  180. } else {
  181. // Handle normal Go struct field that serializes to/from
  182. // a single JSON object member.
  183. // Unexported fields cannot be serialized except for
  184. // embedded fields of a struct type,
  185. // which might promote exported fields of their own.
  186. if !sf.IsExported() {
  187. tf := indirectType(f.typ)
  188. if !(sf.Anonymous && tf.Kind() == reflect.Struct) {
  189. serr = orErrorf(serr, t, "Go struct field %s is not exported", sf.Name)
  190. continue
  191. }
  192. // Unfortunately, methods on the unexported field
  193. // still cannot be called.
  194. if implementsAny(tf, allMethodTypes...) ||
  195. (f.omitzero && implementsAny(tf, isZeroerType)) {
  196. serr = orErrorf(serr, t, "Go struct field %s is not exported for method calls", sf.Name)
  197. continue
  198. }
  199. }
  200. // Provide a function that uses a type's IsZero method.
  201. switch {
  202. case sf.Type.Kind() == reflect.Interface && sf.Type.Implements(isZeroerType):
  203. f.isZero = func(va addressableValue) bool {
  204. // Avoid panics calling IsZero on a nil interface or
  205. // non-nil interface with nil pointer.
  206. return va.IsNil() || (va.Elem().Kind() == reflect.Pointer && va.Elem().IsNil()) || va.Interface().(isZeroer).IsZero()
  207. }
  208. case sf.Type.Kind() == reflect.Pointer && sf.Type.Implements(isZeroerType):
  209. f.isZero = func(va addressableValue) bool {
  210. // Avoid panics calling IsZero on nil pointer.
  211. return va.IsNil() || va.Interface().(isZeroer).IsZero()
  212. }
  213. case sf.Type.Implements(isZeroerType):
  214. f.isZero = func(va addressableValue) bool { return va.Interface().(isZeroer).IsZero() }
  215. case reflect.PointerTo(sf.Type).Implements(isZeroerType):
  216. f.isZero = func(va addressableValue) bool { return va.Addr().Interface().(isZeroer).IsZero() }
  217. }
  218. // Provide a function that can determine whether the value would
  219. // serialize as an empty JSON value.
  220. switch sf.Type.Kind() {
  221. case reflect.String, reflect.Map, reflect.Array, reflect.Slice:
  222. f.isEmpty = func(va addressableValue) bool { return va.Len() == 0 }
  223. case reflect.Pointer, reflect.Interface:
  224. f.isEmpty = func(va addressableValue) bool { return va.IsNil() }
  225. }
  226. // Reject multiple fields with same name within the same struct.
  227. if j, ok := namesIndex[f.name]; ok {
  228. serr = orErrorf(serr, t, "Go struct fields %s and %s conflict over JSON object name %q", t.Field(j).Name, sf.Name, f.name)
  229. // Still append f to allFields as there is still a
  230. // check for a dominant field before returning.
  231. }
  232. namesIndex[f.name] = i
  233. f.id = len(allFields)
  234. f.fncs = lookupArshaler(sf.Type)
  235. allFields = append(allFields, f)
  236. }
  237. }
  238. // NOTE: New users to the json package are occasionally surprised that
  239. // unexported fields are ignored. This occurs by necessity due to our
  240. // inability to directly introspect such fields with Go reflection
  241. // without the use of unsafe.
  242. //
  243. // To reduce friction here, refuse to serialize any Go struct that
  244. // has no JSON serializable fields, has at least one Go struct field,
  245. // and does not have any `json` tags present. For example,
  246. // errors returned by errors.New would fail to serialize.
  247. isEmptyStruct := t.NumField() == 0
  248. if !isEmptyStruct && !hasAnyJSONTag && !hasAnyJSONField {
  249. serr = cmp.Or(serr, &SemanticError{GoType: t, Err: errNoExportedFields})
  250. }
  251. }
  252. // Sort the fields by exact name (breaking ties by depth and
  253. // then by presence of an explicitly provided JSON name).
  254. // Select the dominant field from each set of fields with the same name.
  255. // If multiple fields have the same name, then the dominant field
  256. // is the one that exists alone at the shallowest depth,
  257. // or the one that is uniquely tagged with a JSON name.
  258. // Otherwise, no dominant field exists for the set.
  259. flattened := allFields[:0]
  260. slices.SortStableFunc(allFields, func(x, y structField) int {
  261. return cmp.Or(
  262. strings.Compare(x.name, y.name),
  263. cmp.Compare(len(x.index), len(y.index)),
  264. boolsCompare(!x.hasName, !y.hasName))
  265. })
  266. for len(allFields) > 0 {
  267. n := 1 // number of fields with the same exact name
  268. for n < len(allFields) && allFields[n-1].name == allFields[n].name {
  269. n++
  270. }
  271. if n == 1 || len(allFields[0].index) != len(allFields[1].index) || allFields[0].hasName != allFields[1].hasName {
  272. flattened = append(flattened, allFields[0]) // only keep field if there is a dominant field
  273. }
  274. allFields = allFields[n:]
  275. }
  276. // Sort the fields according to a breadth-first ordering
  277. // so that we can re-number IDs with the smallest possible values.
  278. // This optimizes use of uintSet such that it fits in the 64-entry bit set.
  279. slices.SortFunc(flattened, func(x, y structField) int {
  280. return cmp.Compare(x.id, y.id)
  281. })
  282. for i := range flattened {
  283. flattened[i].id = i
  284. }
  285. // Sort the fields according to a depth-first ordering
  286. // as the typical order that fields are marshaled.
  287. slices.SortFunc(flattened, func(x, y structField) int {
  288. return slices.Compare(x.index, y.index)
  289. })
  290. // Compute the mapping of fields in the byActualName map.
  291. // Pre-fold all names so that we can lookup folded names quickly.
  292. fs = structFields{
  293. flattened: flattened,
  294. byActualName: make(map[string]*structField, len(flattened)),
  295. byFoldedName: make(map[string][]*structField, len(flattened)),
  296. }
  297. for i, f := range fs.flattened {
  298. foldedName := string(foldName([]byte(f.name)))
  299. fs.byActualName[f.name] = &fs.flattened[i]
  300. fs.byFoldedName[foldedName] = append(fs.byFoldedName[foldedName], &fs.flattened[i])
  301. }
  302. for foldedName, fields := range fs.byFoldedName {
  303. if len(fields) > 1 {
  304. // The precedence order for conflicting ignoreCase names
  305. // is by breadth-first order, rather than depth-first order.
  306. slices.SortFunc(fields, func(x, y *structField) int {
  307. return cmp.Compare(x.id, y.id)
  308. })
  309. fs.byFoldedName[foldedName] = fields
  310. }
  311. }
  312. if n := len(inlinedFallbacks); n == 1 || (n > 1 && len(inlinedFallbacks[0].index) != len(inlinedFallbacks[1].index)) {
  313. fs.inlinedFallback = &inlinedFallbacks[0] // dominant inlined fallback field
  314. }
  315. fs.reindex()
  316. return fs, serr
  317. }
  318. // indirectType unwraps one level of pointer indirection
  319. // similar to how Go only allows embedding either T or *T,
  320. // but not **T or P (which is a named pointer).
  321. func indirectType(t reflect.Type) reflect.Type {
  322. if t.Kind() == reflect.Pointer && t.Name() == "" {
  323. t = t.Elem()
  324. }
  325. return t
  326. }
  327. // matchFoldedName matches a case-insensitive name depending on the options.
  328. // It assumes that foldName(f.name) == foldName(name).
  329. //
  330. // Case-insensitive matching is used if the `case:ignore` tag option is specified
  331. // or the MatchCaseInsensitiveNames call option is specified
  332. // (and the `case:strict` tag option is not specified).
  333. // Functionally, the `case:ignore` and `case:strict` tag options take precedence.
  334. //
  335. // The v1 definition of case-insensitivity operated under strings.EqualFold
  336. // and would strictly compare dashes and underscores,
  337. // while the v2 definition would ignore the presence of dashes and underscores.
  338. // Thus, if the MatchCaseSensitiveDelimiter call option is specified,
  339. // the match is further restricted to using strings.EqualFold.
  340. func (f *structField) matchFoldedName(name []byte, flags *jsonflags.Flags) bool {
  341. if f.casing == caseIgnore || (flags.Get(jsonflags.MatchCaseInsensitiveNames) && f.casing != caseStrict) {
  342. if !flags.Get(jsonflags.MatchCaseSensitiveDelimiter) || strings.EqualFold(string(name), f.name) {
  343. return true
  344. }
  345. }
  346. return false
  347. }
  348. const (
  349. caseIgnore = 1
  350. caseStrict = 2
  351. )
  352. type fieldOptions struct {
  353. name string
  354. quotedName string // quoted name per RFC 8785, section 3.2.2.2.
  355. hasName bool
  356. nameNeedEscape bool
  357. casing int8 // either 0, caseIgnore, or caseStrict
  358. inline bool
  359. unknown bool
  360. omitzero bool
  361. omitempty bool
  362. string bool
  363. format string
  364. }
  365. // parseFieldOptions parses the `json` tag in a Go struct field as
  366. // a structured set of options configuring parameters such as
  367. // the JSON member name and other features.
  368. func parseFieldOptions(sf reflect.StructField) (out fieldOptions, ignored bool, err error) {
  369. tag, hasTag := sf.Tag.Lookup("json")
  370. // Check whether this field is explicitly ignored.
  371. if tag == "-" {
  372. return fieldOptions{}, true, nil
  373. }
  374. // Check whether this field is unexported and not embedded,
  375. // which Go reflection cannot mutate for the sake of serialization.
  376. //
  377. // An embedded field of an unexported type is still capable of
  378. // forwarding exported fields, which may be JSON serialized.
  379. // This technically operates on the edge of what is permissible by
  380. // the Go language, but the most recent decision is to permit this.
  381. //
  382. // See https://go.dev/issue/24153 and https://go.dev/issue/32772.
  383. if !sf.IsExported() && !sf.Anonymous {
  384. // Tag options specified on an unexported field suggests user error.
  385. if hasTag {
  386. err = cmp.Or(err, fmt.Errorf("unexported Go struct field %s cannot have non-ignored `json:%q` tag", sf.Name, tag))
  387. }
  388. return fieldOptions{}, true, err
  389. }
  390. // Determine the JSON member name for this Go field. A user-specified name
  391. // may be provided as either an identifier or a single-quoted string.
  392. // The single-quoted string allows arbitrary characters in the name.
  393. // See https://go.dev/issue/2718 and https://go.dev/issue/3546.
  394. out.name = sf.Name // always starts with an uppercase character
  395. if len(tag) > 0 && !strings.HasPrefix(tag, ",") {
  396. // For better compatibility with v1, accept almost any unescaped name.
  397. n := len(tag) - len(strings.TrimLeftFunc(tag, func(r rune) bool {
  398. return !strings.ContainsRune(",\\'\"`", r) // reserve comma, backslash, and quotes
  399. }))
  400. name := tag[:n]
  401. // If the next character is not a comma, then the name is either
  402. // malformed (if n > 0) or a single-quoted name.
  403. // In either case, call consumeTagOption to handle it further.
  404. var err2 error
  405. if !strings.HasPrefix(tag[n:], ",") && len(name) != len(tag) {
  406. name, n, err2 = consumeTagOption(tag)
  407. if err2 != nil {
  408. err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed `json` tag: %v", sf.Name, err2))
  409. }
  410. }
  411. if !utf8.ValidString(name) {
  412. err = cmp.Or(err, fmt.Errorf("Go struct field %s has JSON object name %q with invalid UTF-8", sf.Name, name))
  413. name = string([]rune(name)) // replace invalid UTF-8 with utf8.RuneError
  414. }
  415. if err2 == nil {
  416. out.hasName = true
  417. out.name = name
  418. }
  419. tag = tag[n:]
  420. }
  421. b, _ := jsonwire.AppendQuote(nil, out.name, &jsonflags.Flags{})
  422. out.quotedName = string(b)
  423. out.nameNeedEscape = jsonwire.NeedEscape(out.name)
  424. // Handle any additional tag options (if any).
  425. var wasFormat bool
  426. seenOpts := make(map[string]bool)
  427. for len(tag) > 0 {
  428. // Consume comma delimiter.
  429. if tag[0] != ',' {
  430. err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed `json` tag: invalid character %q before next option (expecting ',')", sf.Name, tag[0]))
  431. } else {
  432. tag = tag[len(","):]
  433. if len(tag) == 0 {
  434. err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed `json` tag: invalid trailing ',' character", sf.Name))
  435. break
  436. }
  437. }
  438. // Consume and process the tag option.
  439. opt, n, err2 := consumeTagOption(tag)
  440. if err2 != nil {
  441. err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed `json` tag: %v", sf.Name, err2))
  442. }
  443. rawOpt := tag[:n]
  444. tag = tag[n:]
  445. switch {
  446. case wasFormat:
  447. err = cmp.Or(err, fmt.Errorf("Go struct field %s has `format` tag option that was not specified last", sf.Name))
  448. case strings.HasPrefix(rawOpt, "'") && strings.TrimFunc(opt, isLetterOrDigit) == "":
  449. err = cmp.Or(err, fmt.Errorf("Go struct field %s has unnecessarily quoted appearance of `%s` tag option; specify `%s` instead", sf.Name, rawOpt, opt))
  450. }
  451. switch opt {
  452. case "case":
  453. if !strings.HasPrefix(tag, ":") {
  454. err = cmp.Or(err, fmt.Errorf("Go struct field %s is missing value for `case` tag option; specify `case:ignore` or `case:strict` instead", sf.Name))
  455. break
  456. }
  457. tag = tag[len(":"):]
  458. opt, n, err2 := consumeTagOption(tag)
  459. if err2 != nil {
  460. err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed value for `case` tag option: %v", sf.Name, err2))
  461. break
  462. }
  463. rawOpt := tag[:n]
  464. tag = tag[n:]
  465. if strings.HasPrefix(rawOpt, "'") {
  466. err = cmp.Or(err, fmt.Errorf("Go struct field %s has unnecessarily quoted appearance of `case:%s` tag option; specify `case:%s` instead", sf.Name, rawOpt, opt))
  467. }
  468. switch opt {
  469. case "ignore":
  470. out.casing |= caseIgnore
  471. case "strict":
  472. out.casing |= caseStrict
  473. default:
  474. err = cmp.Or(err, fmt.Errorf("Go struct field %s has unknown `case:%s` tag value", sf.Name, rawOpt))
  475. }
  476. case "inline":
  477. out.inline = true
  478. case "unknown":
  479. out.unknown = true
  480. case "omitzero":
  481. out.omitzero = true
  482. case "omitempty":
  483. out.omitempty = true
  484. case "string":
  485. out.string = true
  486. case "format":
  487. if !strings.HasPrefix(tag, ":") {
  488. err = cmp.Or(err, fmt.Errorf("Go struct field %s is missing value for `format` tag option", sf.Name))
  489. break
  490. }
  491. tag = tag[len(":"):]
  492. opt, n, err2 := consumeTagOption(tag)
  493. if err2 != nil {
  494. err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed value for `format` tag option: %v", sf.Name, err2))
  495. break
  496. }
  497. tag = tag[n:]
  498. out.format = opt
  499. wasFormat = true
  500. default:
  501. // Reject keys that resemble one of the supported options.
  502. // This catches invalid mutants such as "omitEmpty" or "omit_empty".
  503. normOpt := strings.ReplaceAll(strings.ToLower(opt), "_", "")
  504. switch normOpt {
  505. case "case", "inline", "unknown", "omitzero", "omitempty", "string", "format":
  506. err = cmp.Or(err, fmt.Errorf("Go struct field %s has invalid appearance of `%s` tag option; specify `%s` instead", sf.Name, opt, normOpt))
  507. }
  508. // NOTE: Everything else is ignored. This does not mean it is
  509. // forward compatible to insert arbitrary tag options since
  510. // a future version of this package may understand that tag.
  511. }
  512. // Reject duplicates.
  513. switch {
  514. case out.casing == caseIgnore|caseStrict:
  515. err = cmp.Or(err, fmt.Errorf("Go struct field %s cannot have both `case:ignore` and `case:strict` tag options", sf.Name))
  516. case seenOpts[opt]:
  517. err = cmp.Or(err, fmt.Errorf("Go struct field %s has duplicate appearance of `%s` tag option", sf.Name, rawOpt))
  518. }
  519. seenOpts[opt] = true
  520. }
  521. return out, false, err
  522. }
  523. // consumeTagOption consumes the next option,
  524. // which is either a Go identifier or a single-quoted string.
  525. // If the next option is invalid, it returns all of in until the next comma,
  526. // and reports an error.
  527. func consumeTagOption(in string) (string, int, error) {
  528. // For legacy compatibility with v1, assume options are comma-separated.
  529. i := strings.IndexByte(in, ',')
  530. if i < 0 {
  531. i = len(in)
  532. }
  533. switch r, _ := utf8.DecodeRuneInString(in); {
  534. // Option as a Go identifier.
  535. case r == '_' || unicode.IsLetter(r):
  536. n := len(in) - len(strings.TrimLeftFunc(in, isLetterOrDigit))
  537. return in[:n], n, nil
  538. // Option as a single-quoted string.
  539. case r == '\'':
  540. // The grammar is nearly identical to a double-quoted Go string literal,
  541. // but uses single quotes as the terminators. The reason for a custom
  542. // grammar is because both backtick and double quotes cannot be used
  543. // verbatim in a struct tag.
  544. //
  545. // Convert a single-quoted string to a double-quote string and rely on
  546. // strconv.Unquote to handle the rest.
  547. var inEscape bool
  548. b := []byte{'"'}
  549. n := len(`'`)
  550. for len(in) > n {
  551. r, rn := utf8.DecodeRuneInString(in[n:])
  552. switch {
  553. case inEscape:
  554. if r == '\'' {
  555. b = b[:len(b)-1] // remove escape character: `\'` => `'`
  556. }
  557. inEscape = false
  558. case r == '\\':
  559. inEscape = true
  560. case r == '"':
  561. b = append(b, '\\') // insert escape character: `"` => `\"`
  562. case r == '\'':
  563. b = append(b, '"')
  564. n += len(`'`)
  565. out, err := strconv.Unquote(string(b))
  566. if err != nil {
  567. return in[:i], i, fmt.Errorf("invalid single-quoted string: %s", in[:n])
  568. }
  569. return out, n, nil
  570. }
  571. b = append(b, in[n:][:rn]...)
  572. n += rn
  573. }
  574. if n > 10 {
  575. n = 10 // limit the amount of context printed in the error
  576. }
  577. return in[:i], i, fmt.Errorf("single-quoted string not terminated: %s...", in[:n])
  578. case len(in) == 0:
  579. return in[:i], i, io.ErrUnexpectedEOF
  580. default:
  581. return in[:i], i, fmt.Errorf("invalid character %q at start of option (expecting Unicode letter or single quote)", r)
  582. }
  583. }
  584. func isLetterOrDigit(r rune) bool {
  585. return r == '_' || unicode.IsLetter(r) || unicode.IsNumber(r)
  586. }
  587. // boolsCompare compares x and y, ordering false before true.
  588. func boolsCompare(x, y bool) int {
  589. switch {
  590. case !x && y:
  591. return -1
  592. default:
  593. return 0
  594. case x && !y:
  595. return +1
  596. }
  597. }