go-enum icon indicating copy to clipboard operation
go-enum copied to clipboard

Add functionality to get enum array and description

Open fnoopv opened this issue 2 months ago • 1 comments

Use Cases:

  1. I have a validator that performs validation when a user submits information. The validation method is similar to v.In(arrary), and I need to obtain an array of enumeration values ​​for validation. current: v.In([]enum.UserStatus{enum.UserStatusDisabled, enum.UserStatusBanned, enum.UserStatusNormal,}) Implementation: Add a GetRecords (or other name) method that returns an array of enumeration values.

  2. In my application, users have several statuses (user_status: disabled, banned, normal). Users in other statuses except normal are prohibited from logging in. When they log in, I can only return "disabled" or "banned." However, because I'm in a non-English speaking country, these aren't sufficient to describe the reason.

I'd like to add corresponding descriptive information for each enumeration value. When a user enters a certain status, I can retrieve the corresponding description and provide it to the user. Implementation: a. Allow the use of - to add description information when defining. example

// ENUM(disabled - The account has been disabled, banned - This account has been disabled for security reasons, normal)
type UserStatus string

The - and the description following it are optional. If present, parse it and add a Map and the methods described below. Otherwise, maintain the current logic. b. Add a GetDescription (or other name) method to return the description of the enumeration value.

var _UserStatusValueDescription = map[string]string{
	"disabled":    "The account has been disabled",
	"banned":      "This account has been disabled for security reasons",
	"normal":      "",
}

func (x DocumentStatus) GetDescription() string {
    description, _ := _UserStatusValueDescription[x]
    return description
}

If you think these requirements are reasonable, I'd be happy to implement them.

fnoopv avatar Oct 24 '25 09:10 fnoopv

The values flag already exists. But happy to review a PR for the other one

abice avatar Oct 24 '25 12:10 abice