12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package xvdoc
- import (
- "encoding/json"
- )
- type Cell interface {
- Val() interface{}
- Type() string
- Styles() []string
- }
- type CellConstructor func(*json.Decoder, json.Token) (Cell, error)
- var (
- cellConstructors []CellConstructor
- )
- func AppendCellConstructor(constructor CellConstructor) {
- cellConstructors = append(cellConstructors, constructor)
- }
- func init() {
- cellConstructors = []CellConstructor{
- constructorString,
- constructorNumber,
- constructorObject,
- }
- }
- func parseCell(dec *json.Decoder, t json.Token) (cell Cell, err error) {
- for _, constructor := range cellConstructors {
- if cell, err = constructor(dec, t); cell != nil || err != nil {
- return
- }
- }
- return
- }
- func parseCells(dec *json.Decoder) (res []Cell, err error) {
- var (
- t json.Token
- cell Cell
- )
- if t, err = dec.Token(); err == nil {
- if _, check := t.(json.Delim); check && t == json.Delim('[') {
- for err == nil {
- if t, err = dec.Token(); err == nil {
- if _, check := t.(json.Delim); check && t == json.Delim(']') {
- return
- }
- }
- if cell, err = parseCell(dec, t); err == nil && cell != nil {
- res = append(res, cell)
- }
- }
- } else {
- if cell, err = parseCell(dec, t); err == nil && cell != nil {
- res = []Cell{cell}
- }
- }
- }
- return
- }
|