resolve.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package yaml
  2. import (
  3. "math"
  4. "strconv"
  5. "strings"
  6. )
  7. // TODO: merge, timestamps, base 60 floats, omap.
  8. type resolveMapItem struct {
  9. value interface{}
  10. tag string
  11. }
  12. var resolveTable = make([]byte, 256)
  13. var resolveMap = make(map[string]resolveMapItem)
  14. func init() {
  15. t := resolveTable
  16. t[int('+')] = 'S' // Sign
  17. t[int('-')] = 'S'
  18. for _, c := range "0123456789" {
  19. t[int(c)] = 'D' // Digit
  20. }
  21. for _, c := range "yYnNtTfFoO~" {
  22. t[int(c)] = 'M' // In map
  23. }
  24. t[int('.')] = '.' // Float (potentially in map)
  25. var resolveMapList = []struct {
  26. v interface{}
  27. tag string
  28. l []string
  29. }{
  30. {true, "!!bool", []string{"y", "Y", "yes", "Yes", "YES"}},
  31. {true, "!!bool", []string{"true", "True", "TRUE"}},
  32. {true, "!!bool", []string{"on", "On", "ON"}},
  33. {false, "!!bool", []string{"n", "N", "no", "No", "NO"}},
  34. {false, "!!bool", []string{"false", "False", "FALSE"}},
  35. {false, "!!bool", []string{"off", "Off", "OFF"}},
  36. {nil, "!!null", []string{"~", "null", "Null", "NULL"}},
  37. {math.NaN(), "!!float", []string{".nan", ".NaN", ".NAN"}},
  38. {math.Inf(+1), "!!float", []string{".inf", ".Inf", ".INF"}},
  39. {math.Inf(+1), "!!float", []string{"+.inf", "+.Inf", "+.INF"}},
  40. {math.Inf(-1), "!!float", []string{"-.inf", "-.Inf", "-.INF"}},
  41. {"<<", "!!merge", []string{"<<"}},
  42. }
  43. m := resolveMap
  44. for _, item := range resolveMapList {
  45. for _, s := range item.l {
  46. m[s] = resolveMapItem{item.v, item.tag}
  47. }
  48. }
  49. }
  50. const longTagPrefix = "tag:yaml.org,2002:"
  51. func shortTag(tag string) string {
  52. if strings.HasPrefix(tag, longTagPrefix) {
  53. return "!!" + tag[len(longTagPrefix):]
  54. }
  55. return tag
  56. }
  57. func resolvableTag(tag string) bool {
  58. switch tag {
  59. case "", "!!str", "!!bool", "!!int", "!!float", "!!null":
  60. return true
  61. }
  62. return false
  63. }
  64. func resolve(tag string, in string) (rtag string, out interface{}) {
  65. tag = shortTag(tag)
  66. if !resolvableTag(tag) {
  67. return tag, in
  68. }
  69. defer func() {
  70. if tag != "" && tag != rtag {
  71. panic("Can't decode " + rtag + " '" + in + "' as a " + tag)
  72. }
  73. }()
  74. if in == "" {
  75. return "!!null", nil
  76. }
  77. c := resolveTable[in[0]]
  78. if c == 0 {
  79. // It's a string for sure. Nothing to do.
  80. return "!!str", in
  81. }
  82. // Handle things we can lookup in a map.
  83. if item, ok := resolveMap[in]; ok {
  84. return item.tag, item.value
  85. }
  86. switch c {
  87. case 'M':
  88. // We've already checked the map above.
  89. case '.':
  90. // Not in the map, so maybe a normal float.
  91. floatv, err := strconv.ParseFloat(in, 64)
  92. if err == nil {
  93. return "!!float", floatv
  94. }
  95. // XXX Handle base 60 floats here (WTF!)
  96. case 'D', 'S':
  97. // Int, float, or timestamp.
  98. plain := strings.Replace(in, "_", "", -1)
  99. intv, err := strconv.ParseInt(plain, 0, 64)
  100. if err == nil {
  101. if intv == int64(int(intv)) {
  102. return "!!int", int(intv)
  103. } else {
  104. return "!!int", intv
  105. }
  106. }
  107. floatv, err := strconv.ParseFloat(plain, 64)
  108. if err == nil {
  109. return "!!float", floatv
  110. }
  111. if strings.HasPrefix(plain, "0b") {
  112. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  113. if err == nil {
  114. return "!!int", int(intv)
  115. }
  116. } else if strings.HasPrefix(plain, "-0b") {
  117. intv, err := strconv.ParseInt(plain[3:], 2, 64)
  118. if err == nil {
  119. return "!!int", -int(intv)
  120. }
  121. }
  122. // XXX Handle timestamps here.
  123. default:
  124. panic("resolveTable item not yet handled: " +
  125. string([]byte{c}) + " (with " + in + ")")
  126. }
  127. return "!!str", in
  128. }