Enums Maintenance - 图1tip

This feature is an experimental feature, available from version v2.4.

Introduction

This command is used to analyze the source code in the specified code directory and generate enum values and Go code files according to specifications, mainly to improve the maintenance of enum values in OpenAPIv3 documents.

Addressed Pain Points

Pain Points Description

  • The issue of not displaying the optional items of enum values in the API documentation.
  • The difficulty in maintaining enum values in the API documentation, and the issue of code and documentation being maintained separately. This reduces the collaboration efficiency with the caller, especially between frontend and backend.

For example, in the following API definition, tasks have multiple states which are enum values. It’s costly for the backend to maintain and easy to miss states maintenance, causing incomplete state enum values.

Enums Maintenance - 图2

Pain Points Resolution

The tool parses source code and generates enum values into the startup package Go file, automatically loading enum values when the service runs, reducing manual maintenance costs and avoiding omitted enum value maintenance.

For example, in the following API definition, using the tool to maintain enum values improves development efficiency.

Enums Maintenance - 图3

Command Usage

  1. $ gf gen enums -h
  2. USAGE
  3. gf gen enums [OPTION]
  4. OPTION
  5. -s, --src source folder path to be parsed
  6. -p, --path output go file path storing enums content
  7. -x, --prefixes only exports packages that starts with specified prefixes
  8. -h, --help more information about this command
  9. EXAMPLE
  10. gf gen enums
  11. gf gen enums -p internal/boot/boot_enums.go
  12. gf gen enums -p internal/boot/boot_enums.go -s .
  13. gf gen enums -x github.com/gogf

Parameter Description:

NameRequiredDefaultDescription
srcNo.Specify the source code directory path for analysis, default is the current project root directory
pathNointernal/boot/boot_enums.goSpecify the path for the generated enum registration Go code file
prefixesNo-Only generate enum values for package names with specified prefixes, supports multiple prefix configurations

Enums Maintenance - 图4info

This command uses AST parsing to recursively analyze enums definitions for all dependency packages. If the business project dependencies are complex, the generated enums may be many, but most of the time we only care about the enums definitions of our own project or specific dependency packages, hence the prefixes parameter can be used to control the generation of enums for only specific package name prefixes.

Example of the tool configuration item in yaml format:

hack/config.yaml

  1. gfcli:
  2. gen:
  3. enums:
  4. src: "api"
  5. path: "internal/boot/boot_enums.go"
  6. prefixes:
  7. - github.com/gogf
  8. - myexample/project

Usage of the Generated File

Execute the gf gen enums command to generate the enum analysis file internal/boot/boot_enums.go. After generating the file, it needs to be anonymously imported in the project’s entry file:

  1. import (
  2. _ "project_module_name/internal/boot"
  3. )

Further Reading

How to Standardize Enum Values

Please refer to the section: Golang Enums

How to Validate Enum Values

If enum values are standardized and files are generated by command for enum value maintenance, then in parameter validation, the enums rule can be used to validate enum value fields. For specific rule introduction, please refer to the section: Data Validation - Rules