From 8ed85f6a0a1688ca78dcbdfe444442887c4be5e9 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Tue, 28 Sep 2021 11:10:33 -0700 Subject: [PATCH] add NewLookupFuncWithGates method --- gates/compile.go | 8 ++++---- gates/lookup.go | 41 ++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/gates/compile.go b/gates/compile.go index 715d692..a9dea70 100644 --- a/gates/compile.go +++ b/gates/compile.go @@ -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 { @@ -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 diff --git a/gates/lookup.go b/gates/lookup.go index 7f9aa63..084c5c6 100644 --- a/gates/lookup.go +++ b/gates/lookup.go @@ -1,7 +1,6 @@ package gates import ( - "bytes" "context" "encoding/json" "fmt" @@ -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 { @@ -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) }