value.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. type MapValue struct {
  27. Map map[Value]Value
  28. }
  29. // Values is simply a shorthand alias for []Value
  30. type Values = []Value
  31. const (
  32. TrueValue = BoolValue(true)
  33. FalseValue = BoolValue(false)
  34. IntTypeValue = TypeValue("Int")
  35. FloatTypeValue = TypeValue("Float")
  36. StringTypeValue = TypeValue("String")
  37. BoolTypeValue = TypeValue("Bool")
  38. WordTypeValue = TypeValue("Word")
  39. ErrorTypeValue = TypeValue("Error")
  40. TypeTypeValue = TypeValue("Type")
  41. EmptyTypeValue = TypeValue("Empty")
  42. ListTypeValue = TypeValue("List")
  43. MapTypeValue = TypeValue("Map")
  44. AnyTypeValue = TypeValue("Any")
  45. ZeroTypeValue = TypeValue("")
  46. )
  47. var NilValue = Value(nil)
  48. func (val IntValue) String() string {
  49. return fmt.Sprintf("%d", int64(val))
  50. }
  51. func (val FloatValue) String() string {
  52. return fmt.Sprintf("%f", float64(val))
  53. }
  54. func (val BoolValue) String() string {
  55. if bool(val) {
  56. return "true"
  57. } else {
  58. return "false"
  59. }
  60. }
  61. func (val StringValue) String() string {
  62. return string(val)
  63. }
  64. func (val WordValue) String() string {
  65. return string(val)
  66. }
  67. func (val TypeValue) String() string {
  68. return string(val)
  69. }
  70. func (val ErrorValue) String() string {
  71. return fmt.Sprintf("%s", val.Error())
  72. }
  73. func (val EmptyValue) String() string {
  74. return "<empty>"
  75. }
  76. func (val ListValue) String() string {
  77. res := "["
  78. sep := ""
  79. for _, elt := range val.List {
  80. if elt == nil {
  81. res = res + sep + "nil"
  82. } else {
  83. res = res + sep + elt.String()
  84. }
  85. sep = ", "
  86. }
  87. res += "]"
  88. return res
  89. }
  90. func (val MapValue) String() string {
  91. res := "{"
  92. sep := ""
  93. for k, v := range val.Map {
  94. res = res + sep + k.String() + "=>" + v.String()
  95. sep = ", "
  96. }
  97. res += "}"
  98. return res
  99. }
  100. func (v IntValue) Type() TypeValue { return IntTypeValue }
  101. func (v FloatValue) Type() TypeValue { return FloatTypeValue }
  102. func (v StringValue) Type() TypeValue { return StringTypeValue }
  103. func (v BoolValue) Type() TypeValue { return BoolTypeValue }
  104. func (v WordValue) Type() TypeValue { return WordTypeValue }
  105. func (v TypeValue) Type() TypeValue { return TypeTypeValue }
  106. func (v ErrorValue) Type() TypeValue { return ErrorTypeValue }
  107. func (v EmptyValue) Type() TypeValue { return EmptyTypeValue }
  108. func (v ListValue) Type() TypeValue { return ListTypeValue }
  109. func (v MapValue) Type() TypeValue { return MapTypeValue }
  110. func NewErrorValuef(format string, args ...interface{}) ErrorValue {
  111. err := fmt.Errorf(format, args...)
  112. return ErrorValue{err}
  113. }
  114. func NewListValue(elements ...Value) * ListValue {
  115. return &ListValue{elements}
  116. }
  117. func (list *ListValue) Append(elements ...Value) {
  118. list.List = append(list.List, elements...)
  119. }
  120. func (list *ListValue) AppendList(toAppend ListValue) {
  121. list.List = append(list.List, toAppend.List...)
  122. }
  123. func (list ListValue) Length() int {
  124. return len(list.List)
  125. }
  126. func (list *ListValue) Fetch(i int) Value {
  127. if i >= len(list.List) {
  128. return NilValue
  129. }
  130. return list.List[i]
  131. }
  132. func (list *ListValue) Place(i int, v Value) Value {
  133. if i >= len(list.List) {
  134. return NilValue
  135. }
  136. list.List[i] = v
  137. return list.List[i]
  138. }
  139. func (list *ListValue) First() Value {
  140. return list.Fetch(0)
  141. }
  142. func (list *ListValue) Last() Value {
  143. return list.Fetch(list.Length()-1)
  144. }
  145. func EmptyListValue() ListValue {
  146. return ListValue{make([]Value, 0)}
  147. }
  148. func EmptyValueArray() []Value {
  149. return make([]Value, 0)
  150. }
  151. func NewValueArray(elements ...Value) []Value {
  152. return elements
  153. }
  154. func NewMapValue(elements map[Value]Value) * MapValue {
  155. return &MapValue{elements}
  156. }
  157. func (m *MapValue) Fetch(key Value) Value {
  158. var res Value
  159. var ok bool
  160. if res, ok = m.Map[key] ; !ok {
  161. return NilValue
  162. }
  163. return res
  164. }
  165. func (m *MapValue) Place(key Value, value Value) Value {
  166. m.Map[key] = value
  167. return m.Map[key]
  168. }
  169. func (from IntValue) Convert(to interface{}) error {
  170. switch toPtr := to.(type) {
  171. case *string:
  172. (*toPtr) = from.String()
  173. case *int8:
  174. (*toPtr) = int8(from)
  175. case *int16:
  176. (*toPtr) = int16(from)
  177. case *int32:
  178. (*toPtr) = int32(from)
  179. case *int64:
  180. (*toPtr) = int64(from)
  181. case *int:
  182. (*toPtr) = int(from)
  183. case *bool:
  184. (*toPtr) = (from != 0)
  185. case *float32:
  186. (*toPtr) = float32(from)
  187. case *float64:
  188. (*toPtr) = float64(from)
  189. case *IntValue:
  190. (*toPtr) = from
  191. case *Value:
  192. (*toPtr) = from
  193. default:
  194. return NewErrorValuef("Cannot convert IntValue value %v to %v", from, to)
  195. }
  196. return nil
  197. }
  198. func (from FloatValue) Convert(to interface{}) error {
  199. switch toPtr := to.(type) {
  200. case *string:
  201. (*toPtr) = from.String()
  202. case *int8:
  203. (*toPtr) = int8(from)
  204. case *int16:
  205. (*toPtr) = int16(from)
  206. case *int32:
  207. (*toPtr) = int32(from)
  208. case *int64:
  209. (*toPtr) = int64(from)
  210. case *int:
  211. (*toPtr) = int(from)
  212. case *bool:
  213. (*toPtr) = (from != 0)
  214. case *float32:
  215. (*toPtr) = float32(from)
  216. case *float64:
  217. (*toPtr) = float64(from)
  218. case *FloatValue:
  219. (*toPtr) = from
  220. case *Value:
  221. (*toPtr) = from
  222. default:
  223. return NewErrorValuef("Cannot convert FloatValue value %v to %v", from, to)
  224. }
  225. return nil
  226. }
  227. func (from StringValue) Convert(to interface{}) error {
  228. switch toPtr := to.(type) {
  229. case *string:
  230. (*toPtr) = from.String()
  231. case *StringValue:
  232. (*toPtr) = from
  233. case *Value:
  234. (*toPtr) = from
  235. default:
  236. return NewErrorValuef("Cannot convert StringValue %v to %v", from, to)
  237. }
  238. return nil
  239. }
  240. func (from WordValue) Convert(to interface{}) error {
  241. switch toPtr := to.(type) {
  242. case *string:
  243. (*toPtr) = from.String()
  244. case *WordValue:
  245. (*toPtr) = from
  246. case *Value:
  247. (*toPtr) = from
  248. default:
  249. return NewErrorValuef("Cannot convert WordValue %v to %v", from, to)
  250. }
  251. return nil
  252. }
  253. func (from TypeValue) Convert(to interface{}) error {
  254. switch toPtr := to.(type) {
  255. case *string:
  256. (*toPtr) = from.String()
  257. case *TypeValue:
  258. (*toPtr) = from
  259. case *Value:
  260. (*toPtr) = from
  261. default:
  262. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  263. }
  264. return nil
  265. }
  266. func (from BoolValue) Convert(to interface{}) error {
  267. switch toPtr := to.(type) {
  268. case *bool:
  269. (*toPtr) = bool(from)
  270. case *BoolValue:
  271. (*toPtr) = from
  272. case *Value:
  273. (*toPtr) = from
  274. default:
  275. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  276. }
  277. return nil
  278. }
  279. func (from ErrorValue) Convert(to interface{}) error {
  280. switch toPtr := to.(type) {
  281. case *string:
  282. (*toPtr) = from.String()
  283. case *error:
  284. (*toPtr) = from.error
  285. case *ErrorValue:
  286. (*toPtr) = from
  287. case *Value:
  288. (*toPtr) = from
  289. default:
  290. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  291. }
  292. return nil
  293. }
  294. func (from EmptyValue) Convert(to interface{}) error {
  295. return NewErrorValuef("Cannot convert the empty value %v to %v", from, to)
  296. }
  297. func (from * ListValue) Convert(to interface{}) error {
  298. switch toPtr := to.(type) {
  299. case *[]Value:
  300. (*toPtr) = from.List
  301. case **ListValue:
  302. (*toPtr) = from
  303. case *Value:
  304. (*toPtr) = from
  305. default:
  306. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  307. }
  308. return nil
  309. }
  310. func (from * MapValue) Convert(to interface{}) error {
  311. switch toPtr := to.(type) {
  312. case *map[Value]Value:
  313. (*toPtr) = from.Map
  314. case **MapValue:
  315. (*toPtr) = from
  316. case *Value:
  317. (*toPtr) = from
  318. default:
  319. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  320. }
  321. return nil
  322. }
  323. /* Helpers to easily convert Muesli values to "normal" Go values. */
  324. func From(from Value, to interface{}) error {
  325. return from.Convert(to)
  326. }
  327. // ParseOptArgs ia helper to easily convert Muesli value lists to "normal"
  328. // Go values. args is the input, required is the amount of required aruments,
  329. // and to are pointers to where to store the data. The args are converted by
  330. // calling the Convert() method on them.
  331. // The returned array contains the remaining unparsed arguments.
  332. func ParseOptArgs(args []Value, required int, to...interface{}) ([]Value, error) {
  333. if required > len(args) {
  334. return nil, NewErrorValuef("Too few arguments, expected %d, got %d", required, len(args))
  335. }
  336. stop := len(args)
  337. if len(to) < stop {
  338. stop = len(to)
  339. }
  340. i:= 0
  341. for ; i < stop ; i ++ {
  342. fromElt := args[i]
  343. toElt := to[i]
  344. if fromElt == nil {
  345. return nil, NewErrorValuef("Nil pointer to result %d", i)
  346. }
  347. err := fromElt.Convert(toElt)
  348. if err != nil {
  349. return nil, err
  350. }
  351. }
  352. rest := args[i:len(args)]
  353. return rest, nil
  354. }
  355. // ParseArgs is helper to easily convert Muesli value lists to "normal"
  356. // Go values. It is the same as ParseOptArgs(args, len(to, to...)
  357. func ParseArgs(args []Value, to...interface{}) ([]Value, error) {
  358. return ParseOptArgs(args, len(to), to...)
  359. }
  360. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  361. func ParseArgsToIntSlice(args []Value) ([]int, error) {
  362. res := []int{}
  363. for _, arg := range args{
  364. value := int(0)
  365. err := arg.Convert(&value)
  366. if err != nil {
  367. return res, err
  368. }
  369. res = append(res, value)
  370. }
  371. return res, nil
  372. }
  373. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  374. func ParseArgsToFloat64Slice(args []Value) ([]float64, error) {
  375. res := []float64{}
  376. for _, arg := range args{
  377. value := float64(0.0)
  378. err := arg.Convert(&value)
  379. if err != nil {
  380. return res, err
  381. }
  382. res = append(res, value)
  383. }
  384. return res, nil
  385. }
  386. /* Helpers to easily convert Muesli values from "normal" Go values. */
  387. func To(from interface{}) Value {
  388. switch val := from.(type) {
  389. case string:
  390. return StringValue(val)
  391. case int8:
  392. return IntValue(val)
  393. case int16:
  394. return IntValue(val)
  395. case int32:
  396. return IntValue(val)
  397. case int64:
  398. return IntValue(val)
  399. case int:
  400. return IntValue(val)
  401. case bool:
  402. return BoolValue(val)
  403. case float32:
  404. return FloatValue(val)
  405. case float64:
  406. return FloatValue(val)
  407. case error:
  408. return ErrorValue{val}
  409. case Value:
  410. return val
  411. default:
  412. return NewErrorValuef("Cannot convert value %v", from)
  413. }
  414. }
  415. func ListTo(froms ...interface{}) []Value {
  416. list := make([]Value, 0)
  417. for _, from := range froms {
  418. val := To(from)
  419. list = append(list, val)
  420. }
  421. return list
  422. }
  423. func ListFrom(froms []Value, tos ...interface{}) error {
  424. for i, from := range froms {
  425. if i >= len(tos) {
  426. break
  427. }
  428. err := From(from, tos[i])
  429. if err != nil {
  430. return err
  431. }
  432. }
  433. return nil
  434. }
  435. func ListFromList(froms []Value) []interface{} {
  436. res := make([]interface{}, len(froms))
  437. for i, from := range froms {
  438. res[i] = from
  439. }
  440. return res
  441. }
  442. func Return(args ... Value) []Value {
  443. return args
  444. }
  445. func ReturnEmpty() []Value {
  446. return []Value{EmptyValue{}}
  447. }
  448. func ReturnError(err error) []Value {
  449. return []Value{ErrorValue{err}}
  450. }
  451. func Ok(args ... Value) []Value {
  452. return args
  453. }
  454. func Fail(err error) []Value {
  455. return []Value{ErrorValue{err}}
  456. }
  457. func None() []Value {
  458. return []Value{EmptyValue{}}
  459. }
  460. func BoolOk(b bool) []Value{
  461. return []Value{BoolValue(b)}
  462. }
  463. func IntOk(i int) []Value{
  464. return []Value{IntValue(i)}
  465. }
  466. func FloatOk(f float64) []Value{
  467. return []Value{FloatValue(f)}
  468. }