flagenum

package
v0.0.0-...-e834411 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: Apache-2.0 Imports: 5 Imported by: 19

Documentation

Overview

Package flagenum is a utility package which facilitates implementation of flag.Value, json.Marshaler, and json.Unmarshaler interfaces via a string-to- value mapping.

Example

Example demonstrates how to use flagenum to create bindings for a custom type.

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
)

type myType uint

var myTypeEnum = Enum{
	"foo": myType(10),
	"bar": myType(20),
}

var _ flag.Value = (*myType)(nil)

func (val *myType) Set(v string) error {
	return myTypeEnum.FlagSet(val, v)
}

func (val *myType) String() string {
	return myTypeEnum.FlagString(*val)
}

func (val myType) MarshalJSON() ([]byte, error) {
	return myTypeEnum.JSONMarshal(val)
}

// Example demonstrates how to use flagenum to create bindings for a custom
// type.
func main() {
	var value myType

	fs := flag.NewFlagSet("test", flag.ContinueOnError)
	fs.Var(&value, "value", "Set the value. Options are: "+myTypeEnum.Choices())
	fs.SetOutput(os.Stdout)

	fs.PrintDefaults()

	// Flag parsing.
	fs.Parse([]string{"-value", "bar"})
	fmt.Printf("Value is: %d\n", value)

	// JSON Marshalling.
	c := struct {
		Value myType `json:"value"`
	}{
		Value: value,
	}
	j, err := json.Marshal(&c)
	if err != nil {
		panic("Failed to marshal JSON.")
	}
	fmt.Printf("JSON is: %s\n", string(j))

}
Output:

-value value
    	Set the value. Options are: bar, foo
Value is: 20
JSON is: {"value":"bar"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enum

type Enum map[string]any

Enum is a mapping of enumeration key strings to values that can be used as flags.

Strings can be mapped to any value type that is comparable via reflect.DeepEqual.

func (Enum) Choices

func (e Enum) Choices() string

Choices returns a comma-separated string listing sorted enumeration choices.

func (Enum) FlagSet

func (e Enum) FlagSet(v any, key string) error

FlagSet implements flag.Value's Set semantics. It identifies the mapped value associated with the supplied key and stores it in the supplied interface.

The interface, v, must be a valid pointer to the mapped enumeration type.

func (Enum) FlagString

func (e Enum) FlagString(v any) string

FlagString implements flag.Value's String semantics.

func (Enum) GetKey

func (e Enum) GetKey(value any) string

GetKey performs reverse lookup of the enumeration value, returning the key that corresponds to the value.

If multiple keys correspond to the same value, the result is undefined.

func (Enum) GetValue

func (e Enum) GetValue(key string) (any, error)

GetValue returns the mapped enumeration value associated with a key.

func (Enum) JSONMarshal

func (e Enum) JSONMarshal(v any) ([]byte, error)

JSONMarshal implements json.Marshaler's MarshalJSON semantics. It marshals the value in the supplied interface to its associated key and emits a quoted string containing that key.

The interface, v, must be a valid pointer to the mapped enumeration type.

func (Enum) JSONUnmarshal

func (e Enum) JSONUnmarshal(v any, data []byte) error

JSONUnmarshal implements json.Unmarshaler's UnmarshalJSON semantics. It parses data containing a quoted string, identifies the enumeration value associated with that string, and stores it in the supplied interface.

The interface, v, must be a valid pointer to the mapped enumeration type. a string corresponding to one of the enum's keys.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL