Skip to content

Add functionality to get enum array and description #297

@fnoopv

Description

@fnoopv

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions