-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |