value.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. default:
  149. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  150. }
  151. return nil
  152. }
  153. func (from FloatValue) Convert(to interface{}) error {
  154. switch toPtr := to.(type) {
  155. case *string:
  156. (*toPtr) = from.String()
  157. case *int8:
  158. (*toPtr) = int8(from)
  159. case *int16:
  160. (*toPtr) = int16(from)
  161. case *int32:
  162. (*toPtr) = int32(from)
  163. case *int64:
  164. (*toPtr) = int64(from)
  165. case *int:
  166. (*toPtr) = int(from)
  167. case *bool:
  168. (*toPtr) = (from != 0)
  169. case *float32:
  170. (*toPtr) = float32(from)
  171. case *float64:
  172. (*toPtr) = float64(from)
  173. default:
  174. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  175. }
  176. return nil
  177. }
  178. func (from StringValue) Convert(to interface{}) error {
  179. switch toPtr := to.(type) {
  180. case *string:
  181. (*toPtr) = from.String()
  182. default:
  183. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  184. }
  185. return nil
  186. }
  187. func (from WordValue) Convert(to interface{}) error {
  188. switch toPtr := to.(type) {
  189. case *string:
  190. (*toPtr) = from.String()
  191. default:
  192. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  193. }
  194. return nil
  195. }
  196. func (from TypeValue) Convert(to interface{}) error {
  197. switch toPtr := to.(type) {
  198. case *string:
  199. (*toPtr) = from.String()
  200. default:
  201. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  202. }
  203. return nil
  204. }
  205. func (from BoolValue) Convert(to interface{}) error {
  206. switch toPtr := to.(type) {
  207. case *bool:
  208. (*toPtr) = bool(from)
  209. default:
  210. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  211. }
  212. return nil
  213. }
  214. func (from ErrorValue) Convert(to interface{}) error {
  215. switch toPtr := to.(type) {
  216. case *string:
  217. (*toPtr) = from.String()
  218. case *error:
  219. (*toPtr) = from.error
  220. default:
  221. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  222. }
  223. return nil
  224. }
  225. func (from EmptyValue) Convert(to interface{}) error {
  226. return NewErrorValuef("Cannot convert the empty value %v to %v", from, to)
  227. }
  228. func (from ListValue) Convert(to interface{}) error {
  229. switch toPtr := to.(type) {
  230. case *[]Value:
  231. (*toPtr) = from.List
  232. default:
  233. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  234. }
  235. return nil
  236. }
  237. /* Helpers to easily convert Muesli values to "normal" Go values. */
  238. func From(from Value, to interface{}) error {
  239. return from.Convert(to)
  240. }
  241. /* Helpers to easily convert Muesli value lists to "normal" Go values. */
  242. func ParseArgs(args []Value, to...interface{}) ([]interface{}, error) {
  243. if len(to) > len(args) {
  244. return nil, NewErrorValuef("Too few arguments, expected %d, got %d", len(to), len(args))
  245. }
  246. i:= 0
  247. for ; i < len(to) ; i ++ {
  248. fromElt := args[i]
  249. toElt := to[i]
  250. err := fromElt.Convert(toElt)
  251. if err != nil {
  252. return nil, err
  253. }
  254. }
  255. rest := args[i:len(args)]
  256. return ListFromList(rest), nil
  257. }
  258. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  259. func ParseArgsToIntSlice(args []Value) ([]int, error) {
  260. res := []int{}
  261. for _, arg := range args{
  262. value := int(0)
  263. err := arg.Convert(&value)
  264. if err != nil {
  265. return res, err
  266. }
  267. res = append(res, value)
  268. }
  269. return res, nil
  270. }
  271. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  272. func ParseArgsToFloat64Slice(args []Value) ([]float64, error) {
  273. res := []float64{}
  274. for _, arg := range args{
  275. value := float64(0.0)
  276. err := arg.Convert(&value)
  277. if err != nil {
  278. return res, err
  279. }
  280. res = append(res, value)
  281. }
  282. return res, nil
  283. }
  284. /* Helpers to easily convert Muesli values from "normal" Go values. */
  285. func To(from interface{}) Value {
  286. switch val := from.(type) {
  287. case string:
  288. return StringValue(val)
  289. case int8:
  290. return IntValue(val)
  291. case int16:
  292. return IntValue(val)
  293. case int32:
  294. return IntValue(val)
  295. case int64:
  296. return IntValue(val)
  297. case int:
  298. return IntValue(val)
  299. case bool:
  300. return BoolValue(val)
  301. case float32:
  302. return FloatValue(val)
  303. case float64:
  304. return FloatValue(val)
  305. case error:
  306. return ErrorValue{val}
  307. case Value:
  308. return val
  309. default:
  310. return NewErrorValuef("Cannot convert value %v", from)
  311. }
  312. }
  313. func ListTo(froms ...interface{}) []Value {
  314. list := make([]Value, 0)
  315. for _, from := range froms {
  316. val := To(from)
  317. list = append(list, val)
  318. }
  319. return list
  320. }
  321. func ListFrom(froms []Value, tos ...interface{}) error {
  322. for i, from := range froms {
  323. if i >= len(tos) {
  324. break
  325. }
  326. err := From(from, tos[i])
  327. if err != nil {
  328. return err
  329. }
  330. }
  331. return nil
  332. }
  333. func ListFromList(froms []Value) []interface{} {
  334. res := make([]interface{}, len(froms))
  335. for i, from := range froms {
  336. res[i] = from
  337. }
  338. return res
  339. }
  340. func Return(args ... Value) []Value {
  341. return args
  342. }
  343. func ReturnEmpty() []Value {
  344. return []Value{EmptyValue{}}
  345. }
  346. func ReturnError(err error) []Value {
  347. return []Value{ErrorValue{err}}
  348. }
  349. func Ok(args ... Value) []Value {
  350. return args
  351. }
  352. func Fail(err error) []Value {
  353. return []Value{ErrorValue{err}}
  354. }
  355. func None() []Value {
  356. return []Value{EmptyValue{}}
  357. }
  358. func BoolOk(b bool) []Value{
  359. return []Value{BoolValue(b)}
  360. }
  361. func IntOk(i int) []Value{
  362. return []Value{IntValue(i)}
  363. }
  364. func FloatOk(f float64) []Value{
  365. return []Value{FloatValue(f)}
  366. }