resolve.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package yaml
  2. import (
  3. "encoding/base64"
  4. "math"
  5. "strconv"
  6. "strings"
  7. "unicode/utf8"
  8. )
  9. type resolveMapItem struct {
  10. value interface{}
  11. tag string
  12. }
  13. var resolveTable = make([]byte, 256)
  14. var resolveMap = make(map[string]resolveMapItem)
  15. func init() {
  16. t := resolveTable
  17. t[int('+')] = 'S' // Sign
  18. t[int('-')] = 'S'
  19. for _, c := range "0123456789" {
  20. t[int(c)] = 'D' // Digit
  21. }
  22. for _, c := range "yYnNtTfFoO~" {
  23. t[int(c)] = 'M' // In map
  24. }
  25. t[int('.')] = '.' // Float (potentially in map)
  26. var resolveMapList = []struct {
  27. v interface{}
  28. tag string
  29. l []string
  30. }{
  31. {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
  32. {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
  33. {true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
  34. {false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
  35. {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
  36. {false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
  37. {nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
  38. {math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
  39. {math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
  40. {math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
  41. {math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
  42. {"<<", yaml_MERGE_TAG, []string{"<<"}},
  43. }
  44. m := resolveMap
  45. for _, item := range resolveMapList {
  46. for _, s := range item.l {
  47. m[s] = resolveMapItem{item.v, item.tag}
  48. }
  49. }
  50. }
  51. const longTagPrefix = "tag:yaml.org,2002:"
  52. func shortTag(tag string) string {
  53. // TODO This can easily be made faster and produce less garbage.
  54. if strings.HasPrefix(tag, longTagPrefix) {
  55. return "!!" + tag[len(longTagPrefix):]
  56. }
  57. return tag
  58. }
  59. func longTag(tag string) string {
  60. if strings.HasPrefix(tag, "!!") {
  61. return longTagPrefix + tag[2:]
  62. }
  63. return tag
  64. }
  65. func resolvableTag(tag string) bool {
  66. switch tag {
  67. case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG:
  68. return true
  69. }
  70. return false
  71. }
  72. func resolve(tag string, in string) (rtag string, out interface{}) {
  73. if !resolvableTag(tag) {
  74. return tag, in
  75. }
  76. defer func() {
  77. switch tag {
  78. case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
  79. return
  80. }
  81. failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
  82. }()
  83. // Any data is accepted as a !!str or !!binary.
  84. // Otherwise, the prefix is enough of a hint about what it might be.
  85. hint := byte('N')
  86. if in != "" {
  87. hint = resolveTable[in[0]]
  88. }
  89. if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
  90. // Handle things we can lookup in a map.
  91. if item, ok := resolveMap[in]; ok {
  92. return item.tag, item.value
  93. }
  94. // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
  95. // are purposefully unsupported here. They're still quoted on
  96. // the way out for compatibility with other parser, though.
  97. switch hint {
  98. case 'M':
  99. // We've already checked the map above.
  100. case '.':
  101. // Not in the map, so maybe a normal float.
  102. floatv, err := strconv.ParseFloat(in, 64)
  103. if err == nil {
  104. return yaml_FLOAT_TAG, floatv
  105. }
  106. case 'D', 'S':
  107. // Int, float, or timestamp.
  108. plain := strings.Replace(in, "_", "", -1)
  109. intv, err := strconv.ParseInt(plain, 0, 64)
  110. if err == nil {
  111. if intv == int64(int(intv)) {
  112. return yaml_INT_TAG, int(intv)
  113. } else {
  114. return yaml_INT_TAG, intv
  115. }
  116. }
  117. uintv, err := strconv.ParseUint(plain, 0, 64)
  118. if err == nil {
  119. return yaml_INT_TAG, uintv
  120. }
  121. floatv, err := strconv.ParseFloat(plain, 64)
  122. if err == nil {
  123. return yaml_FLOAT_TAG, floatv
  124. }
  125. if strings.HasPrefix(plain, "0b") {
  126. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  127. if err == nil {
  128. if intv == int64(int(intv)) {
  129. return yaml_INT_TAG, int(intv)
  130. } else {
  131. return yaml_INT_TAG, intv
  132. }
  133. }
  134. uintv, err := strconv.ParseUint(plain[2:], 2, 64)
  135. if err == nil {
  136. return yaml_INT_TAG, uintv
  137. }
  138. } else if strings.HasPrefix(plain, "-0b") {
  139. intv, err := strconv.ParseInt(plain[3:], 2, 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. }
  148. // XXX Handle timestamps here.
  149. default:
  150. panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
  151. }
  152. }
  153. if tag == yaml_BINARY_TAG {
  154. return yaml_BINARY_TAG, in
  155. }
  156. if utf8.ValidString(in) {
  157. return yaml_STR_TAG, in
  158. }
  159. return yaml_BINARY_TAG, encodeBase64(in)
  160. }
  161. // encodeBase64 encodes s as base64 that is broken up into multiple lines
  162. // as appropriate for the resulting length.
  163. func encodeBase64(s string) string {
  164. const lineLen = 70
  165. encLen := base64.StdEncoding.EncodedLen(len(s))
  166. lines := encLen/lineLen + 1
  167. buf := make([]byte, encLen*2+lines)
  168. in := buf[0:encLen]
  169. out := buf[encLen:]
  170. base64.StdEncoding.Encode(in, []byte(s))
  171. k := 0
  172. for i := 0; i < len(in); i += lineLen {
  173. j := i + lineLen
  174. if j > len(in) {
  175. j = len(in)
  176. }
  177. k += copy(out[k:], in[i:j])
  178. if lines > 1 {
  179. out[k] = '\n'
  180. k++
  181. }
  182. }
  183. return string(out[:k])
  184. }