Skip to content

Create a tool which create the Code interface methods from constants #3

@ifraixedes

Description

@ifraixedes

Creating a new code is basically have a type which satisfy the the Code interface, so each different lib or service which use this package for returning error codes must implement a specific type for satisfying the interface and create some package level constant values to specify all the error codes which the lib/service may return.

An optimal implementation is to define a specific type based on an integer (uint32, byte, int, etc., henceforth I will use just use the word integer for mentioning them) type and use iota to assign values, and then implement the 2 Code interface methods which return the code string value and the string message based on the integer value, but this is repetitive, boring and tedious boiler plate code.

In order of easing this task, the goal is to implement a tool which generates the Code interface from the constants defined in a package of the give integer type, similar what stringer does. The main idea is to use the constant name as the code string and a comment above or next to it as a message, for example:

type errCode uint8

const(
	NotFoundEntity  // The entity doesn't exist
	NotFoundValue   // The value doesn't exist

	// The ID is invalid, only letters and numbers can be used and the length must be between 6 and 18
	InvalidID
)

In addition, it's quite usual that those error codes constants have a common prefix for distinguishing them from other package constants for example

type errCode uint8

const(
	ErrCodeNotFoundEntity  // The entity doesn't exist
	ErrCodeNotFoundValue   // The value doesn't exist

	// The ID is invalid, only letters and numbers can be used and the length must be between 6 and 18
	ErrCodeInvalidID
)

but that prefix shouldn't appear in the string returned by the String method, hence the tool should accept a flag to indicate the common prefix to exclude from the string returned by the String method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions