This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
/
sfomuseum.go
147 lines (113 loc) · 3.52 KB
/
sfomuseum.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
package sfomuseum
import (
_ "gocloud.dev/blob/fileblob"
)
import (
"context"
"encoding/json"
"errors"
"github.com/sfomuseum/go-lookup"
_ "github.com/sfomuseum/go-lookup-blob"
_ "github.com/sfomuseum/go-lookup-git"
"github.com/sfomuseum/go-lookup/catalog"
"github.com/sfomuseum/go-lookup/iterator"
"github.com/tidwall/pretty"
"net/url"
)
const SFOMUSEUM_DATA_PUBLICART string = "https://github.com/sfomuseum-data/sfomuseum-data-publicart.git"
const SFOMUSEUM_DATA_EXHIBITION string = "https://github.com/sfomuseum-data/sfomuseum-data-exhibition.git"
const SFOMUSEUM_DATA_MEDIA string = "https://github.com/sfomuseum-data/sfomuseum-data-media.git"
const SFOMUSEUM_DATA_ARCHITECTURE string = "https://github.com/sfomuseum-data/sfomuseum-data-architecture.git"
const SFOMUSEUM_DATA_WHOSONFIRST string = "https://github.com/sfomuseum-data/sfomuseum-data-whosonfirst.git"
const SFOMUSEUM_DATA_ENTERPRISE string = "https://github.com/sfomuseum-data/sfomuseum-data-enterprise.git"
const SFOMUSEUM_DATA_FLIGHTS string = "https://github.com/sfomuseum-data/sfomuseum-data-flights-%s.git"
const SFOMUSEUM_DATA_FAA string = "https://github.com/sfomuseum-data/sfomuseum-data-faa-%s.git"
type CatalogOptions struct {
Catalog catalog.Catalog
AppendFuncs []iterator.AppendLookupFunc
Iterators []iterator.Iterator
}
func DefaultCatalogOptions() (*CatalogOptions, error) {
ctx := context.Background()
c, err := catalog.NewCatalog(ctx, "syncmap://")
if err != nil {
return nil, err
}
funcs := make([]iterator.AppendLookupFunc, 0)
lookers := make([]iterator.Iterator, 0)
opts := &CatalogOptions{
Catalog: c,
AppendFuncs: funcs,
Iterators: lookers,
}
return opts, nil
}
func NewIteratorURI(scheme string, lu_scheme string, uri string) string {
u := url.URL{}
u.Scheme = scheme
u.Host = lu_scheme
p := url.Values{}
p.Set("uri", uri)
u.RawQuery = p.Encode()
return u.String()
}
func NewCatalog(ctx context.Context, uri string) (catalog.Catalog, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
var opts *CatalogOptions
var opts_err error
switch u.Scheme {
case "airlines":
opts, opts_err = DefaultAirlinesCatalogOptions()
case "airports":
opts, opts_err = DefaultAirportsCatalogOptions()
case "faa":
opts, opts_err = DefaultFAACatalogOptions()
case "flights":
opts, opts_err = DefaultFlightsCatalogOptions()
case "gates":
opts, opts_err = DefaultGatesCatalogOptions()
case "publicart":
opts, opts_err = DefaultPublicArtCatalogOptions()
case "exhibitions":
opts, opts_err = DefaultExhibitionsCatalogOptions()
case "images":
opts, opts_err = DefaultMediaImagesCatalogOptions()
default:
return nil, errors.New("Unsupported iterator")
}
if opts_err != nil {
return nil, opts_err
}
q := u.Query()
lu_uri := q.Get("uri")
lu, err := iterator.NewIterator(ctx, lu_uri)
if err != nil {
return nil, err
}
opts.Iterators = append(opts.Iterators, lu)
return NewCatalogWithOptions(ctx, opts)
}
func NewCatalogWithOptions(ctx context.Context, opts *CatalogOptions) (catalog.Catalog, error) {
err := lookup.SeedCatalog(ctx, opts.Catalog, opts.Iterators, opts.AppendFuncs)
if err != nil {
return nil, err
}
return opts.Catalog, nil
}
func MarshalCatalog(c catalog.Catalog) ([]byte, error) {
lookup := make(map[string]interface{})
c.Range(func(key interface{}, value interface{}) bool {
gate_name := key.(string)
lookup[gate_name] = value
return true
})
body, err := json.Marshal(lookup)
if err != nil {
return nil, err
}
body = pretty.Pretty(body)
return body, nil
}