tags.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2011 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. "strings"
  7. )
  8. // tagOptions is the string following a comma in a struct field's "json"
  9. // tag, or the empty string. It does not include the leading comma.
  10. type tagOptions string
  11. // parseTag splits a struct field's json tag into its name and
  12. // comma-separated options.
  13. func parseTag(tag string) (string, tagOptions) {
  14. if idx := strings.Index(tag, ","); idx != -1 {
  15. return tag[:idx], tagOptions(tag[idx+1:])
  16. }
  17. return tag, tagOptions("")
  18. }
  19. // Contains reports whether a comma-separated list of options
  20. // contains a particular substr flag. substr must be surrounded by a
  21. // string boundary or commas.
  22. func (o tagOptions) Contains(optionName string) bool {
  23. if len(o) == 0 {
  24. return false
  25. }
  26. s := string(o)
  27. for s != "" {
  28. var next string
  29. i := strings.Index(s, ",")
  30. if i >= 0 {
  31. s, next = s[:i], s[i+1:]
  32. }
  33. if s == optionName {
  34. return true
  35. }
  36. s = next
  37. }
  38. return false
  39. }