Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: enable migrate msg to be either a struct or an enum #74

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"})
}
}
Loading