value.go 11 KB

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