resolve.go 6.4 KB

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