resolve.go 6.5 KB

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