Skip to content

Commit

Permalink
feat: convert to type EnumArray[enumType ~string] []enumType
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenWadeOddball committed Nov 1, 2024
1 parent f714c1e commit b9caaa8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/graph/resolvers/model_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func ModelPlanShare(
modelPlanCategoriesHumainzed = append(modelPlanCategoriesHumainzed, models.ModelCategoryHumanized[*planBasics.ModelCategory])
}

for _, category := range planBasics.AdditionalModelCategories.Values {
for _, category := range planBasics.AdditionalModelCategories {
// Have to cast the additional category as a models.ModelCategory so we can fetch it from the models.ModelCategoryHumanized map
modelPlanCategoriesHumainzed = append(modelPlanCategoriesHumainzed, models.ModelCategoryHumanized[models.ModelCategory(category)])
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/graph/resolvers/plan_basics.resolvers.go

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

26 changes: 13 additions & 13 deletions pkg/models/enum_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ import (
"github.com/lib/pq"
)

type EnumArray[enumType ~string] struct {
Values []enumType
}
// type EnumArray[enumType ~string] struct {
// Values []enumType
// }

// type EnumArray[enumType ~string] []enumType
type EnumArray[enumType ~string] []enumType

// Scan Implements the Scanner interface to decode data from the database
// Scan implements the Scanner interface to decode data from the database
func (e *EnumArray[EnumType]) Scan(value interface{}) error {
var stringArray pq.StringArray
if err := stringArray.Scan(value); err != nil {
return err
}

e.Values = make([]EnumType, len(stringArray))
*e = make(EnumArray[EnumType], len(stringArray))
for i, v := range stringArray {
e.Values[i] = EnumType(v)
(*e)[i] = EnumType(v)
}
return nil
}

// Value Implements the Valuer interface to encode data into the database
// Value implements the Valuer interface to encode data into the database
func (e EnumArray[EnumType]) Value() (driver.Value, error) {
stringArray := make([]string, len(e.Values))
for i, v := range e.Values {
stringArray := make([]string, len(e))
for i, v := range e {
stringArray[i] = string(v)
}
return pq.StringArray(stringArray).Value()
}

// MarshalJSON marshals the EnumArray into JSON for GraphQL
func (e EnumArray[EnumType]) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Values)
return json.Marshal([]EnumType(e))
}

// UnmarshalJSON unmarshals the EnumArray from JSON for GraphQL
Expand All @@ -48,9 +48,9 @@ func (e *EnumArray[EnumType]) UnmarshalJSON(data []byte) error {
return err
}

e.Values = make([]EnumType, len(stringArray))
*e = make(EnumArray[EnumType], len(stringArray))
for i, v := range stringArray {
e.Values[i] = EnumType(v)
(*e)[i] = EnumType(v)
}
return nil
}

0 comments on commit b9caaa8

Please sign in to comment.