resolve.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package yaml
  2. import (
  3. "encoding/base64"
  4. "math"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "unicode/utf8"
  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:
  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. plain := strings.Replace(in, "_", "", -1)
  111. intv, err := strconv.ParseInt(plain, 0, 64)
  112. if err == nil {
  113. if intv == int64(int(intv)) {
  114. return yaml_INT_TAG, int(intv)
  115. } else {
  116. return yaml_INT_TAG, intv
  117. }
  118. }
  119. uintv, err := strconv.ParseUint(plain, 0, 64)
  120. if err == nil {
  121. return yaml_INT_TAG, uintv
  122. }
  123. if yamlStyleFloat.MatchString(plain) {
  124. floatv, err := strconv.ParseFloat(plain, 64)
  125. if err == nil {
  126. return yaml_FLOAT_TAG, floatv
  127. }
  128. }
  129. if strings.HasPrefix(plain, "0b") {
  130. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  131. if err == nil {
  132. if intv == int64(int(intv)) {
  133. return yaml_INT_TAG, int(intv)
  134. } else {
  135. return yaml_INT_TAG, intv
  136. }
  137. }
  138. uintv, err := strconv.ParseUint(plain[2:], 2, 64)
  139. if err == nil {
  140. return yaml_INT_TAG, uintv
  141. }
  142. } else if strings.HasPrefix(plain, "-0b") {
  143. intv, err := strconv.ParseInt(plain[3:], 2, 64)
  144. if err == nil {
  145. if intv == int64(int(intv)) {
  146. return yaml_INT_TAG, -int(intv)
  147. } else {
  148. return yaml_INT_TAG, -intv
  149. }
  150. }
  151. }
  152. // XXX Handle timestamps here.
  153. default:
  154. panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
  155. }
  156. }
  157. if tag == yaml_BINARY_TAG {
  158. return yaml_BINARY_TAG, in
  159. }
  160. if utf8.ValidString(in) {
  161. return yaml_STR_TAG, in
  162. }
  163. return yaml_BINARY_TAG, encodeBase64(in)
  164. }
  165. // encodeBase64 encodes s as base64 that is broken up into multiple lines
  166. // as appropriate for the resulting length.
  167. func encodeBase64(s string) string {
  168. const lineLen = 70
  169. encLen := base64.StdEncoding.EncodedLen(len(s))
  170. lines := encLen/lineLen + 1
  171. buf := make([]byte, encLen*2+lines)
  172. in := buf[0:encLen]
  173. out := buf[encLen:]
  174. base64.StdEncoding.Encode(in, []byte(s))
  175. k := 0
  176. for i := 0; i < len(in); i += lineLen {
  177. j := i + lineLen
  178. if j > len(in) {
  179. j = len(in)
  180. }
  181. k += copy(out[k:], in[i:j])
  182. if lines > 1 {
  183. out[k] = '\n'
  184. k++
  185. }
  186. }
  187. return string(out[:k])
  188. }