Skip to content

Commit

Permalink
add enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Aug 17, 2024
1 parent 03f8e8e commit 69c2ff6
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions enums/enums.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Package enums provides a way to register enum descriptors and lookup them by name.
package enums

import "sync"

// Descriptor is a struct that describes an enum type.
type Descriptor struct {
Name string `json:"name"`
Description string `json:"description"`
Members []MemberDescriptor `json:"members"`
}

// MemberDescriptor is a struct that describes an enum member.
type MemberDescriptor struct {
Name string `json:"name"`
Value int `json:"value"`
Description string `json:"description"`
}

var (
descriptorsMu sync.RWMutex
descriptors = make(map[string]*Descriptor)
)

// RegisterDescriptor registers an enum descriptor.
func RegisterDescriptor(descriptor *Descriptor) {
descriptorsMu.Lock()
defer descriptorsMu.Unlock()
if _, dup := descriptors[descriptor.Name]; dup {
panic("enums: RegisterDescriptor called twice for descriptor " + descriptor.Name)
}
descriptors[descriptor.Name] = descriptor
}

// LookupDescriptor looks up an enum descriptor by name.
func LookupDescriptor(name string) *Descriptor {
descriptorsMu.RLock()
defer descriptorsMu.RUnlock()
descriptor := descriptors[name]
return descriptor
}

0 comments on commit 69c2ff6

Please sign in to comment.