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

Ogent doesn't work with ENUM types. #51

Closed
chasak opened this issue Feb 28, 2022 · 3 comments · Fixed by #54
Closed

Ogent doesn't work with ENUM types. #51

chasak opened this issue Feb 28, 2022 · 3 comments · Fixed by #54

Comments

@chasak
Copy link

chasak commented Feb 28, 2022

When I try to use Ogent with Enum , I am getting Errors.
I have hosted an example at https://github.com/chasak/enumeg
Here is part of schema file.

func (Video) Fields() []ent.Field {
	return []ent.Field{
		field.String("title"),
		field.Enum("videotype").Values("live", "video", "playlist"),
	}
}

This generates the following response

func NewGroupVideosList(e *ent.Video) *GroupVideosList {
	if e == nil {
		return nil
	}
	return &GroupVideosList{
		ID:          e.ID,
		Title:       e.Title,
		Videotype:   e.Videotype,
	}
}

The error is on Videotype: e.Videotype, line due to the mismatched types. Take a look at GroupVideosList

type GroupVideosList struct {
	ID          int                      "json:\"id\""
	Title       string                   "json:\"title\""
	Videotype   GroupVideosListVideotype "json:\"videotype\""
}

type GroupVideosListVideotype string

const (
	GroupVideosListVideotypeLive     GroupVideosListVideotype = "live"
	GroupVideosListVideotypeVideo    GroupVideosListVideotype = "video"
	GroupVideosListVideotypePlaylist GroupVideosListVideotype = "playlist"
)

and ent.Video

type Video struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Title holds the value of the "title" field.
	Title string `json:"title,omitempty"`
	// Videotype holds the value of the "videotype" field.
	Videotype video.Videotype `json:"videotype,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the VideoQuery when eager-loading is set.
	Edges        VideoEdges `json:"edges"`
	group_videos *int
}

// Videotype defines the type for the "videotype" enum field.
type Videotype string

// Videotype values.
const (
	VideotypeLive     Videotype = "live"
	VideotypeVideo    Videotype = "video"
	VideotypePlaylist Videotype = "playlist"
)

The error is happeing due to not doing explicit typecasting and can be solved by
Videotype: GroupVideosListVideotype(e.Videotype),

func NewGroupVideosList(e *ent.Video) *GroupVideosList {
	if e == nil {
		return nil
	}
	return &GroupVideosList{
		ID:          e.ID,
		Title:       e.Title,
		Videotype:   GroupVideosListVideotype(e.Videotype),
	}
}

Same Issue with handler Function

func (h *OgentHandler) CreateVideo(ctx context.Context, req CreateVideoReq) (CreateVideoRes, error) {
	b := h.client.Video.Create()
	// Add all fields.
	b.SetTitle(req.Title)
	b.SetVideotype(req.Videotype)
}

also here

func (h *OgentHandler) UpdateVideo(ctx context.Context, req UpdateVideoReq, params UpdateVideoParams) (UpdateVideoRes, error) {
	b := h.client.Video.UpdateOneID(params.ID)
	// Add all fields.
	if v, ok := req.Title.Get(); ok {
		b.SetTitle(v)
	}
	if v, ok := req.Videotype.Get(); ok {
		b.SetVideotype(v)
	}

Thanks.

@masseelch
Copy link
Member

Hey @chasak, thank you for reporting this. I will look into it.

@masseelch
Copy link
Member

Hey @chasak, ogent now works with Enums. Let me know, if you experience any other issues.

@chasak
Copy link
Author

chasak commented Mar 1, 2022

I have just checked it. Thanks

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 a pull request may close this issue.

2 participants