stephenafamo/bob

Invalid Code Generation with enum values containing symbols

Closed this issue · 7 comments

Since enum values are title-cased and appended to the enum name to get the enum variable name, an enum value containing symbols would lead to invalid generated code:

CREATE TYPE "healthapp"."blood_group_enum" AS ENUM ('A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-');
❯ go generate
   1278 bytes        psql/bob_enums.go
2024/02/13 21:41:19 singleton template output: failed to format template

   7
   8 	type HealthappBloodGroupEnum string
   9
  10 	// Enum values for HealthappBloodGroupEnum
  11 	const (
>>>> 	HealthappBloodGroupEnumA+ HealthappBloodGroupEnum = "A+"
  13 		HealthappBloodGroupEnumA- HealthappBloodGroupEnum = "A-"
  14 		HealthappBloodGroupEnumB+ HealthappBloodGroupEnum = "B+"
  15 		HealthappBloodGroupEnumB- HealthappBloodGroupEnum = "B-"
  16 		HealthappBloodGroupEnumAB+ HealthappBloodGroupEnum = "AB+"
  17 		HealthappBloodGroupEnumAB- HealthappBloodGroupEnum = "AB-"

:12:26: expected ';', found '+' (and 4 more errors)
exit status 1
generate.go:10: running "go": exit status 1

@stephenafamo wouldn't a quick solution for this be either replacing english words + -> Plus for all the special characters ?

If that works, and you can point me to where this needs to change, I can work on the PR.

That would be a good solution. However, I can't yet find a way to do the "symbol -> name" lookup. That is the blocker.

I can create a dictionary for this, though I'm unsure where in the codebase I need to be looking to create this.

Well... I think this type of utility package can be outside of the main package itself, in case anyone else was looking for a similar thing.

However, I think creating a dictionary by hand is far too tedious and would be hard to maintain and update with time.

What would be ideal is to find a structured specification of the Unicode symbols, and then generate the dictionary based on that.
In the future, as the Unicode spec gets updated, it would only be a matter of re-generating the dictionary.

For example, I have a package that provides some helpers around working with Google fonts, and every now and then, I just have to regenerate part of the code based on data from the Google Fonts API.

Agreed, that would be the best way to go about it. Only question for me is where does the enum generation happen ?

The template that writes enums is gen/templates/models/singleton/bob_enums.go.tpl

However, it is up to each driver to construct the enum type and value and provide them in DBInfo[T].

The Enum structure is defined here

@rkrishnasanka @safaci2000 check if #207 fixes your issues