Skip to content

String with whitespace not generated correctly. #302

@krhubert

Description

@krhubert

Hey,

How can I generate an enum string with only whitespaces? I tried different alias variants but nothing seems to work.
Is there any workaround with go-enum?

Example:

package main

//go:generate go tool go-enum -f $GOFILE


// MY CURRENT WORKAROUND
// to make it work, change unset=" " -> unset=@
// DISABLE go:generate sed -i "s/@/ /g" main_enum.go

// ENUM(unset=" ",correct=*,error=!)
type V string

Output:

// Code generated by go-enum DO NOT EDIT.
// Version: v0.9.2

// Built By: go install

package main

import (
	"errors"
	"fmt"
)

const (
	// VUnset is a V of type unset.
	VUnset V = ""
	// VCorrect is a V of type correct.
	VCorrect V = "*"
	// VError is a V of type error.
	VError V = "!"
)

var ErrInvalidV = errors.New("not a valid V")

// String implements the Stringer interface.
func (x V) String() string {
	return string(x)
}

// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x V) IsValid() bool {
	_, err := ParseV(string(x))
	return err == nil
}

var _VValue = map[string]V{
	"":  VUnset,
	"*": VCorrect,
	"!": VError,
}

// ParseV attempts to convert a string to a V.
func ParseV(name string) (V, error) {
	if x, ok := _VValue[name]; ok {
		return x, nil
	}
	return V(""), fmt.Errorf("%s is %w", name, ErrInvalidV)
}

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