resolve.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package yaml
  2. import (
  3. "encoding/base64"
  4. "math"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type resolveMapItem struct {
  11. value interface{}
  12. tag string
  13. }
  14. // MergeTag is the value used for a merge tag (!!merge "<<").
  15. // When marshaled as a map key, the << will not be quoted,
  16. // which makes it possible to include merge tags explicitly
  17. // when encoding YAML values.
  18. var MergeTag = mergeTag{}
  19. type mergeTag struct{}
  20. var resolveTable = make([]byte, 256)
  21. var resolveMap = make(map[string]resolveMapItem)
  22. func init() {
  23. t := resolveTable
  24. t['+'] = 'S' // Sign
  25. t['-'] = 'S'
  26. for _, c := range "0123456789" {
  27. t[c] = 'D' // Digit
  28. }
  29. for _, c := range "yYnNtTfFoO~<" {
  30. t[c] = 'M' // In resolveMap
  31. }
  32. t['.'] = '.' // Float (potentially in map)
  33. var resolveMapList = []struct {
  34. v interface{}
  35. tag string
  36. l []string
  37. }{
  38. {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
  39. {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
  40. {true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
  41. {false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
  42. {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
  43. {false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
  44. {nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
  45. {math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
  46. {math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
  47. {math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
  48. {math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
  49. {MergeTag, yaml_MERGE_TAG, []string{"<<"}},
  50. }
  51. m := resolveMap
  52. for _, item := range resolveMapList {
  53. for _, s := range item.l {
  54. m[s] = resolveMapItem{item.v, item.tag}
  55. }
  56. }
  57. }
  58. const longTagPrefix = "tag:yaml.org,2002:"
  59. func shortTag(tag string) string {
  60. // TODO This can easily be made faster and produce less garbage.
  61. if strings.HasPrefix(tag, longTagPrefix) {
  62. return "!!" + tag[len(longTagPrefix):]
  63. }
  64. return tag
  65. }
  66. func longTag(tag string) string {
  67. if strings.HasPrefix(tag, "!!") {
  68. return longTagPrefix + tag[2:]
  69. }
  70. return tag
  71. }
  72. func resolvableTag(tag string) bool {
  73. switch tag {
  74. case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG, yaml_MERGE_TAG:
  75. return true
  76. }
  77. return false
  78. }
  79. var yamlStyleFloat = regexp.MustCompile(`^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)?$`)
  80. func resolve(tag string, in string) (rtag string, out interface{}) {
  81. if !resolvableTag(tag) {
  82. return tag, in
  83. }
  84. defer func() {
  85. switch tag {
  86. case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
  87. return
  88. }
  89. failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
  90. }()
  91. // Any data is accepted as a !!str or !!binary.
  92. // Otherwise, the prefix is enough of a hint about what it might be.
  93. hint := byte('N')
  94. if in != "" {
  95. hint = resolveTable[in[0]]
  96. }
  97. if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
  98. // Handle things we can lookup in a map.
  99. if item, ok := resolveMap[in]; ok {
  100. return item.tag, item.value
  101. }
  102. // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
  103. // are purposefully unsupported here. They're still quoted on
  104. // the way out for compatibility with other parser, though.
  105. switch hint {
  106. case 'M':
  107. // We've already checked the map above.
  108. case '.':
  109. // Not in the map, so maybe a normal float.
  110. floatv, err := strconv.ParseFloat(in, 64)
  111. if err == nil {
  112. return yaml_FLOAT_TAG, floatv
  113. }
  114. case 'D', 'S':
  115. // Int, float, or timestamp.
  116. // Only try values as a timestamp if the value is unquoted or there's an explicit
  117. // !!timestamp tag.
  118. if tag == "" || tag == yaml_TIMESTAMP_TAG {
  119. t, ok := parseTimestamp(in)
  120. if ok {
  121. return yaml_TIMESTAMP_TAG, t
  122. }
  123. }
  124. plain := strings.Replace(in, "_", "", -1)
  125. intv, err := strconv.ParseInt(plain, 0, 64)
  126. if err == nil {
  127. if intv == int64(int(intv)) {
  128. return yaml_INT_TAG, int(intv)
  129. } else {
  130. return yaml_INT_TAG, intv
  131. }
  132. }
  133. uintv, err := strconv.ParseUint(plain, 0, 64)
  134. if err == nil {
  135. return yaml_INT_TAG, uintv
  136. }
  137. if yamlStyleFloat.MatchString(plain) {
  138. floatv, err := strconv.ParseFloat(plain, 64)
  139. if err == nil {
  140. return yaml_FLOAT_TAG, floatv
  141. }
  142. }
  143. if strings.HasPrefix(plain, "0b") {
  144. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  145. if err == nil {
  146. if intv == int64(int(intv)) {
  147. return yaml_INT_TAG, int(intv)
  148. } else {
  149. return yaml_INT_TAG, intv
  150. }
  151. }
  152. uintv, err := strconv.ParseUint(plain[2:], 2, 64)
  153. if err == nil {
  154. return yaml_INT_TAG, uintv
  155. }
  156. } else if strings.HasPrefix(plain, "-0b") {
  157. intv, err := strconv.ParseInt(plain[3:], 2, 64)
  158. if err == nil {
  159. if intv == int64(int(intv)) {
  160. return yaml_INT_TAG, -int(intv)
  161. } else {
  162. return yaml_INT_TAG, -intv
  163. }
  164. }
  165. }
  166. default:
  167. panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
  168. }
  169. }
  170. return yaml_STR_TAG, in
  171. }
  172. // encodeBase64 encodes s as base64 that is broken up into multiple lines
  173. // as appropriate for the resulting length.
  174. func encodeBase64(s string) string {
  175. const lineLen = 70
  176. encLen := base64.StdEncoding.EncodedLen(len(s))
  177. lines := encLen/lineLen + 1
  178. buf := make([]byte, encLen*2+lines)
  179. in := buf[0:encLen]
  180. out := buf[encLen:]
  181. base64.StdEncoding.Encode(in, []byte(s))
  182. k := 0
  183. for i := 0; i < len(in); i += lineLen {
  184. j := i + lineLen
  185. if j > len(in) {
  186. j = len(in)
  187. }
  188. k += copy(out[k:], in[i:j])
  189. if lines > 1 {
  190. out[k] = '\n'
  191. k++
  192. }
  193. }
  194. return string(out[:k])
  195. }
  196. // This is a subset of the formats allowed by the regular expression
  197. // defined at http://yaml.org/type/timestamp.html.
  198. var allowedTimestampFormats = []string{
  199. "2006-1-2T15:4:5Z07:00",
  200. "2006-1-2t15:4:5Z07:00", // RFC3339 with lower-case "t".
  201. "2006-1-2 15:4:5", // space separated with no time zone
  202. "2006-1-2", // date only
  203. // Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
  204. // from the set of examples.
  205. }
  206. // parseTimestamp parses s as a timestamp string and
  207. // returns the timestamp and reports whether it succeeded.
  208. // Timestamp formats are defined at http://yaml.org/type/timestamp.html
  209. func parseTimestamp(s string) (time.Time, bool) {
  210. // TODO write code to check all the formats supported by
  211. // http://yaml.org/type/timestamp.html instead of using time.Parse.
  212. // Quick check: all date formats start with YYYY-.
  213. i := 0
  214. for ; i < len(s); i++ {
  215. if c := s[i]; c < '0' || c > '9' {
  216. break
  217. }
  218. }
  219. if i != 4 || i == len(s) || s[i] != '-' {
  220. return time.Time{}, false
  221. }
  222. for _, format := range allowedTimestampFormats {
  223. if t, err := time.Parse(format, s); err == nil {
  224. return t, true
  225. }
  226. }
  227. return time.Time{}, false
  228. }