Skip to content

Commit

Permalink
add NewLookupFuncWithGates method
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Sep 28, 2021
1 parent 9f6523b commit 8ed85f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
8 changes: 4 additions & 4 deletions gates/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
// CompileGatesData will generate a list of `Gate` struct to be used as the source data for an `SFOMuseumLookup` instance.
// The list of gate are compiled by iterating over one or more source. `iterator_uri` is a valid `whosonfirst/go-whosonfirst-iterate` URI
// and `iterator_sources` are one more (iterator) URIs to process.
func CompileGatesData(ctx context.Context, iterator_uri string, iterator_sources ...string) ([]Gate, error) {
func CompileGatesData(ctx context.Context, iterator_uri string, iterator_sources ...string) ([]*Gate, error) {

lookup := make([]Gate, 0)
lookup := make([]*Gate, 0)
mu := new(sync.RWMutex)

iter_cb := func(ctx context.Context, fh io.ReadSeeker, args ...interface{}) error {
Expand Down Expand Up @@ -54,13 +54,13 @@ func CompileGatesData(ctx context.Context, iterator_uri string, iterator_sources
wof_id := whosonfirst.Id(f)
name := whosonfirst.Name(f)

a := Gate{
g := &Gate{
WhosOnFirstId: wof_id,
Name: name,
}

mu.Lock()
lookup = append(lookup, a)
lookup = append(lookup, g)
mu.Unlock()

return nil
Expand Down
41 changes: 20 additions & 21 deletions gates/lookup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gates

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -48,25 +47,35 @@ func NewLookup(ctx context.Context, uri string) (architecture.Lookup, error) {
return NewLookupWithLookupFunc(ctx, lookup_func)
}

// NewLookup will return an `GatesLookupFunc` function instance that, when invoked, will populate an `architecture.Lookup` instance with data stored in `r`.
// NewLookupWithReader will return an `GatesLookupFunc` function instance that, when invoked, will populate an `architecture.Lookup` instance with data stored in `r`.
// `r` will be closed when the `GatesLookupFunc` function instance is invoked.
// It is assumed that the data in `r` will be formatted in the same way as the procompiled (embedded) data stored in `data/sfomuseum.json`.
func NewLookupFuncWithReader(ctx context.Context, r io.ReadCloser) GatesLookupFunc {

lookup_func := func(ctx context.Context) {
defer r.Close()

defer r.Close()
var gates_list []*Gate

var gates_list []*Gate
dec := json.NewDecoder(r)
err := dec.Decode(&gates_list)

dec := json.NewDecoder(r)
err := dec.Decode(&gates_list)
if err != nil {

if err != nil {
lookup_func := func(ctx context.Context) {
lookup_init_err = err
return
}

return lookup_func
}

return NewLookupFuncWithGates(ctx, gates_list)
}

// NewLookupFuncWithGates will return an `GatesLookupFunc` function instance that, when invoked, will populate an `architecture.Lookup` instance with data stored in `gates_list`.
func NewLookupFuncWithGates(ctx context.Context, gates_list []*Gate) GatesLookupFunc {

lookup_func := func(ctx context.Context) {

table := new(sync.Map)

for _, data := range gates_list {
Expand Down Expand Up @@ -106,23 +115,13 @@ func NewLookupWithLookupFunc(ctx context.Context, lookup_func GatesLookupFunc) (

func NewLookupFromIterator(ctx context.Context, iterator_uri string, iterator_sources ...string) (architecture.Lookup, error) {

gates_data, err := CompileGatesData(ctx, iterator_uri, iterator_sources...)
gates_list, err := CompileGatesData(ctx, iterator_uri, iterator_sources...)

if err != nil {
return nil, fmt.Errorf("Failed to compile gates data, %w", err)
}

// necessary until there is a NewLookupFuncWithGates method
enc_data, err := json.Marshal(gates_data)

if err != nil {
return nil, fmt.Errorf("Failed to marshal gates data, %w", err)
}

r := bytes.NewReader(enc_data)
rc := io.NopCloser(r)

lookup_func := NewLookupFuncWithReader(ctx, rc)
lookup_func := NewLookupFuncWithGates(ctx, gates_list)
return NewLookupWithLookupFunc(ctx, lookup_func)
}

Expand Down

0 comments on commit 8ed85f6

Please sign in to comment.