Skip to content

Commit

Permalink
fix(orgs): droppped typed plans
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Sep 17, 2024
1 parent d880ac0 commit f923894
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 135 deletions.
60 changes: 3 additions & 57 deletions axiom/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,7 @@ import (
"go.opentelemetry.io/otel/trace"
)

//go:generate go run golang.org/x/tools/cmd/stringer -type=Plan,PaymentStatus -linecomment -output=orgs_string.go

// Plan represents the plan of an [Organization].
type Plan uint8

// All available [Organization] [Plan]s.
const (
emptyPlan Plan = iota //

Personal // personal
Basic // basic
Team // teamMonthly
Enterprise // enterprise
Comped // comped
)

func planFromString(s string) (plan Plan, err error) {
switch s {
case emptyPlan.String():
plan = emptyPlan
case Personal.String():
plan = Personal
case Basic.String():
plan = Basic
case Team.String():
plan = Team
case Enterprise.String():
plan = Enterprise
case Comped.String():
plan = Comped
default:
err = fmt.Errorf("unknown plan %q", s)
}

return plan, err
}

// MarshalJSON implements [json.Marshaler]. It is in place to marshal the plan
// to its string representation because that's what the server expects.
func (p Plan) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}

// UnmarshalJSON implements [json.Unmarshaler]. It is in place to unmarshal the
// plan from the string representation the server returns.
func (p *Plan) UnmarshalJSON(b []byte) (err error) {
var s string
if err = json.Unmarshal(b, &s); err != nil {
return err
}

*p, err = planFromString(s)

return err
}
//go:generate go run golang.org/x/tools/cmd/stringer -type=PaymentStatus -linecomment -output=orgs_string.go

// PaymentStatus represents the payment status of an [Organization].
type PaymentStatus uint8
Expand Down Expand Up @@ -135,7 +81,7 @@ type License struct {
// ExpiresAt is the time the license expires.
ExpiresAt time.Time `json:"expiresAt"`
// Plan associated with the license.
Plan Plan `json:"tier"`
Plan string `json:"tier"`
// MonthlyIngestGB is the monthly amount of data in gigabytes that can be
// ingested as part of the license.
MonthlyIngestGB uint64 `json:"monthlyIngestGb"`
Expand Down Expand Up @@ -199,7 +145,7 @@ type Organization struct {
// Trial describes if the plan is trialed or not.
Trial bool `json:"inTrial"`
// Plan the organization is on.
Plan Plan `json:"plan"`
Plan string `json:"plan"`
// PlanCreated is the time the plan was created.
PlanCreated time.Time `json:"planCreated"`
// LastUsageSync is the last time the usage instance usage statistics were
Expand Down
24 changes: 1 addition & 23 deletions axiom/orgs_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 7 additions & 55 deletions axiom/orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestOrganizationsService_List(t *testing.T) {
ID: "axiom",
Name: "Axiom Industries Ltd",
Slug: "",
Plan: Basic,
Plan: "enterprise",
PlanCreated: testhelper.MustTimeParse(t, time.RFC3339, "1970-01-01T00:00:00Z"),
Trial: false,
LastUsageSync: testhelper.MustTimeParse(t, time.RFC3339, "0001-01-01T00:00:00Z"),
Expand All @@ -33,7 +33,7 @@ func TestOrganizationsService_List(t *testing.T) {
IssuedAt: testhelper.MustTimeParse(t, time.RFC3339, "2021-01-19T17:55:53Z"),
ValidFrom: testhelper.MustTimeParse(t, time.RFC3339, "2021-01-19T17:55:53Z"),
ExpiresAt: testhelper.MustTimeParse(t, time.RFC3339, "2022-01-19T17:55:53Z"),
Plan: Enterprise,
Plan: "enterprise",
MonthlyIngestGB: 100,
MaxUsers: 50,
MaxTeams: 10,
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestOrganizationsService_List(t *testing.T) {
"name": "Axiom Industries Ltd",
"slug": "",
"inTrial": false,
"plan": "basic",
"plan": "enterprise",
"planCreated": "1970-01-01T00:00:00Z",
"lastUsageSync": "0001-01-01T00:00:00Z",
"role": "admin",
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestOrganizationsService_Get(t *testing.T) {
ID: "axiom",
Name: "Axiom Industries Ltd",
Slug: "",
Plan: Basic,
Plan: "enterprise",
PlanCreated: testhelper.MustTimeParse(t, time.RFC3339, "1970-01-01T00:00:00Z"),
Trial: false,
LastUsageSync: testhelper.MustTimeParse(t, time.RFC3339, "0001-01-01T00:00:00Z"),
Expand All @@ -124,7 +124,7 @@ func TestOrganizationsService_Get(t *testing.T) {
IssuedAt: testhelper.MustTimeParse(t, time.RFC3339, "2021-01-19T17:55:53Z"),
ValidFrom: testhelper.MustTimeParse(t, time.RFC3339, "2021-01-19T17:55:53Z"),
ExpiresAt: testhelper.MustTimeParse(t, time.RFC3339, "2022-01-19T17:55:53Z"),
Plan: Enterprise,
Plan: "enterprise",
MonthlyIngestGB: 100,
MaxUsers: 50,
MaxTeams: 10,
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestOrganizationsService_Get(t *testing.T) {
"name": "Axiom Industries Ltd",
"slug": "",
"inTrial": false,
"plan": "basic",
"plan": "enterprise",
"planCreated": "1970-01-01T00:00:00Z",
"lastUsageSync": "0001-01-01T00:00:00Z",
"role": "admin",
Expand Down Expand Up @@ -194,54 +194,6 @@ func TestOrganizationsService_Get(t *testing.T) {
assert.Equal(t, exp, res)
}

func TestPlan_Marshal(t *testing.T) {
exp := `{
"plan": "personal"
}`

b, err := json.Marshal(struct {
Plan Plan `json:"plan"`
}{
Plan: Personal,
})
require.NoError(t, err)
require.NotEmpty(t, b)

assert.JSONEq(t, exp, string(b))
}

func TestPlan_Unmarshal(t *testing.T) {
var act struct {
Plan Plan `json:"plan"`
}
err := json.Unmarshal([]byte(`{ "plan": "personal" }`), &act)
require.NoError(t, err)

assert.Equal(t, Personal, act.Plan)
}

func TestPlan_String(t *testing.T) {
// Check outer bounds.
assert.Empty(t, Plan(0).String())
assert.Empty(t, emptyPlan.String())
assert.Equal(t, emptyPlan, Plan(0))
assert.Contains(t, (Comped + 1).String(), "Plan(")

for p := Personal; p <= Comped; p++ {
s := p.String()
assert.NotEmpty(t, s)
assert.NotContains(t, s, "Plan(")
}
}

func TestPlanFromString(t *testing.T) {
for p := Personal; p <= Comped; p++ {
parsed, err := planFromString(p.String())
assert.NoError(t, err)
assert.Equal(t, p, parsed)
}
}

func TestPaymentStatus_Marshal(t *testing.T) {
exp := `{
"paymentStatus": "success"
Expand Down Expand Up @@ -293,7 +245,7 @@ func TestPaymentStatusFromString(t *testing.T) {
func TestLicense(t *testing.T) {
exp := License{
ID: "98baf1f7-0b51-403f-abc1-2ee91972a225",
Plan: Personal,
Plan: "personal",
MaxUsers: 50,
MaxTeams: 10,
MaxDatasets: 25,
Expand Down

0 comments on commit f923894

Please sign in to comment.