forked from klauspost/dawa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistquery.go
355 lines (326 loc) · 10.8 KB
/
listquery.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package dawa
import (
"bufio"
"encoding/json"
"fmt"
"github.com/ugorji/go/codec"
"io"
"io/ioutil"
"reflect"
"strconv"
)
// ListQuery returns query item for searching DAWA for specific list types.
// Use dawa.NewListQuery(type string, autocomplete bool) to create a new query.
//
// See 'examples/query-list.go' for a usage example.
//
// See documentation at http://dawa.aws.dk/listerdok
type ListQuery struct {
queryGeoJSON
listType string
}
// ListQuery returns query item for searching DAWA for specific list types.
//
// Supported list types are "regioner","sogne","retskredse","politikredse","opstillingskredse","valglandsdele","ejerlav", "adgangsadresser", "adresser" or "postnumre".
// Use the corresponding iterator function, for instance i.NextRegion() to get typed results.
//
// See 'examples/query-list.go' for a usage example.
//
// See documentation at http://dawa.aws.dk/listerdok
func NewListQuery(listType string, autoComplete bool) *ListQuery {
path := "/" + listType
if autoComplete {
path += "/autocomplete"
}
q := &ListQuery{listType: listType, queryGeoJSON: queryGeoJSON{query: query{host: DefaultHost, path: path}}}
return q
}
// Q will add a parameter for 'q' to the ListQuery.
//
// Søgetekst. Der søges i kode og navn. Alle ord i søgeteksten skal matche. Wildcard * er tilladt i slutningen af hvert ord.
//
// See http://dawa.aws.dk/listerdok
func (q *ListQuery) Q(s string) *ListQuery {
q.add(&textQuery{Name: "q", Values: []string{s}, Multi: true, Null: false})
return q
}
// Kode will add a parameter for 'kode' to the ListQuery.
//
// Kode for det der søges.
func (q *ListQuery) Kode(s ...string) *ListQuery {
q.add(&textQuery{Name: "kode", Values: s, Multi: true, Null: false})
return q
}
// Navn will add a parameter for 'navn' to the ListQuery.
//
// Navn for det der søges.
func (q *ListQuery) Navn(s string) *ListQuery {
q.add(&textQuery{Name: "navn", Values: []string{s}, Multi: true, Null: false})
return q
}
// NoFormat will disable extra whitespace. Always enabled when querying
func (q *ListQuery) NoFormat() *ListQuery {
q.add(&textQuery{Name: "noformat", Multi: false, Null: true})
return q
}
// ListIter is an Iterator that enable you to get individual entries.
type ListIter struct {
closer
a reflect.Value // Channel
eType reflect.Type // Type of the element
err error
}
func makeChannel(t reflect.Type, chanDir reflect.ChanDir, buffer int) reflect.Value {
ctype := reflect.ChanOf(chanDir, t)
return reflect.MakeChan(ctype, buffer)
}
// Iter creates a list iterator that will allow you to get the items one by one.
//
func (q ListQuery) Iter() (*ListIter, error) {
resp, err := q.NoFormat().Request()
if err != nil {
return nil, err
}
typ := q.Type()
if typ == nil {
return nil, fmt.Errorf("Unknown list type: %s", q.listType)
}
var h codec.JsonHandle
h.DecodeOptions.ErrorIfNoField = JSONStrictFieldCheck
// use a buffered reader for efficiency
in := bufio.NewReader(resp)
ret := &ListIter{}
ret.eType = reflect.TypeOf(typ)
// We create a channel with the expected type
ret.a = makeChannel(ret.eType, reflect.BothDir, 100)
go func() {
defer ret.a.Close()
var dec *codec.Decoder = codec.NewDecoder(in, &h)
channel := ret.a.Interface()
ret.err = dec.Decode(&channel)
if ret.err == nil {
ret.err = io.EOF
}
}()
if err != nil {
return nil, err
}
ret.AddCloser(resp)
return ret, nil
}
// Returns a writeable
func (q ListQuery) Type() interface{} {
switch q.listType {
case "regioner":
return &Region{}
case "kommuner":
return &Kommune{}
case "sogne":
return &Sogn{}
case "retskredse":
return &Retskreds{}
case "politikredse":
return &Politikreds{}
case "opstillingskredse":
return &Opstillingskreds{}
case "valglandsdele":
return &Valglandsdel{}
case "ejerlav":
return &Ejerlav{}
case "adgangsadresser":
return &AdgangsAdresse{}
case "adresser":
return &Adresse{}
case "postnumre":
return &Postnummer{}
}
return nil
}
// Next will return the next item untyped.
// It will return an error if that has been encountered.
// When there are not more entries nil, io.EOF will be returned.
func (a *ListIter) Next() (interface{}, error) {
v, ok := a.a.Recv()
if ok {
return v.Interface(), nil
}
return nil, a.err
}
// NextKommune will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextKommune() (*Kommune, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Kommune{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Kommune), nil
}
// NextRegion will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextRegion() (*Region, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Region{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Region), nil
}
// NextSogn will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextSogn() (*Sogn, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Sogn{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Sogn), nil
}
// NextRetskreds will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextRetskreds() (*Retskreds, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Retskreds{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Retskreds), nil
}
// NextPolitikreds will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextPolitikreds() (*Politikreds, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Politikreds{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Politikreds), nil
}
// NextOpstillingskreds will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextOpstillingskreds() (*Opstillingskreds, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Opstillingskreds{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Opstillingskreds), nil
}
// NextValglandsdel will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextValglandsdel() (*Valglandsdel, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Valglandsdel{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Valglandsdel), nil
}
// NextEjerlav will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextEjerlav() (*Ejerlav, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Ejerlav{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Ejerlav), nil
}
// NextAdgangsAdresse will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextAdgangsAdresse() (*AdgangsAdresse, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&AdgangsAdresse{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*AdgangsAdresse), nil
}
// NextAdresse will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextAdresse() (*Adresse, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Adresse{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Adresse), nil
}
// NextPostnummer will return the next item.
// The query must be built using the corresponding type. See NewListQuery() function.
func (a *ListIter) NextPostnummer() (*Postnummer, error) {
if !a.eType.ConvertibleTo(reflect.TypeOf(&Postnummer{})) {
return nil, fmt.Errorf("Wrong type requested from iterator. Expected %s", a.eType.String())
}
item, err := a.Next()
if err != nil {
return nil, a.err
}
return item.(*Postnummer), nil
}
// NewReverseQuery will create a reverse location to item lookup. Parameters are:
//
// * listType: See NewListQuery() for valid options.
// * x: X koordinat. (Hvis ETRS89/UTM32 anvendes angives øst-værdien.) Hvis WGS84/geografisk anvendex angives bredde-værdien.
// * y: Y koordinat. (Hvis ETRS89/UTM32 anvendes angives nord-værdien.) Hvis WGS84/geografisk anvendex angives længde-værdien.
// * srid: Angiver SRID for det koordinatsystem, som geospatiale parametre er angivet i. Default er 4326 (WGS84). Leave this empty for default value
//
// See examples/query-list-reverse.go for usage example
//
// An iterator will be returned, but it will only contain zero or one values.
func NewReverseQuery(listType string, x, y float64, srid string) (*ListIter, error) {
path := "/" + listType + "/reverse"
q := &ListQuery{listType: listType, queryGeoJSON: queryGeoJSON{query: query{host: DefaultHost, path: path}}}
typ := q.Type()
if typ == nil {
return nil, fmt.Errorf("unknown list type '%s'", listType)
}
q.add(&textQuery{Name: "x", Values: []string{strconv.FormatFloat(x, 'f', -1, 64)}, Multi: false, Null: false})
q.add(&textQuery{Name: "y", Values: []string{strconv.FormatFloat(y, 'f', -1, 64)}, Multi: false, Null: false})
if srid != "" {
q.add(&textQuery{Name: "srid", Values: []string{srid}, Multi: false, Null: false})
}
// Execute request
resp, err := q.NoFormat().Request()
if err != nil {
return nil, err
}
defer resp.Close()
// We create the iterator and fill it with data.
ret := &ListIter{}
ret.eType = reflect.TypeOf(typ)
ret.a = makeChannel(ret.eType, reflect.BothDir, 1)
all, err := ioutil.ReadAll(resp)
if err != nil {
return nil, err
}
// Decode to typ
err = json.Unmarshal(all, typ)
if err != nil {
return nil, err
}
ret.a.Send(reflect.ValueOf(typ))
ret.a.Close()
ret.err = io.EOF
return ret, nil
}