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

String with whitespace not generated correctly.

Open krhubert opened this issue 1 month ago • 1 comments

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)
}

krhubert avatar Nov 07 '25 16:11 krhubert

I'm pretty sure the spaces are trimmed when determining the enum string. Not sure there is a workaround for you at the moment.

abice avatar Nov 07 '25 22:11 abice