Skip to content

Commit

Permalink
feat: enable migrate msg to be either a struct or an enum (#74)
Browse files Browse the repository at this point in the history
* feature: enable migrate msg to be either a struct or an enum

* chore: ran gofumpt
  • Loading branch information
kakucodes authored Jun 5, 2024
1 parent c76a8fb commit cb68050
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions pkg/codegen/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func GenerateMigrateMsg(f *jen.File, schema *schemas.JSONSchema) {
return
}

generateStructMsg(f, schema, "MigrateMsg")
// MigrateMsg can be either a struct or an enum
generateStructOrEnumMsg(f, schema, "MigrateMsg")
}

func generateStructMsg(f *jen.File, schema *schemas.JSONSchema, allowedTitle string) {
Expand All @@ -46,11 +47,30 @@ func generateStructMsg(f *jen.File, schema *schemas.JSONSchema, allowedTitle str

func validateAsStructMsg(schema *schemas.JSONSchema, allowedTitle string) error {
if schema.Title != allowedTitle {
return fmt.Errorf("title must be InstantiateMsg")
return fmt.Errorf("title must be %v", allowedTitle)
}

if schema.Type == nil {
return fmt.Errorf("%v type must be defined", allowedTitle)
}

if schema.Type[0] != "object" {
return fmt.Errorf("InstantiateMsg type must be object")
return fmt.Errorf("%v type must be object", allowedTitle)
}

return nil
}

// Generate the msg regardless if it is a struct or enum this is mostly needed for migration msgs
func generateStructOrEnumMsg(f *jen.File, schema *schemas.JSONSchema, allowedTitle string) {
if schema == nil {
panic(fmt.Errorf("schema of %s is nil", allowedTitle))
}

err := validateAsStructMsg(schema, allowedTitle)
if err == nil {
generateStructMsg(f, schema, allowedTitle)
} else {
generateEnumMsg(f, schema, []string{allowedTitle, allowedTitle + "_for_Empty"})
}
}

0 comments on commit cb68050

Please sign in to comment.