From 9f6523b3bdd7793745e8e243d6a05d95ad7094eb Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Tue, 28 Sep 2021 11:07:31 -0700 Subject: [PATCH] add NewLookupFuncWithGalleries method --- galleries/compile.go | 8 ++++---- galleries/lookup.go | 39 +++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/galleries/compile.go b/galleries/compile.go index 4213275..eeefd6b 100644 --- a/galleries/compile.go +++ b/galleries/compile.go @@ -14,9 +14,9 @@ import ( // CompileGalleriesData will generate a list of `Gallery` 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 CompileGalleriesData(ctx context.Context, iterator_uri string, iterator_sources ...string) ([]Gallery, error) { +func CompileGalleriesData(ctx context.Context, iterator_uri string, iterator_sources ...string) ([]*Gallery, error) { - lookup := make([]Gallery, 0) + lookup := make([]*Gallery, 0) mu := new(sync.RWMutex) iter_cb := func(ctx context.Context, fh io.ReadSeeker, args ...interface{}) error { @@ -70,7 +70,7 @@ func CompileGalleriesData(ctx context.Context, iterator_uri string, iterator_sou inception_rsp := gjson.GetBytes(body, "properties.edtf:inception") cessation_rsp := gjson.GetBytes(body, "properties.edtf:cessation") - a := Gallery{ + g := &Gallery{ WhosOnFirstId: wofid_rsp.Int(), SFOMuseumId: sfomid_rsp.Int(), MapId: mapid_rsp.String(), @@ -80,7 +80,7 @@ func CompileGalleriesData(ctx context.Context, iterator_uri string, iterator_sou } mu.Lock() - lookup = append(lookup, a) + lookup = append(lookup, g) mu.Unlock() return nil diff --git a/galleries/lookup.go b/galleries/lookup.go index 621d0ea..ba83345 100644 --- a/galleries/lookup.go +++ b/galleries/lookup.go @@ -1,7 +1,6 @@ package galleries import ( - "bytes" "context" "encoding/json" "fmt" @@ -53,20 +52,30 @@ func NewLookup(ctx context.Context, uri string) (architecture.Lookup, error) { // 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) GalleriesLookupFunc { - lookup_func := func(ctx context.Context) { + defer r.Close() - defer r.Close() + var galleries_list []*Gallery - var galleries_list []*Gallery + dec := json.NewDecoder(r) + err := dec.Decode(&galleries_list) - dec := json.NewDecoder(r) - err := dec.Decode(&galleries_list) + if err != nil { - if err != nil { + lookup_func := func(ctx context.Context) { lookup_init_err = err - return } + return lookup_func + } + + return NewLookupFuncWithGalleries(ctx, galleries_list) +} + +// NewLookup will return an `GalleriesLookupFunc` function instance that, when invoked, will populate an `architecture.Lookup` instance with data stored in `galleries_list`. +func NewLookupFuncWithGalleries(ctx context.Context, galleries_list []*Gallery) GalleriesLookupFunc { + + lookup_func := func(ctx context.Context) { + table := new(sync.Map) for _, data := range galleries_list { @@ -106,23 +115,13 @@ func NewLookupWithLookupFunc(ctx context.Context, lookup_func GalleriesLookupFun func NewLookupFromIterator(ctx context.Context, iterator_uri string, iterator_sources ...string) (architecture.Lookup, error) { - galleries_data, err := CompileGalleriesData(ctx, iterator_uri, iterator_sources...) + galleries_list, err := CompileGalleriesData(ctx, iterator_uri, iterator_sources...) if err != nil { return nil, fmt.Errorf("Failed to compile galleries data, %w", err) } - // necessary until there is a NewLookupFuncWithGalleries method - enc_data, err := json.Marshal(galleries_data) - - if err != nil { - return nil, fmt.Errorf("Failed to marshal galleries data, %w", err) - } - - r := bytes.NewReader(enc_data) - rc := io.NopCloser(r) - - lookup_func := NewLookupFuncWithReader(ctx, rc) + lookup_func := NewLookupFuncWithGalleries(ctx, galleries_list) return NewLookupWithLookupFunc(ctx, lookup_func) }