resolve.go 3.9 KB

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