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

No support for optional enum fields #280

Open
RomanSter opened this issue Dec 1, 2024 · 0 comments
Open

No support for optional enum fields #280

RomanSter opened this issue Dec 1, 2024 · 0 comments

Comments

@RomanSter
Copy link
Contributor

it seems like the lib does not know how to cast from enum optinal (pointer) fields.

command used to generate the proto:

protoc --go_out=./ --go-grpc_out=. --gorm_out=engine=postgres:./ user.proto

protoc verison:

libprotoc 3.20.3

proto example user.proto:


syntax = "proto3";

package user;

option go_package = "./user";
import "gorm.proto";

message User {
  option (gorm.opts).ormable = true;
  int32 id = 1;
  optional Gender gender = 2;
}

enum Gender {
  male = 0;
  female = 1;
}

One of the generated user.pb.gorm.go methods:


// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *UserORM) ToPB(ctx context.Context) (User, error) {
	to := User{}
	var err error
	if prehook, ok := interface{}(m).(UserWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	to.Gender = Gender(m.Gender)
	if posthook, ok := interface{}(m).(UserWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

to.Gender = Gender(m.Gender)

causes "Cannot use '(Gender)(m. Gender)' (type Gender) as the type *Gender"

to fix this we need to cast it to a pointer Gender type like : to.Gender = (*Gender)(m.Gender)

right now I have to update the generated gorm file maualy each time I re-generate for new fields...
if there is a solution please let me know!

RomanSter added a commit to RomanSter/protoc-gen-gorm that referenced this issue Dec 9, 2024
Calebjh pushed a commit that referenced this issue Dec 9, 2024
* added support for optional enums for gorm protoc generator.

based on the issue I opened here:

#280

* return missing line from code that handles b.stringEnums
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

No branches or pull requests

1 participant