/
data.go
88 lines (71 loc) · 1.94 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package action
import (
"errors"
"fmt"
"strings"
"time"
"github.com/peterstace/simplefeatures/geom"
)
// Data to hold the current state of the input and the stack of applied transformations
type Data struct {
RawValue []byte
Value any
Format Format
StructuredData map[string]any
Stack []*Action
}
var ErrEmptyStack = errors.New("empty stack")
func NewDataText(v []byte) *Data {
return &Data{RawValue: v, Format: textFormat}
}
func (d *Data) StoreTextValue(v []byte, a *Action) *Data {
return &Data{RawValue: v, Format: textFormat, Stack: append(d.Stack, a)}
}
func (d *Data) StoreTextListValue(l []string, a *Action) *Data {
return &Data{Value: l, Format: textListFormat, Stack: append(d.Stack, a)}
}
func (d *Data) StoreTimeValue(t time.Time, a *Action) *Data {
return &Data{Value: t, Stack: append(d.Stack, a), Format: timeFormat}
}
func (d *Data) StoreGeomValue(g geom.Geometry, a *Action) *Data {
return &Data{Value: g, Stack: append(d.Stack, a), Format: geoFormat}
}
func (d *Data) StoreJSONValue(t time.Time, a *Action) *Data {
return &Data{Value: t, Stack: append(d.Stack, a), Format: jsonFormat}
}
// Undo removed the last actions if any
// Reapply the stack with input
func (d *Data) Undo(in []byte) (*Data, *Action, error) {
if len(d.Stack) == 0 {
return nil, nil, ErrEmptyStack
}
var oa *Action
oa, d.Stack = d.Stack[len(d.Stack)-1], d.Stack[:len(d.Stack)-1]
nd := NewDataText(in)
for _, a := range d.Stack {
out, err := a.Transform(nd)
if err != nil {
return nil, nil, err
}
nd = out
}
return nd, oa, nil
}
func (d *Data) String() string {
switch d.Format {
case textFormat:
return string(d.RawValue)
case timeFormat:
t := d.Value.(time.Time)
return t.String()
default:
return fmt.Sprintf("%v", d.Value)
}
}
func (d *Data) StackString() string {
names := make([]string, len(d.Stack))
for i, a := range d.Stack {
names[i] = a.Title()
}
return strings.Join(names, ",")
}