Skip to content

Commit

Permalink
add NewLookupFuncWithGalleries method
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Sep 28, 2021
1 parent bdcbc20 commit 9f6523b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
8 changes: 4 additions & 4 deletions galleries/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand Down
39 changes: 19 additions & 20 deletions galleries/lookup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package galleries

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 9f6523b

Please sign in to comment.