resolve.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. var resolveTable = make([]byte, 256)
  15. var resolveMap = make(map[string]resolveMapItem)
  16. func init() {
  17. t := resolveTable
  18. t[int('+')] = 'S' // Sign
  19. t[int('-')] = 'S'
  20. for _, c := range "0123456789" {
  21. t[int(c)] = 'D' // Digit
  22. }
  23. for _, c := range "yYnNtTfFoO~" {
  24. t[int(c)] = 'M' // In map
  25. }
  26. t[int('.')] = '.' // Float (potentially in map)
  27. var resolveMapList = []struct {
  28. v interface{}
  29. tag string
  30. l []string
  31. }{
  32. {true, boolTag, []string{"true", "True", "TRUE"}},
  33. {false, boolTag, []string{"false", "False", "FALSE"}},
  34. {nil, nullTag, []string{"", "~", "null", "Null", "NULL"}},
  35. {math.NaN(), floatTag, []string{".nan", ".NaN", ".NAN"}},
  36. {math.Inf(+1), floatTag, []string{".inf", ".Inf", ".INF"}},
  37. {math.Inf(+1), floatTag, []string{"+.inf", "+.Inf", "+.INF"}},
  38. {math.Inf(-1), floatTag, []string{"-.inf", "-.Inf", "-.INF"}},
  39. {"<<", mergeTag, []string{"<<"}},
  40. }
  41. m := resolveMap
  42. for _, item := range resolveMapList {
  43. for _, s := range item.l {
  44. m[s] = resolveMapItem{item.v, item.tag}
  45. }
  46. }
  47. }
  48. const (
  49. nullTag = "!!null"
  50. boolTag = "!!bool"
  51. strTag = "!!str"
  52. intTag = "!!int"
  53. floatTag = "!!float"
  54. timestampTag = "!!timestamp"
  55. seqTag = "!!seq"
  56. mapTag = "!!map"
  57. binaryTag = "!!binary"
  58. mergeTag = "!!merge"
  59. )
  60. var longTags = make(map[string]string)
  61. var shortTags = make(map[string]string)
  62. func init() {
  63. for _, stag := range []string{nullTag, boolTag, strTag, intTag, floatTag, timestampTag, seqTag, mapTag, binaryTag, mergeTag} {
  64. ltag := longTag(stag)
  65. longTags[stag] = ltag
  66. shortTags[ltag] = stag
  67. }
  68. }
  69. const longTagPrefix = "tag:yaml.org,2002:"
  70. func shortTag(tag string) string {
  71. if strings.HasPrefix(tag, longTagPrefix) {
  72. if stag, ok := shortTags[tag]; ok {
  73. return stag
  74. }
  75. return "!!" + tag[len(longTagPrefix):]
  76. }
  77. return tag
  78. }
  79. func longTag(tag string) string {
  80. if strings.HasPrefix(tag, "!!") {
  81. if ltag, ok := longTags[tag]; ok {
  82. return ltag
  83. }
  84. return longTagPrefix + tag[2:]
  85. }
  86. return tag
  87. }
  88. func resolvableTag(tag string) bool {
  89. switch tag {
  90. case "", strTag, boolTag, intTag, floatTag, nullTag, timestampTag:
  91. return true
  92. }
  93. return false
  94. }
  95. var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`)
  96. func resolve(tag string, in string) (rtag string, out interface{}) {
  97. tag = shortTag(tag)
  98. if !resolvableTag(tag) {
  99. return tag, in
  100. }
  101. defer func() {
  102. switch tag {
  103. case "", rtag, strTag, binaryTag:
  104. return
  105. case floatTag:
  106. if rtag == intTag {
  107. switch v := out.(type) {
  108. case int64:
  109. rtag = floatTag
  110. out = float64(v)
  111. return
  112. case int:
  113. rtag = floatTag
  114. out = float64(v)
  115. return
  116. }
  117. }
  118. }
  119. failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
  120. }()
  121. // Any data is accepted as a !!str or !!binary.
  122. // Otherwise, the prefix is enough of a hint about what it might be.
  123. hint := byte('N')
  124. if in != "" {
  125. hint = resolveTable[in[0]]
  126. }
  127. if hint != 0 && tag != strTag && tag != binaryTag {
  128. // Handle things we can lookup in a map.
  129. if item, ok := resolveMap[in]; ok {
  130. return item.tag, item.value
  131. }
  132. // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
  133. // are purposefully unsupported here. They're still quoted on
  134. // the way out for compatibility with other parser, though.
  135. switch hint {
  136. case 'M':
  137. // We've already checked the map above.
  138. case '.':
  139. // Not in the map, so maybe a normal float.
  140. floatv, err := strconv.ParseFloat(in, 64)
  141. if err == nil {
  142. return floatTag, floatv
  143. }
  144. case 'D', 'S':
  145. // Int, float, or timestamp.
  146. // Only try values as a timestamp if the value is unquoted or there's an explicit
  147. // !!timestamp tag.
  148. if tag == "" || tag == timestampTag {
  149. t, ok := parseTimestamp(in)
  150. if ok {
  151. return timestampTag, t
  152. }
  153. }
  154. plain := strings.Replace(in, "_", "", -1)
  155. intv, err := strconv.ParseInt(plain, 0, 64)
  156. if err == nil {
  157. if intv == int64(int(intv)) {
  158. return intTag, int(intv)
  159. } else {
  160. return intTag, intv
  161. }
  162. }
  163. uintv, err := strconv.ParseUint(plain, 0, 64)
  164. if err == nil {
  165. return intTag, uintv
  166. }
  167. if yamlStyleFloat.MatchString(plain) {
  168. floatv, err := strconv.ParseFloat(plain, 64)
  169. if err == nil {
  170. return floatTag, floatv
  171. }
  172. }
  173. if strings.HasPrefix(plain, "0b") {
  174. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  175. if err == nil {
  176. if intv == int64(int(intv)) {
  177. return intTag, int(intv)
  178. } else {
  179. return intTag, intv
  180. }
  181. }
  182. uintv, err := strconv.ParseUint(plain[2:], 2, 64)
  183. if err == nil {
  184. return intTag, uintv
  185. }
  186. } else if strings.HasPrefix(plain, "-0b") {
  187. intv, err := strconv.ParseInt("-"+plain[3:], 2, 64)
  188. if err == nil {
  189. if true || intv == int64(int(intv)) {
  190. return intTag, int(intv)
  191. } else {
  192. return intTag, intv
  193. }
  194. }
  195. }
  196. // Octals as introduced in version 1.2 of the spec.
  197. // Octals from the 1.1 spec, spelled as 0777, are still
  198. // decoded by default in v3 as well for compatibility.
  199. // May be dropped in v4 depending on how usage evolves.
  200. if strings.HasPrefix(plain, "0o") {
  201. intv, err := strconv.ParseInt(plain[2:], 8, 64)
  202. if err == nil {
  203. if intv == int64(int(intv)) {
  204. return intTag, int(intv)
  205. } else {
  206. return intTag, intv
  207. }
  208. }
  209. uintv, err := strconv.ParseUint(plain[2:], 8, 64)
  210. if err == nil {
  211. return intTag, uintv
  212. }
  213. } else if strings.HasPrefix(plain, "-0o") {
  214. intv, err := strconv.ParseInt("-"+plain[3:], 8, 64)
  215. if err == nil {
  216. if true || intv == int64(int(intv)) {
  217. return intTag, int(intv)
  218. } else {
  219. return intTag, intv
  220. }
  221. }
  222. }
  223. default:
  224. panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")")
  225. }
  226. }
  227. return strTag, in
  228. }
  229. // encodeBase64 encodes s as base64 that is broken up into multiple lines
  230. // as appropriate for the resulting length.
  231. func encodeBase64(s string) string {
  232. const lineLen = 70
  233. encLen := base64.StdEncoding.EncodedLen(len(s))
  234. lines := encLen/lineLen + 1
  235. buf := make([]byte, encLen*2+lines)
  236. in := buf[0:encLen]
  237. out := buf[encLen:]
  238. base64.StdEncoding.Encode(in, []byte(s))
  239. k := 0
  240. for i := 0; i < len(in); i += lineLen {
  241. j := i + lineLen
  242. if j > len(in) {
  243. j = len(in)
  244. }
  245. k += copy(out[k:], in[i:j])
  246. if lines > 1 {
  247. out[k] = '\n'
  248. k++
  249. }
  250. }
  251. return string(out[:k])
  252. }
  253. // This is a subset of the formats allowed by the regular expression
  254. // defined at http://yaml.org/type/timestamp.html.
  255. var allowedTimestampFormats = []string{
  256. "2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
  257. "2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t".
  258. "2006-1-2 15:4:5.999999999", // space separated with no time zone
  259. "2006-1-2", // date only
  260. // Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
  261. // from the set of examples.
  262. }
  263. // parseTimestamp parses s as a timestamp string and
  264. // returns the timestamp and reports whether it succeeded.
  265. // Timestamp formats are defined at http://yaml.org/type/timestamp.html
  266. func parseTimestamp(s string) (time.Time, bool) {
  267. // TODO write code to check all the formats supported by
  268. // http://yaml.org/type/timestamp.html instead of using time.Parse.
  269. // Quick check: all date formats start with YYYY-.
  270. i := 0
  271. for ; i < len(s); i++ {
  272. if c := s[i]; c < '0' || c > '9' {
  273. break
  274. }
  275. }
  276. if i != 4 || i == len(s) || s[i] != '-' {
  277. return time.Time{}, false
  278. }
  279. for _, format := range allowedTimestampFormats {
  280. if t, err := time.Parse(format, s); err == nil {
  281. return t, true
  282. }
  283. }
  284. return time.Time{}, false
  285. }