From aa90a441171346ba6f6c127795d2263ac67e0a06 Mon Sep 17 00:00:00 2001 From: William Perron Date: Fri, 23 Sep 2022 14:25:53 +0000 Subject: [PATCH] smol refactors --- claim_type.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ errors.go | 16 ++++++++++++++ store.go | 60 --------------------------------------------------- 3 files changed, 68 insertions(+), 60 deletions(-) create mode 100644 claim_type.go create mode 100644 errors.go diff --git a/claim_type.go b/claim_type.go new file mode 100644 index 0000000..3741ecc --- /dev/null +++ b/claim_type.go @@ -0,0 +1,52 @@ +package themis + +import "fmt" + +type ClaimType string + +func ClaimTypeFromString(s string) (ClaimType, error) { + switch s { + case CLAIM_TYPE_AREA: + return CLAIM_TYPE_AREA, nil + case CLAIM_TYPE_REGION: + return CLAIM_TYPE_REGION, nil + case CLAIM_TYPE_TRADE: + return CLAIM_TYPE_TRADE, nil + } + return "", fmt.Errorf("no claim type matching '%s'", s) +} + +func (ct ClaimType) String() string { + switch ct { + case CLAIM_TYPE_AREA: + return "Area" + case CLAIM_TYPE_REGION: + return "Region" + case CLAIM_TYPE_TRADE: + return "Trade Node" + } + return "" +} + +const ( + CLAIM_TYPE_AREA = "area" + CLAIM_TYPE_REGION = "region" + CLAIM_TYPE_TRADE = "trade" +) + +var claimTypeToColumn = map[ClaimType]string{ + CLAIM_TYPE_AREA: "area", + CLAIM_TYPE_REGION: "region", + CLAIM_TYPE_TRADE: "trade_node", +} + +type Claim struct { + ID int + Player string + Name string + Type ClaimType +} + +func (c Claim) String() string { + return fmt.Sprintf("id=%d player=%s claim_type=%s name=%s", c.ID, c.Player, c.Type, c.Name) +} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..3e906d8 --- /dev/null +++ b/errors.go @@ -0,0 +1,16 @@ +package themis + +import ( + "errors" + "fmt" +) + +var ErrNoSuchClaim = errors.New("no such claim") + +type ErrConflict struct { + Conflicts []Conflict +} + +func (ec ErrConflict) Error() string { + return fmt.Sprintf("found %d conflicting provinces", len(ec.Conflicts)) +} diff --git a/store.go b/store.go index b5c7a9c..8365540 100644 --- a/store.go +++ b/store.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" _ "embed" - "errors" "fmt" "strings" @@ -14,63 +13,6 @@ import ( //go:embed migrations/init.sql var initScript string -type ClaimType string - -func ClaimTypeFromString(s string) (ClaimType, error) { - switch s { - case CLAIM_TYPE_AREA: - return CLAIM_TYPE_AREA, nil - case CLAIM_TYPE_REGION: - return CLAIM_TYPE_REGION, nil - case CLAIM_TYPE_TRADE: - return CLAIM_TYPE_TRADE, nil - } - return "", fmt.Errorf("no claim type matching '%s'", s) -} - -func (ct ClaimType) String() string { - switch ct { - case CLAIM_TYPE_AREA: - return "Area" - case CLAIM_TYPE_REGION: - return "Region" - case CLAIM_TYPE_TRADE: - return "Trade Node" - } - return "" -} - -const ( - CLAIM_TYPE_AREA = "area" - CLAIM_TYPE_REGION = "region" - CLAIM_TYPE_TRADE = "trade" -) - -var claimTypeToColumn = map[ClaimType]string{ - CLAIM_TYPE_AREA: "area", - CLAIM_TYPE_REGION: "region", - CLAIM_TYPE_TRADE: "trade_node", -} - -type Claim struct { - ID int - Player string - Name string - Type ClaimType -} - -func (c Claim) String() string { - return fmt.Sprintf("id=%d player=%s claim_type=%s name=%s", c.ID, c.Player, c.Type, c.Name) -} - -type ErrConflict struct { - Conflicts []Conflict -} - -func (ec ErrConflict) Error() string { - return fmt.Sprintf("found %d conflicting provinces", len(ec.Conflicts)) -} - type Store struct { db *sql.DB } @@ -274,8 +216,6 @@ func (s *Store) DescribeClaim(ctx context.Context, ID int) (ClaimDetail, error) }, nil } -var ErrNoSuchClaim = errors.New("no such claim") - func (s *Store) DeleteClaim(ctx context.Context, ID int, userId string) error { stmt, err := s.db.PrepareContext(ctx, "DELETE FROM claims WHERE id = ? AND userid = ?") if err != nil {