resolve.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package yaml
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "unicode/utf8"
  9. )
  10. // TODO: merge, timestamps, base 60 floats, omap.
  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:
  70. return true
  71. }
  72. return false
  73. }
  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. fail(fmt.Sprintf("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. floatv, err := strconv.ParseFloat(plain, 64)
  120. if err == nil {
  121. return yaml_FLOAT_TAG, floatv
  122. }
  123. if strings.HasPrefix(plain, "0b") {
  124. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  125. if err == nil {
  126. return yaml_INT_TAG, int(intv)
  127. }
  128. } else if strings.HasPrefix(plain, "-0b") {
  129. intv, err := strconv.ParseInt(plain[3:], 2, 64)
  130. if err == nil {
  131. return yaml_INT_TAG, -int(intv)
  132. }
  133. }
  134. // XXX Handle timestamps here.
  135. default:
  136. panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
  137. }
  138. }
  139. if tag == yaml_BINARY_TAG {
  140. return yaml_BINARY_TAG, in
  141. }
  142. if utf8.ValidString(in) {
  143. return yaml_STR_TAG, in
  144. }
  145. return yaml_BINARY_TAG, encodeBase64(in)
  146. }
  147. // encodeBase64 encodes s as base64 that is broken up into multiple lines
  148. // as appropriate for the resulting length.
  149. func encodeBase64(s string) string {
  150. const lineLen = 70
  151. encLen := base64.StdEncoding.EncodedLen(len(s))
  152. lines := encLen/lineLen + 1
  153. buf := make([]byte, encLen*2+lines)
  154. in := buf[0:encLen]
  155. out := buf[encLen:]
  156. base64.StdEncoding.Encode(in, []byte(s))
  157. k := 0
  158. for i := 0; i < len(in); i += lineLen {
  159. j := i + lineLen
  160. if j > len(in) {
  161. j = len(in)
  162. }
  163. k += copy(out[k:], in[i:j])
  164. if lines > 1 {
  165. out[k] = '\n'
  166. k++
  167. }
  168. }
  169. return string(out[:k])
  170. }