convert.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package attl
  2. //Converter is an interface that Values can optionally implement
  3. // to allow conversion to other arbitrary types at run time.
  4. type Converter interface {
  5. Convert(to interface{}) *Error
  6. }
  7. func (from Int) Convert(to interface{}) *Error {
  8. switch toPtr := to.(type) {
  9. case *string:
  10. (*toPtr) = from.String()
  11. case *int8:
  12. (*toPtr) = int8(from)
  13. case *int16:
  14. (*toPtr) = int16(from)
  15. case *int32:
  16. (*toPtr) = int32(from)
  17. case *int64:
  18. (*toPtr) = int64(from)
  19. case *int:
  20. (*toPtr) = int(from)
  21. case *bool:
  22. (*toPtr) = (from != 0)
  23. case *Bool:
  24. (*toPtr) = (from != 0)
  25. case *float32:
  26. (*toPtr) = float32(from)
  27. case *float64:
  28. (*toPtr) = float64(from)
  29. case *Int:
  30. (*toPtr) = from
  31. case *Value:
  32. (*toPtr) = from
  33. default:
  34. return ErrorFromString("Cannot convert Int value")
  35. }
  36. return nil
  37. }
  38. func (from Bool) Convert(to interface{}) *Error {
  39. iVal := 0
  40. if from {
  41. iVal = -1
  42. }
  43. switch toPtr := to.(type) {
  44. case *string:
  45. (*toPtr) = from.String()
  46. case *int8:
  47. (*toPtr) = int8(iVal)
  48. case *int16:
  49. (*toPtr) = int16(iVal)
  50. case *int32:
  51. (*toPtr) = int32(iVal)
  52. case *int64:
  53. (*toPtr) = int64(iVal)
  54. case *int:
  55. (*toPtr) = int(iVal)
  56. case *bool:
  57. (*toPtr) = bool(from)
  58. case *Bool:
  59. (*toPtr) = from
  60. case *float32:
  61. (*toPtr) = float32(iVal)
  62. case *float64:
  63. (*toPtr) = float64(iVal)
  64. case *Int:
  65. (*toPtr) = Int(iVal)
  66. case *Value:
  67. (*toPtr) = from
  68. default:
  69. return ErrorFromString("Cannot convert Int value")
  70. }
  71. return nil
  72. }
  73. func (from Word) Convert(to interface{}) *Error {
  74. switch toPtr := to.(type) {
  75. case *string:
  76. (*toPtr) = from.String()
  77. case *bool:
  78. (*toPtr) = (from.String() != "")
  79. case *Bool:
  80. (*toPtr) = (from.String() != "")
  81. case *Word:
  82. (*toPtr) = from
  83. case *Type:
  84. (*toPtr) = Type(string(from))
  85. case *Value:
  86. (*toPtr) = from
  87. default:
  88. return ErrorFromString("Cannot convert Word value")
  89. }
  90. return nil
  91. }
  92. func (from Type) Convert(to interface{}) *Error {
  93. switch toPtr := to.(type) {
  94. case *string:
  95. (*toPtr) = from.String()
  96. case *bool:
  97. (*toPtr) = (from.String() != "")
  98. case *Bool:
  99. (*toPtr) = (from.String() != "")
  100. case *Type:
  101. (*toPtr) = from
  102. case *Word:
  103. (*toPtr) = Word(string(from))
  104. case *Value:
  105. (*toPtr) = from
  106. default:
  107. return ErrorFromString("Cannot convert Word value")
  108. }
  109. return nil
  110. }
  111. func (from String) Convert(to interface{}) *Error {
  112. switch toPtr := to.(type) {
  113. case *string:
  114. (*toPtr) = from.String()
  115. case *bool:
  116. (*toPtr) = (from.String() != "")
  117. case *Bool:
  118. (*toPtr) = (from.String() != "")
  119. case **Error:
  120. (*toPtr) = ErrorFromString(from.String())
  121. case *String:
  122. (*toPtr) = from
  123. case *Value:
  124. (*toPtr) = from
  125. default:
  126. return ErrorFromString("Cannot convert String value")
  127. }
  128. return nil
  129. }
  130. func (from *Error) Convert(to interface{}) *Error {
  131. switch toPtr := to.(type) {
  132. case *string:
  133. (*toPtr) = from.String()
  134. case *bool:
  135. (*toPtr) = (from == nil)
  136. case *Bool:
  137. (*toPtr) = (from == nil)
  138. case *error:
  139. (*toPtr) = from
  140. case **Error:
  141. (*toPtr) = from
  142. case *Value:
  143. (*toPtr) = from
  144. default:
  145. return ErrorFromString("Cannot convert Error value")
  146. }
  147. return nil
  148. }
  149. func (from Block) Convert(to interface{}) *Error {
  150. switch toPtr := to.(type) {
  151. case *bool:
  152. (*toPtr) = (len(from.Statements) > 0)
  153. case *Bool:
  154. (*toPtr) = (len(from.Statements) > 0)
  155. case *Block:
  156. (*toPtr) = from
  157. case *Value:
  158. (*toPtr) = from
  159. default:
  160. return ErrorFromString("Cannot convert block value")
  161. }
  162. return nil
  163. }
  164. func (from Map) Convert(to interface{}) *Error {
  165. switch toPtr := to.(type) {
  166. case *bool:
  167. (*toPtr) = (len(from) > 0)
  168. case *Bool:
  169. (*toPtr) = (len(from) > 0)
  170. case *Map:
  171. (*toPtr) = from
  172. case *Value:
  173. (*toPtr) = from
  174. default:
  175. return ErrorFromString("Cannot convert map value")
  176. }
  177. return nil
  178. }
  179. func (from List) Convert(to interface{}) *Error {
  180. switch toPtr := to.(type) {
  181. case *bool:
  182. (*toPtr) = (len(from) > 0)
  183. case *Bool:
  184. (*toPtr) = (len(from) > 0)
  185. case *List:
  186. (*toPtr) = from
  187. case *Value:
  188. (*toPtr) = from
  189. default:
  190. return ErrorFromString("Cannot convert map value")
  191. }
  192. return nil
  193. }
  194. func ValToBool(val Value) bool {
  195. if val == nil {
  196. return false
  197. }
  198. switch check := val.(type) {
  199. case *Error:
  200. return check == nil
  201. case Int:
  202. return (int(check) != 0)
  203. case Bool:
  204. return bool(check)
  205. default:
  206. return val.String() != ""
  207. }
  208. }
  209. func Convert(val Value, to interface{}) *Error {
  210. if converter, ok := val.(Converter); ok {
  211. return converter.Convert(to)
  212. } else if pval, ok := to.(*Value); ok {
  213. *pval = val
  214. return nil
  215. }
  216. return ErrorFromString("Value cannot be converted")
  217. }
  218. // StringList makes a List from string arguments
  219. func StringList(sa ...string) List {
  220. list := List{}
  221. for _, s := range sa {
  222. list = append(list, String(s))
  223. }
  224. return list
  225. }
  226. // Converts a list to raw strings
  227. func (l List) ToStrings() []string {
  228. res := []string{}
  229. for _, s := range l {
  230. res = append(res, s.String())
  231. }
  232. return res
  233. }