Skip to content

Commit

Permalink
add FindCurrentGate (and related) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Sep 30, 2021
1 parent 1036c96 commit 0373b45
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
72 changes: 72 additions & 0 deletions gates/gates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
28 changes: 28 additions & 0 deletions gates/gates_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 0373b45

Please sign in to comment.