-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_reader.go
160 lines (135 loc) · 3.64 KB
/
block_reader.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
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
)
type Statement struct {
Statement string `json:"statement"`
}
type TransactionInfo struct {
Statements []Statement `json:"statements"`
}
type Block struct {
TransactionId string `json:"transactionId"`
TransactionInfo TransactionInfo `json:"transactionInfo"`
}
type VehicleRegistrationBlock struct {
TransactionId string `json:"transactionId"`
TransactionInfo TransactionInfo `json:"transactionInfo"`
Revisions []VehicleRegistrationRevision `json:"revisions"`
}
type PersonBlock struct {
TransactionId string `json:"transactionId"`
TransactionInfo TransactionInfo `json:"transactionInfo"`
Revisions []PersonRevision `json:"revisions"`
}
type DriversLicenseBlock struct {
TransactionId string `json:"transactionId"`
TransactionInfo TransactionInfo `json:"transactionInfo"`
Revisions []DriversLicenseRevision `json:"revisions"`
}
type VehicleBlock struct {
TransactionId string `json:"transactionId"`
TransactionInfo TransactionInfo `json:"transactionInfo"`
Revisions []VehicleRevision `json:"revisions"`
}
type BlockUnmarshaler struct {
Data interface{}
}
func (u *BlockUnmarshaler) UnmarshalJSON(b []byte) error {
vehicleRegistrationBlock := &VehicleRegistrationBlock{
TransactionInfo: TransactionInfo{
Statements: make([]Statement, 0),
},
Revisions: make([]VehicleRegistrationRevision, 0),
}
err := json.Unmarshal(b, vehicleRegistrationBlock)
if err == nil {
if len(vehicleRegistrationBlock.Revisions) > 0 {
rev := vehicleRegistrationBlock.Revisions[0]
if rev.Data.VIN != "" && rev.Data.LicensePlateNumber != "" {
u.Data = vehicleRegistrationBlock
return nil
}
}
}
if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {
return err
}
personBlock := &PersonBlock{
TransactionInfo: TransactionInfo{
Statements: make([]Statement, 0),
},
Revisions: make([]PersonRevision, 0),
}
err = json.Unmarshal(b, personBlock)
if err == nil {
if len(personBlock.Revisions) > 0 {
rev := personBlock.Revisions[0]
if rev.Data.GovId != "" {
u.Data = personBlock
return nil
}
}
}
if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {
return err
}
driversLicenseBlock := &DriversLicenseBlock{
TransactionInfo: TransactionInfo{
Statements: make([]Statement, 0),
},
Revisions: make([]DriversLicenseRevision, 0),
}
err = json.Unmarshal(b, driversLicenseBlock)
if err == nil {
if len(driversLicenseBlock.Revisions) > 0 {
rev := driversLicenseBlock.Revisions[0]
if rev.Data.LicensePlateNumber != "" {
u.Data = driversLicenseBlock
return nil
}
}
}
if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {
return err
}
vehicleBlock := &VehicleBlock{
TransactionInfo: TransactionInfo{
Statements: make([]Statement, 0),
},
Revisions: make([]VehicleRevision, 0),
}
err = json.Unmarshal(b, vehicleBlock)
if err == nil {
if len(vehicleBlock.Revisions) > 0 {
rev := vehicleBlock.Revisions[0]
if rev.Data.VIN != "" && rev.Data.Make != "" {
u.Data = vehicleBlock
return nil
}
}
}
// abort if we have an error other than the wrong type
if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {
return err
}
block := &Block{
TransactionInfo: TransactionInfo{
Statements: make([]Statement, 0),
},
}
err = json.Unmarshal(b, block)
if err != nil {
return err
}
u.Data = block
return nil
}
func ReadBlock(b []byte) (interface{}, error) {
bu := &BlockUnmarshaler{}
err := json.Unmarshal(b, bu)
if err != nil {
return nil, err
}
return bu.Data, nil
}