Skip to content

Commit

Permalink
add FindCurrent methods and NotFound, MultipleCandidates errors for g…
Browse files Browse the repository at this point in the history
…alleries
  • Loading branch information
thisisaaronland committed Oct 1, 2021
1 parent e94af64 commit 8aa0076
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 3 deletions.
45 changes: 45 additions & 0 deletions galleries/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package galleries

import (
"fmt"
)

type NotFound struct{ code string }

func (e NotFound) Error() string {
return fmt.Sprintf("Gallery '%s' not found", e.code)
}

func (e NotFound) String() string {
return e.Error()
}

type MultipleCandidates struct{ code string }

func (e MultipleCandidates) Error() string {
return fmt.Sprintf("Multiple candidates for gallery '%s'", e.code)
}

func (e MultipleCandidates) String() string {
return e.Error()
}

func IsNotFound(e error) bool {

switch e.(type) {
case NotFound, *NotFound:
return true
default:
return false
}
}

func IsMultipleCandidates(e error) bool {

switch e.(type) {
case MultipleCandidates, *MultipleCandidates:
return true
default:
return false
}
}
32 changes: 32 additions & 0 deletions galleries/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package galleries

import (
_ "fmt"
"testing"
)

func TestGalleriesNotFound(t *testing.T) {

e := NotFound{"D16"}

if !IsNotFound(e) {
t.Fatalf("Expected NotFound error")
}

if e.String() != "Gallery 'D16' not found" {
t.Fatalf("Invalid stringification")
}
}

func TestGalleriesMultipleCandidates(t *testing.T) {

e := MultipleCandidates{"D16"}

if !IsMultipleCandidates(e) {
t.Fatalf("Expected MultipleCandidates error")
}

if e.String() != "Multiple candidates for gallery 'D16'" {
t.Fatalf("Invalid stringification")
}
}
74 changes: 73 additions & 1 deletion galleries/galleries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package galleries

import (
"context"
"fmt"
"github.com/sfomuseum/go-sfomuseum-architecture"
)

// type Gallery is a struct representing a passenger gallery at SFO.
Expand All @@ -19,11 +21,81 @@ type Gallery struct {
Inception string `json:"edtf:inception"`
// The (EDTF) cessation date for the gallery
Cessation string `json:"edtf:cessation"`
// A Who's On First "existential" (`KnownUnknownFlag`) flag signaling the gate's status
// A Who's On First "existential" (`KnownUnknownFlag`) flag signaling the gallery's status
IsCurrent int64 `json:"mz:is_current"`
}

// String() will return the name of the gallery.
func (g *Gallery) String() string {
return fmt.Sprintf("%d#%d %s-%s %s", g.WhosOnFirstId, g.SFOMuseumId, g.Inception, g.Cessation, g.Name)
}

// Return the current Gallery matching 'code'. Multiple matches throw an error.
func FindCurrentGallery(ctx context.Context, code string) (*Gallery, error) {

lookup, err := NewLookup(ctx, "")

if err != nil {
return nil, fmt.Errorf("Failed to create new lookup, %w", err)
}

return FindCurrentGalleryWithLookup(ctx, lookup, code)
}

// Return the current Gallery matching 'code' with a custom architecture.Lookup instance. Multiple matches throw an error.
func FindCurrentGalleryWithLookup(ctx context.Context, lookup architecture.Lookup, code string) (*Gallery, error) {

current, err := FindGalleriesCurrentWithLookup(ctx, lookup, code)

if err != nil {
return nil, err
}

switch len(current) {
case 0:
return nil, NotFound{code}
case 1:
return current[0], nil
default:
return nil, MultipleCandidates{code}
}

}

// Returns all Gallery instances matching 'code' that are marked as current.
func FindGalleriesCurrent(ctx context.Context, code string) ([]*Gallery, error) {

lookup, err := NewLookup(ctx, "")

if err != nil {
return nil, fmt.Errorf("Failed to create new lookup, %w", err)
}

return FindGalleriesCurrentWithLookup(ctx, lookup, code)
}

// Returns all Gallery instances matching 'code' that are marked as current with a custom architecture.Lookup instance.
func FindGalleriesCurrentWithLookup(ctx context.Context, lookup architecture.Lookup, code string) ([]*Gallery, error) {

rsp, err := lookup.Find(ctx, code)

if err != nil {
return nil, fmt.Errorf("Failed to find %s, %w", code, err)
}

current := make([]*Gallery, 0)

for _, r := range rsp {

g := r.(*Gallery)

// if g.IsCurrent == 0 {
if g.IsCurrent != 1 {
continue
}

current = append(current, g)
}

return current, nil
}
28 changes: 28 additions & 0 deletions galleries/galleries_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package galleries

import (
"context"
"testing"
)

func TestFindCurrentGallery(t *testing.T) {

tests := map[string]int64{
"D16": 1745882461,
}

ctx := context.Background()

for code, id := range tests {

g, err := FindCurrentGallery(ctx, code)

if err != nil {
t.Fatalf("Failed to find current gallery for %s, %v", code, err)
}

if g.WhosOnFirstId != id {
t.Fatalf("Unexpected ID for gallery %s. Got %d but expected %d", code, g.WhosOnFirstId, id)
}
}
}
6 changes: 4 additions & 2 deletions galleries/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"testing"
)

func TestGatesLookup(t *testing.T) {
func TestGalleriesLookup(t *testing.T) {

wofid_tests := map[string]int64{}
wofid_tests := map[string]int64{
"D16": 1745882461,
}

ctx := context.Background()

Expand Down

0 comments on commit 8aa0076

Please sign in to comment.