cell_object.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package xvdoc
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type ObjConstructor func(JSONMap) (Cell, error)
  7. var (
  8. objConstructors = map[string]ObjConstructor{
  9. "formula": constructorFormula,
  10. }
  11. )
  12. func constructorObject(dec *json.Decoder, t json.Token) (res Cell, err error) {
  13. m := make(JSONMap)
  14. if _, check := t.(json.Delim); check && t == json.Delim('{') {
  15. for err == nil {
  16. if t, err = dec.Token(); err == nil {
  17. if _, check := t.(json.Delim); check {
  18. if t == json.Delim('}') {
  19. if constructor, check := objConstructors[m.StringVal("type", "")]; check {
  20. res, err = constructor(m)
  21. } else {
  22. res = &CellObj{
  23. value: m.Interface("value"),
  24. styles: m.StringArray("styles"),
  25. }
  26. }
  27. return
  28. }
  29. } else {
  30. var i interface{}
  31. if err = dec.Decode(&i); err == nil {
  32. m[t.(string)] = i
  33. }
  34. }
  35. }
  36. }
  37. }
  38. return
  39. }
  40. ////////////////////////////////////////////
  41. type CellObj struct {
  42. value interface{}
  43. styles []string
  44. }
  45. func (s *CellObj) Val() interface{} {
  46. return s.value
  47. }
  48. func (s *CellObj) Type() string {
  49. return ""
  50. }
  51. func (s *CellObj) String() string {
  52. return fmt.Sprintf("{ value :: %v }", s.value)
  53. }
  54. func (s *CellObj) Styles() (res []string) {
  55. return s.styles
  56. }