From 0373b452a9364057f6fe7a6c1a5092987132b6db Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Thu, 30 Sep 2021 13:59:09 -0700 Subject: [PATCH] add FindCurrentGate (and related) methods --- gates/gates.go | 72 +++++++++++++++++++++++++++++++++++++++++++++ gates/gates_test.go | 28 ++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 gates/gates_test.go diff --git a/gates/gates.go b/gates/gates.go index 449376a..557ed3e 100644 --- a/gates/gates.go +++ b/gates/gates.go @@ -2,7 +2,9 @@ package gates import ( + "context" "fmt" + "github.com/sfomuseum/go-sfomuseum-architecture" ) // type Gate is a struct representing a passenger gate at SFO. @@ -19,3 +21,73 @@ type Gate struct { func (g *Gate) String() string { return fmt.Sprintf("%d %s (%d)", g.WhosOnFirstId, g.Name, g.IsCurrent) } + +// Return the current Gate matching 'code'. Multiple matches throw an error. +func FindCurrentGate(ctx context.Context, code string) (*Gate, error) { + + lookup, err := NewLookup(ctx, "") + + if err != nil { + return nil, fmt.Errorf("Failed to create new lookup, %w", err) + } + + return FindCurrentGateWithLookup(ctx, lookup, code) +} + +// Return the current Gate matching 'code' with a custom architecture.Lookup instance. Multiple matches throw an error. +func FindCurrentGateWithLookup(ctx context.Context, lookup architecture.Lookup, code string) (*Gate, error) { + + current, err := FindGatesCurrentWithLookup(ctx, lookup, code) + + if err != nil { + return nil, err + } + + switch len(current) { + case 0: + return nil, fmt.Errorf("No matches for %s", code) + case 1: + return current[0], nil + default: + return nil, fmt.Errorf("Multiple matches for %s (%v)", code, current) + } + +} + +// Returns all Gate instances matching 'code' that are marked as current. +func FindGatesCurrent(ctx context.Context, code string) ([]*Gate, error) { + + lookup, err := NewLookup(ctx, "") + + if err != nil { + return nil, fmt.Errorf("Failed to create new lookup, %w", err) + } + + return FindGatesCurrentWithLookup(ctx, lookup, code) +} + +// Returns all Gate instances matching 'code' that are marked as current with a custom architecture.Lookup instance. +func FindGatesCurrentWithLookup(ctx context.Context, lookup architecture.Lookup, code string) ([]*Gate, error) { + + rsp, err := lookup.Find(ctx, code) + + if err != nil { + return nil, fmt.Errorf("Failed to find %s, %w", code, err) + } + + current := make([]*Gate, 0) + + for _, r := range rsp { + + g := r.(*Gate) + + // if g.IsCurrent == 0 { + if g.IsCurrent != 1 { + continue + } + + current = append(current, g) + } + + return current, nil +} diff --git a/gates/gates_test.go b/gates/gates_test.go new file mode 100644 index 0000000..b3e21c9 --- /dev/null +++ b/gates/gates_test.go @@ -0,0 +1,28 @@ +package gates + +import ( + "context" + "testing" +) + +func TestFindCurrentGate(t *testing.T) { + + tests := map[string]int64{ + "A9": 1745882385, + } + + ctx := context.Background() + + for code, id := range tests { + + g, err := FindCurrentGate(ctx, code) + + if err != nil { + t.Fatalf("Failed to find current gate for %s, %v", code, err) + } + + if g.WhosOnFirstId != id { + t.Fatalf("Unexpected ID for gate %s. Got %d but expected %d", code, g.WhosOnFirstId, id) + } + } +}