value.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package muesli
  2. import "fmt"
  3. /* Run time values */
  4. type Value interface {
  5. String() string
  6. Type() TypeValue
  7. // Convert the value to a normal go value which must be passed
  8. // in as a pointer to which the value mmust be set,
  9. // or return an error if the concversion is not possible.
  10. Convert(to interface{}) error
  11. }
  12. type IntValue int64
  13. type FloatValue float64
  14. type StringValue string
  15. type BoolValue bool
  16. type WordValue string
  17. type TypeValue string
  18. type ErrorValue struct {
  19. error
  20. }
  21. type EmptyValue struct {
  22. }
  23. type ListValue struct {
  24. List []Value
  25. }
  26. // Values is simply a shorthand alias for []Value
  27. type Values = []Value
  28. const (
  29. TrueValue = BoolValue(true)
  30. FalseValue = BoolValue(false)
  31. IntTypeValue = TypeValue("Int")
  32. FloatTypeValue = TypeValue("Float")
  33. StringTypeValue = TypeValue("String")
  34. BoolTypeValue = TypeValue("Bool")
  35. WordTypeValue = TypeValue("Word")
  36. ErrorTypeValue = TypeValue("Error")
  37. TypeTypeValue = TypeValue("Type")
  38. EmptyTypeValue = TypeValue("Empty")
  39. ListTypeValue = TypeValue("List")
  40. AnyTypeValue = TypeValue("Any")
  41. ZeroTypeValue = TypeValue("")
  42. )
  43. var NilValue = Value(nil)
  44. func (val IntValue) String() string {
  45. return fmt.Sprintf("%d", int64(val))
  46. }
  47. func (val FloatValue) String() string {
  48. return fmt.Sprintf("%f", float64(val))
  49. }
  50. func (val BoolValue) String() string {
  51. if bool(val) {
  52. return "true"
  53. } else {
  54. return "false"
  55. }
  56. }
  57. func (val StringValue) String() string {
  58. return string(val)
  59. }
  60. func (val WordValue) String() string {
  61. return string(val)
  62. }
  63. func (val TypeValue) String() string {
  64. return string(val)
  65. }
  66. func (val ErrorValue) String() string {
  67. return fmt.Sprintf("%s", val.Error())
  68. }
  69. func (val EmptyValue) String() string {
  70. return "<empty>"
  71. }
  72. func (val ListValue) String() string {
  73. res := "["
  74. sep := ""
  75. for _, elt := range val.List {
  76. res = res + sep + elt.String()
  77. sep = ", "
  78. }
  79. res += "]"
  80. return res
  81. }
  82. func (v IntValue) Type() TypeValue { return IntTypeValue }
  83. func (v FloatValue) Type() TypeValue { return FloatTypeValue }
  84. func (v StringValue) Type() TypeValue { return StringTypeValue }
  85. func (v BoolValue) Type() TypeValue { return BoolTypeValue }
  86. func (v WordValue) Type() TypeValue { return WordTypeValue }
  87. func (v TypeValue) Type() TypeValue { return TypeTypeValue }
  88. func (v ErrorValue) Type() TypeValue { return ErrorTypeValue }
  89. func (v EmptyValue) Type() TypeValue { return EmptyTypeValue }
  90. func (v ListValue) Type() TypeValue { return ListTypeValue }
  91. func NewErrorValuef(format string, args ...interface{}) ErrorValue {
  92. err := fmt.Errorf(format, args...)
  93. return ErrorValue{err}
  94. }
  95. func NewListValue(elements ...Value) ListValue {
  96. return ListValue{elements}
  97. }
  98. func (list *ListValue) Append(elements ...Value) {
  99. list.List = append(list.List, elements...)
  100. }
  101. func (list *ListValue) AppendList(toAppend ListValue) {
  102. list.List = append(list.List, toAppend.List...)
  103. }
  104. func (list ListValue) Length() int {
  105. return len(list.List)
  106. }
  107. func (list *ListValue) Index(i int) Value {
  108. if i >= len(list.List) {
  109. return NilValue
  110. }
  111. return list.List[i]
  112. }
  113. func (list *ListValue) First() Value {
  114. return list.Index(0)
  115. }
  116. func (list *ListValue) Last() Value {
  117. return list.Index(list.Length()-1)
  118. }
  119. func EmptyListValue() ListValue {
  120. return ListValue{make([]Value, 0)}
  121. }
  122. func EmptyValueArray() []Value {
  123. return make([]Value, 0)
  124. }
  125. func NewValueArray(elements ...Value) []Value {
  126. return elements
  127. }
  128. func (from IntValue) Convert(to interface{}) error {
  129. switch toPtr := to.(type) {
  130. case *string:
  131. (*toPtr) = from.String()
  132. case *int8:
  133. (*toPtr) = int8(from)
  134. case *int16:
  135. (*toPtr) = int16(from)
  136. case *int32:
  137. (*toPtr) = int32(from)
  138. case *int64:
  139. (*toPtr) = int64(from)
  140. case *int:
  141. (*toPtr) = int(from)
  142. case *bool:
  143. (*toPtr) = (from != 0)
  144. case *float32:
  145. (*toPtr) = float32(from)
  146. case *float64:
  147. (*toPtr) = float64(from)
  148. case *IntValue:
  149. (*toPtr) = from
  150. default:
  151. return NewErrorValuef("Cannot convert IntValue value %v to %v", from, to)
  152. }
  153. return nil
  154. }
  155. func (from FloatValue) Convert(to interface{}) error {
  156. switch toPtr := to.(type) {
  157. case *string:
  158. (*toPtr) = from.String()
  159. case *int8:
  160. (*toPtr) = int8(from)
  161. case *int16:
  162. (*toPtr) = int16(from)
  163. case *int32:
  164. (*toPtr) = int32(from)
  165. case *int64:
  166. (*toPtr) = int64(from)
  167. case *int:
  168. (*toPtr) = int(from)
  169. case *bool:
  170. (*toPtr) = (from != 0)
  171. case *float32:
  172. (*toPtr) = float32(from)
  173. case *float64:
  174. (*toPtr) = float64(from)
  175. case *FloatValue:
  176. (*toPtr) = from
  177. default:
  178. return NewErrorValuef("Cannot convert FloatValue value %v to %v", from, to)
  179. }
  180. return nil
  181. }
  182. func (from StringValue) Convert(to interface{}) error {
  183. switch toPtr := to.(type) {
  184. case *string:
  185. (*toPtr) = from.String()
  186. case *StringValue:
  187. (*toPtr) = from
  188. default:
  189. return NewErrorValuef("Cannot convert StringValue %v to %v", from, to)
  190. }
  191. return nil
  192. }
  193. func (from WordValue) Convert(to interface{}) error {
  194. switch toPtr := to.(type) {
  195. case *string:
  196. (*toPtr) = from.String()
  197. case *WordValue:
  198. (*toPtr) = from
  199. default:
  200. return NewErrorValuef("Cannot convert WordValue %v to %v", from, to)
  201. }
  202. return nil
  203. }
  204. func (from TypeValue) Convert(to interface{}) error {
  205. switch toPtr := to.(type) {
  206. case *string:
  207. (*toPtr) = from.String()
  208. case *TypeValue:
  209. (*toPtr) = from
  210. default:
  211. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  212. }
  213. return nil
  214. }
  215. func (from BoolValue) Convert(to interface{}) error {
  216. switch toPtr := to.(type) {
  217. case *bool:
  218. (*toPtr) = bool(from)
  219. case *BoolValue:
  220. (*toPtr) = from
  221. default:
  222. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  223. }
  224. return nil
  225. }
  226. func (from ErrorValue) Convert(to interface{}) error {
  227. switch toPtr := to.(type) {
  228. case *string:
  229. (*toPtr) = from.String()
  230. case *error:
  231. (*toPtr) = from.error
  232. case *ErrorValue:
  233. (*toPtr) = from
  234. default:
  235. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  236. }
  237. return nil
  238. }
  239. func (from EmptyValue) Convert(to interface{}) error {
  240. return NewErrorValuef("Cannot convert the empty value %v to %v", from, to)
  241. }
  242. func (from ListValue) Convert(to interface{}) error {
  243. switch toPtr := to.(type) {
  244. case *[]Value:
  245. (*toPtr) = from.List
  246. default:
  247. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  248. }
  249. return nil
  250. }
  251. /* Helpers to easily convert Muesli values to "normal" Go values. */
  252. func From(from Value, to interface{}) error {
  253. return from.Convert(to)
  254. }
  255. /* Helpers to easily convert Muesli value lists to "normal" Go values.
  256. * The returned array are the remaining unparsed arguments. */
  257. func ParseArgs(args []Value, to...interface{}) ([]Value, error) {
  258. if len(to) > len(args) {
  259. return nil, NewErrorValuef("Too few arguments, expected %d, got %d", len(to), len(args))
  260. }
  261. i:= 0
  262. for ; i < len(to) ; i ++ {
  263. fromElt := args[i]
  264. toElt := to[i]
  265. err := fromElt.Convert(toElt)
  266. if err != nil {
  267. return nil, err
  268. }
  269. }
  270. rest := args[i:len(args)]
  271. return rest, nil
  272. }
  273. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  274. func ParseArgsToIntSlice(args []Value) ([]int, error) {
  275. res := []int{}
  276. for _, arg := range args{
  277. value := int(0)
  278. err := arg.Convert(&value)
  279. if err != nil {
  280. return res, err
  281. }
  282. res = append(res, value)
  283. }
  284. return res, nil
  285. }
  286. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  287. func ParseArgsToFloat64Slice(args []Value) ([]float64, error) {
  288. res := []float64{}
  289. for _, arg := range args{
  290. value := float64(0.0)
  291. err := arg.Convert(&value)
  292. if err != nil {
  293. return res, err
  294. }
  295. res = append(res, value)
  296. }
  297. return res, nil
  298. }
  299. /* Helpers to easily convert Muesli values from "normal" Go values. */
  300. func To(from interface{}) Value {
  301. switch val := from.(type) {
  302. case string:
  303. return StringValue(val)
  304. case int8:
  305. return IntValue(val)
  306. case int16:
  307. return IntValue(val)
  308. case int32:
  309. return IntValue(val)
  310. case int64:
  311. return IntValue(val)
  312. case int:
  313. return IntValue(val)
  314. case bool:
  315. return BoolValue(val)
  316. case float32:
  317. return FloatValue(val)
  318. case float64:
  319. return FloatValue(val)
  320. case error:
  321. return ErrorValue{val}
  322. case Value:
  323. return val
  324. default:
  325. return NewErrorValuef("Cannot convert value %v", from)
  326. }
  327. }
  328. func ListTo(froms ...interface{}) []Value {
  329. list := make([]Value, 0)
  330. for _, from := range froms {
  331. val := To(from)
  332. list = append(list, val)
  333. }
  334. return list
  335. }
  336. func ListFrom(froms []Value, tos ...interface{}) error {
  337. for i, from := range froms {
  338. if i >= len(tos) {
  339. break
  340. }
  341. err := From(from, tos[i])
  342. if err != nil {
  343. return err
  344. }
  345. }
  346. return nil
  347. }
  348. func ListFromList(froms []Value) []interface{} {
  349. res := make([]interface{}, len(froms))
  350. for i, from := range froms {
  351. res[i] = from
  352. }
  353. return res
  354. }
  355. func Return(args ... Value) []Value {
  356. return args
  357. }
  358. func ReturnEmpty() []Value {
  359. return []Value{EmptyValue{}}
  360. }
  361. func ReturnError(err error) []Value {
  362. return []Value{ErrorValue{err}}
  363. }
  364. func Ok(args ... Value) []Value {
  365. return args
  366. }
  367. func Fail(err error) []Value {
  368. return []Value{ErrorValue{err}}
  369. }
  370. func None() []Value {
  371. return []Value{EmptyValue{}}
  372. }
  373. func BoolOk(b bool) []Value{
  374. return []Value{BoolValue(b)}
  375. }
  376. func IntOk(i int) []Value{
  377. return []Value{IntValue(i)}
  378. }
  379. func FloatOk(f float64) []Value{
  380. return []Value{FloatValue(f)}
  381. }