resolve.go 6.4 KB

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