flagset

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 9 Imported by: 0

README

github.com/Opvra/flagset

Small, dependency-free flag set utilities for Go. This package provides a compact, stable bitmask type and a registry for name <-> bit mapping.

Why flag sets

  • Compact: a single uint64 fits in caches, tokens, DB rows, and network payloads.
  • Fast: bitwise checks are constant time.
  • Stable: old bits keep their meaning, new bits append.

Why not a struct of bools

  • Structs do not serialize compactly across boundaries.
  • Mapping to SQL/JSON requires custom logic anyway.
  • Bit masks enforce backward-compatible growth with minimal surface area.

Core type

// Each bit is an independent capability or state.
type Flag uint64

Permission (RBAC) example

const (
	PermRead  flagset.Flag = 1 << 0
	PermWrite flagset.Flag = 1 << 1
	PermAdmin flagset.Flag = 1 << 2
)

var perms flagset.Flag
perms.Grant(PermRead | PermWrite)
if perms.Has(PermRead) {
	// allow
}

Feature flag example

const (
	FeatureSearch flagset.Flag = 1 << 0
	FeatureBetaUI flagset.Flag = 1 << 1
)

flags := FeatureSearch
if flags.Any(FeatureBetaUI) {
	// enable beta UI
}

Config/state flag example

const (
	StateWarm flagset.Flag = 1 << 0
	StateBusy flagset.Flag = 1 << 1
)

var state flagset.Flag
state.Grant(StateWarm)
state.Toggle(StateBusy)

JSON usage

JSON uses flag names via an explicit registry. Set it once at startup.

r := flagset.Registry{
	"read":  1 << 0,
	"write": 1 << 1,
	"admin": 1 << 2,
}

_ = flagset.SetJSONRegistry(r)

payload := struct {
	Flags flagset.Flag `json:"flags"`
}{
	Flags: r.MustParse([]string{"read", "admin"}),
}

b, _ := json.Marshal(payload)
// {"flags":["admin","read"]}

SQL usage

Flags implement sql.Scanner and driver.Valuer for INTEGER columns.

var f flagset.Flag
_ = f.Scan(int64(3))
value, _ := f.Value()
// value is int64(3)

Registry usage

r := flagset.Registry{
	"read":  1 << 0,
	"write": 1 << 1,
	"admin": 1 << 2,
}

flags, _ := r.Parse([]string{"read", "write"})
if flags.Has(r.MustParse([]string{"read"})) {
	// ok
}

Examples

  • examples/loyal_user/main.go
  • examples/sql_usage/main.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetJSONRegistry

func SetJSONRegistry(r Registry) error

SetJSONRegistry sets the global JSON registry once at process startup.

Types

type Flag

type Flag uint64

Flag represents a set of bit flags.

func (Flag) All

func (f Flag) All(flag Flag) bool

All is an alias for Has.

func (Flag) Any

func (f Flag) Any(flag Flag) bool

Any reports whether any bit in flag is set in f.

func (*Flag) Grant

func (f *Flag) Grant(flag Flag)

Grant sets the given bits.

func (Flag) Has

func (f Flag) Has(flag Flag) bool

Has reports whether all bits in flag are set in f.

func (Flag) MarshalJSON

func (f Flag) MarshalJSON() ([]byte, error)

MarshalJSON encodes f as a JSON array of flag names.

func (*Flag) Reset

func (f *Flag) Reset()

Reset clears all bits.

func (*Flag) Revoke

func (f *Flag) Revoke(flag Flag)

Revoke clears the given bits.

func (*Flag) Scan

func (f *Flag) Scan(value any) error

Scan implements the sql.Scanner interface.

func (*Flag) Toggle

func (f *Flag) Toggle(flag Flag)

Toggle flips the given bits.

func (*Flag) UnmarshalJSON

func (f *Flag) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON array of flag names into f.

func (Flag) Value

func (f Flag) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type Registry

type Registry map[string]Flag

Registry maps human-readable names to flag bits.

func (Registry) MustParse

func (r Registry) MustParse(names []string) Flag

MustParse is like Parse but panics on error.

func (Registry) Names

func (r Registry) Names(f Flag) []string

Names returns the sorted names for the bits set in f. Unknown bits are ignored.

func (Registry) Parse

func (r Registry) Parse(names []string) (Flag, error)

Parse returns a Flag composed from the provided names.

Directories

Path Synopsis
examples
loyal_user command
sql_usage command

Jump to

Keyboard shortcuts

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