-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload.go
149 lines (132 loc) · 4.09 KB
/
load.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package measure
import (
"encoding/json"
"errors"
"fmt"
)
// ByPointLoader can be embedded in schema structs and unmarshals a ByPoint JSON
// object into the appropriate implementation.
type ByPointLoader struct {
byPoint ByPoint
}
type pointType string
const (
typeScale pointType = "scale"
typeEuclidean pointType = "euclidean"
typeHaversine pointType = "haversine"
typeTaxicab pointType = "taxicab"
typeConstant pointType = "constant"
)
type byPointJSON struct {
ByPoint *ByPointLoader `json:"measure"`
Type pointType `json:"type"`
Scale float64 `json:"scale"`
Constant float64 `json:"constant"`
}
// MarshalJSON returns the JSON representation for the underlying ByPoint.
func (l ByPointLoader) MarshalJSON() ([]byte, error) {
return json.Marshal(l.byPoint)
}
// UnmarshalJSON converts the bytes into the appropriate implementation of
// ByPoint.
func (l *ByPointLoader) UnmarshalJSON(b []byte) error {
var j byPointJSON
if err := json.Unmarshal(b, &j); err != nil {
return err
}
switch j.Type {
case "":
return errors.New(`no "type" field in json input`)
case typeEuclidean:
l.byPoint = EuclideanByPoint()
case typeHaversine:
l.byPoint = HaversineByPoint()
case typeTaxicab:
l.byPoint = TaxicabByPoint()
case typeScale:
l.byPoint = ScaleByPoint(j.ByPoint.To(), j.Scale)
case typeConstant:
l.byPoint = ConstantByPoint(j.Constant)
default:
return fmt.Errorf(`invalid type "%s"`, j.Type)
}
return nil
}
// To returns the underlying ByPoint.
func (l *ByPointLoader) To() ByPoint {
return l.byPoint
}
// ByIndexLoader can be embedded in schema structs and unmarshals a ByIndex JSON
// object into the appropriate implementation.
type ByIndexLoader struct {
byIndex ByIndex
}
// byIndexJSON includes the union of all fields that may appear on a ByIndex
// JSON object (like a C oneof). We unmarshal onto this data structure instead
// of onto a map[string]any for type safety and because this will allow
// recursive measures to be automatically unmarshalled.
type byIndexJSON struct {
ByIndex *ByIndexLoader `json:"measure"`
Arcs map[int]map[int]float64 `json:"arcs"`
Type string `json:"type"`
Measures []ByIndexLoader `json:"measures"`
Costs []float64 `json:"costs"`
Matrix [][]float64 `json:"matrix"`
Constant float64 `json:"constant"`
Scale float64 `json:"scale"`
Exponent float64 `json:"exponent"`
Lower float64 `json:"lower"`
Upper float64 `json:"upper"`
References []int `json:"references"`
}
// MarshalJSON returns the JSON representation for the underlying Byindex.
func (l ByIndexLoader) MarshalJSON() ([]byte, error) {
return json.Marshal(l.byIndex)
}
// UnmarshalJSON converts the bytes into the appropriate implementation of
// ByIndex.
func (l *ByIndexLoader) UnmarshalJSON(b []byte) error {
var j byIndexJSON
if err := json.Unmarshal(b, &j); err != nil {
return err
}
requiresByIndex := j.Type == "location" ||
j.Type == "power" ||
j.Type == "scale" ||
j.Type == "sparse" ||
j.Type == "truncate"
if requiresByIndex && j.ByIndex == nil {
return errors.New(`location measure must include a "by_index" field`)
}
switch j.Type {
case "":
return errors.New(`no "type" field in json input`)
case "constant":
l.byIndex = Constant(j.Constant)
case "sum":
measures := make([]ByIndex, len(j.Measures))
for i, l := range j.Measures {
measures[i] = l.To()
}
l.byIndex = Sum(measures...)
case "matrix":
l.byIndex = Matrix(j.Matrix)
case "power":
l.byIndex = Power(j.ByIndex.To(), j.Exponent)
case "scale":
l.byIndex = Scale(j.ByIndex.To(), j.Scale)
case "sparse":
l.byIndex = Sparse(j.ByIndex.To(), j.Arcs)
case "truncate":
l.byIndex = Truncate(j.ByIndex.To(), j.Lower, j.Upper)
case "unique":
l.byIndex = Unique(j.ByIndex.To(), j.References)
default:
return fmt.Errorf(`invalid type "%s"`, j.Type)
}
return nil
}
// To returns the underlying ByIndex.
func (l *ByIndexLoader) To() ByIndex {
return l.byIndex
}