resolve.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. t[int('<')] = '<' // Merge
  26. var resolveMapList = []struct {
  27. v interface{}
  28. tag string
  29. l []string
  30. }{
  31. {true, "!!bool", []string{"y", "Y", "yes", "Yes", "YES"}},
  32. {true, "!!bool", []string{"true", "True", "TRUE"}},
  33. {true, "!!bool", []string{"on", "On", "ON"}},
  34. {false, "!!bool", []string{"n", "N", "no", "No", "NO"}},
  35. {false, "!!bool", []string{"false", "False", "FALSE"}},
  36. {false, "!!bool", []string{"off", "Off", "OFF"}},
  37. {nil, "!!null", []string{"~", "null", "Null", "NULL"}},
  38. {math.NaN(), "!!float", []string{".nan", ".NaN", ".NAN"}},
  39. {math.Inf(+1), "!!float", []string{".inf", ".Inf", ".INF"}},
  40. {math.Inf(+1), "!!float", []string{"+.inf", "+.Inf", "+.INF"}},
  41. {math.Inf(-1), "!!float", []string{"-.inf", "-.Inf", "-.INF"}},
  42. {"<<", "!!merge", []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. if strings.HasPrefix(tag, longTagPrefix) {
  54. return "!!" + tag[len(longTagPrefix):]
  55. }
  56. return tag
  57. }
  58. func resolvableTag(tag string) bool {
  59. switch tag {
  60. case "", "!!str", "!!bool", "!!int", "!!float", "!!null":
  61. return true
  62. }
  63. return false
  64. }
  65. func resolve(tag string, in string) (rtag string, out interface{}) {
  66. tag = shortTag(tag)
  67. if !resolvableTag(tag) {
  68. return tag, in
  69. }
  70. defer func() {
  71. if tag != "" && tag != rtag {
  72. panic("Can't decode " + rtag + " '" + in + "' as a " + tag)
  73. }
  74. }()
  75. if in == "" {
  76. return "!!null", nil
  77. }
  78. c := resolveTable[in[0]]
  79. if c == 0 {
  80. // It's a string for sure. Nothing to do.
  81. return "!!str", in
  82. }
  83. // Handle things we can lookup in a map.
  84. if item, ok := resolveMap[in]; ok {
  85. return item.tag, item.value
  86. }
  87. switch c {
  88. case 'M':
  89. // We've already checked the map above.
  90. case '.':
  91. // Not in the map, so maybe a normal float.
  92. floatv, err := strconv.ParseFloat(in, 64)
  93. if err == nil {
  94. return "!!float", floatv
  95. }
  96. // XXX Handle base 60 floats here (WTF!)
  97. case 'D', 'S':
  98. // Int, float, or timestamp.
  99. plain := strings.Replace(in, "_", "", -1)
  100. intv, err := strconv.ParseInt(plain, 0, 64)
  101. if err == nil {
  102. if intv == int64(int(intv)) {
  103. return "!!int", int(intv)
  104. } else {
  105. return "!!int", intv
  106. }
  107. }
  108. floatv, err := strconv.ParseFloat(plain, 64)
  109. if err == nil {
  110. return "!!float", floatv
  111. }
  112. if strings.HasPrefix(plain, "0b") {
  113. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  114. if err == nil {
  115. return "!!int", int(intv)
  116. }
  117. } else if strings.HasPrefix(plain, "-0b") {
  118. intv, err := strconv.ParseInt(plain[3:], 2, 64)
  119. if err == nil {
  120. return "!!int", -int(intv)
  121. }
  122. }
  123. // XXX Handle timestamps here.
  124. default:
  125. panic("resolveTable item not yet handled: " +
  126. string([]byte{c}) + " (with " + in + ")")
  127. }
  128. return "!!str", in
  129. }