/
main.go
130 lines (92 loc) · 3.21 KB
/
main.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
// ensure-properties iterates over a collection of Who's On First records and ensures that there is a corresponding properties JSON file.
package main
import (
"context"
"flag"
"fmt"
"github.com/sfomuseum/go-sfomuseum-whosonfirst/custom"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/iterator"
"github.com/whosonfirst/go-whosonfirst-uri"
"github.com/whosonfirst/go-writer/v3"
"io"
"log"
"os"
)
func main() {
iter_uri := flag.String("iterator-uri", "repo://", "A valid whosonfirst/go-whosonfirst-iterate/v2 URI.")
properties_reader_uri := flag.String("properties-reader-uri", "fs:///usr/local/data/sfomuseum-data-whosonfirst/properties", "A valid whosonfirst/go-reader.Reader URI.")
properties_writer_uri := flag.String("properties-writer-uri", "fs:///usr/local/data/sfomuseum-data-whosonfirst/properties", "A valid whosonfirst/go-writer.Writer URI.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "ensure-properties iterates over a collection of Who's On First records and ensures that there is a corresponding properties JSON file.\n")
fmt.Fprintf(os.Stderr, "Usage:\n\t %s [options] uri(N) uri(N)\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
uris := flag.Args()
ctx := context.Background()
props_wr, err := writer.NewWriter(ctx, *properties_writer_uri)
if err != nil {
log.Fatalf("Failed to create new writer, %v", err)
}
props_r, err := reader.NewReader(ctx, *properties_reader_uri)
if err != nil {
log.Fatalf("Failed to create reader, %v", err)
}
iter_cb := func(ctx context.Context, path string, fh io.ReadSeeker, args ...interface{}) error {
id, uri_args, err := uri.ParseURI(path)
if err != nil {
return fmt.Errorf("Failed to parse '%s', %v", path, err)
}
if uri_args.IsAlternate {
return nil
}
body, err := io.ReadAll(fh)
if err != nil {
return fmt.Errorf("Failed to read '%s', %v", path, err)
}
props_map, err := custom.EnsureCustomProperties(ctx, props_r, props_wr, id)
if err != nil {
return fmt.Errorf("Failed to load custom properties for for '%d', %v", id, err)
}
has_updates := false
_, repo_ok := props_map["sfomuseum:repo"]
if !repo_ok {
props_map["sfomuseum:repo"] = "sfomuseum-data-whosonfirst"
has_updates = true
}
_, placetype_ok := props_map["sfomuseum:placetype"]
if !placetype_ok {
pt_rsp := gjson.GetBytes(body, "properties.wof:placetype")
if !pt_rsp.Exists() {
return fmt.Errorf("Failed to derive wof:placetype for '%s'", path)
}
switch pt_rsp.String() {
case "campus":
props_map["sfomuseum:placetype"] = "airport"
case "locality":
props_map["sfomuseum:placetype"] = "city"
default:
props_map["sfomuseum:placetype"] = pt_rsp.String()
}
has_updates = true
}
if !has_updates {
return nil
}
err = custom.WriteCustomProperties(ctx, props_wr, id, props_map)
if err != nil {
return fmt.Errorf("Failed to write custom properties for %d, %v", id, err)
}
return nil
}
iter, err := iterator.NewIterator(ctx, *iter_uri, iter_cb)
if err != nil {
log.Fatalf("Failed to create new iterator, %v", err)
}
err = iter.IterateURIs(ctx, uris...)
if err != nil {
log.Fatalf("Failed to iterate URIs, %v", err)
}
}