Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

sjpotter
Copy link

@sjpotter sjpotter commented May 22, 2025

Without an any, one can't pass an enum value to it, and if one tries a string representation of the enum, one ends up with code that will fail to compile as it will generate

var TypeDefault EnumType = "some string"

Without an any, one can pass an enum value to it, and if one tries a string representation of the enum, one ends up with code that will fail to compile as it will generate

var TypeDefault EnumType = "some string"
@sjpotter
Copy link
Author

sjpotter commented May 23, 2025

for some background, I am creating a field "scope"

func (DependencyResolution) Fields() []ent.Field {
	return []ent.Field{
		field.Bool("circular").Default(false),
		// "Direct" string used as a place holder, fixed with sed in generate_ent.sh
		field.Enum("scope").GoType(scope.Scope(0)).Default("Direct"),
	}
}

this generates the code

const DefaultScope scope.Scope = "Direct"

if I don't provide a default value, it works (but then anything that doesn't set it, fails). even though scope.Scope implements all the required interfaces for a custom GoType, it can't be a string, I have a hack, that works by running a sed script as part of the same go:generate code to do something like

sed -i "" 's/const DefaultScope.*/const DefaultScope scope.Scope = scope.Direct/' ./ent/table/table.go

but this is such a hack, I'm not keen on using it.

The reason for wanting to use Default, as it impacts out schema (and the ALTER TABLE we produce)

i.e. instead of

scope character varying NOT NULL

we get (schema generated after the sed operation).

scope character varying DEFAULT 'Direct'::character varying NOT NULL

In pondering this, I might instead make it optional/nillable. as that would make an alter table less intrusive with just having to handle the nill as the zero value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant