-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataValue.go
117 lines (90 loc) · 2.72 KB
/
dataValue.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
// Described in <https://www.mediawiki.org/wiki/Wikibase/DataModel/JSON#Data_Values>
package gowd
import (
"encoding/json"
"strings"
"errors"
)
var DataValueTypeSupports map[string]bool
func init() {
DataValueTypeSupports = map[string]bool{
"string": true,
"wikibase-entityid": true,
"globecoordinate": false,
"quantity": false,
"time": false,
"monolingualtext": false,
}
}
func IsDataValueType(s string) bool {
_, exist := DataValueTypeSupports[s]
return exist
}
func IsImplementedDataValueType(s string) bool {
return DataValueTypeSupports[s]
}
type DataValue interface{
_isDataValue()
Type() string
}
func (*String) _isDataValue() {}
func (*WikibaseEntityId) _isDataValue() {}
func (*GlobeCoordinate) _isDataValue() {}
func (*Quantity) _isDataValue() {}
func (*Time) _isDataValue() {}
func (*MonolingualText) _isDataValue() {}
func (*String) Type() string { return "string" }
func (*WikibaseEntityId) Type() string { return "wikibase-entityid" }
func (*GlobeCoordinate) Type() string { return "globecoordinate" }
func (*Quantity) Type() string { return "quantity" }
func (*Time) Type() string { return "time" }
func (*MonolingualText) Type() string { return "monolingualtext" }
type String string
type WikibaseEntityId struct {
Value string `json:"id"`
EntityType string `json:"entity-type"`
NumericId Integer `json:"numeric-id"`
}
type GlobeCoordinate struct {
json.RawMessage
}
type Quantity struct {
json.RawMessage
}
type Time struct {
json.RawMessage
}
type MonolingualText struct {
Language string `json:"language"`
Value string `json:"value"`
}
type DataValueInterpreter struct {
DataValue `json:"value"`
}
func (data *DataValueInterpreter) UnmarshalJSON(b []byte) error {
var tempData struct {
Type string `json:"type"`
Value json.RawMessage `json:"value"`
}
if err := json.Unmarshal(b, &tempData); err != nil {
return err
}
switch strings.ToLower(tempData.Type) {
case "string":
data.DataValue = new(String)
case "wikibase-entityid":
data.DataValue = new(WikibaseEntityId)
case "globecoordinate":
data.DataValue = new(GlobeCoordinate)
case "quantity":
data.DataValue = new(Quantity)
case "time":
data.DataValue = new(Time)
case "monolingualtext":
data.DataValue = new(MonolingualText)
default:
return errors.New(`Unsupported value type: "`+tempData.Type+`"`)
}
err := json.Unmarshal(tempData.Value, data.DataValue)
return err
}