mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-08-27 06:58:52 +02:00
* Inital routes to git refs api * Git refs API implementation * Update swagger * Fix copyright * Make swagger happy add basic test * Fix test * Fix test again :)
44 lines
998 B
Go
44 lines
998 B
Go
package types
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
// EnumParser parses "enum" values; i.e. a predefined set of strings to
|
|
// predefined values.
|
|
type EnumParser struct {
|
|
Type string // type name; if not set, use type of first value added
|
|
CaseMatch bool // if true, matching of strings is case-sensitive
|
|
// PrefixMatch bool
|
|
vals map[string]interface{}
|
|
}
|
|
|
|
// AddVals adds strings and values to an EnumParser.
|
|
func (ep *EnumParser) AddVals(vals map[string]interface{}) {
|
|
if ep.vals == nil {
|
|
ep.vals = make(map[string]interface{})
|
|
}
|
|
for k, v := range vals {
|
|
if ep.Type == "" {
|
|
ep.Type = reflect.TypeOf(v).Name()
|
|
}
|
|
if !ep.CaseMatch {
|
|
k = strings.ToLower(k)
|
|
}
|
|
ep.vals[k] = v
|
|
}
|
|
}
|
|
|
|
// Parse parses the string and returns the value or an error.
|
|
func (ep EnumParser) Parse(s string) (interface{}, error) {
|
|
if !ep.CaseMatch {
|
|
s = strings.ToLower(s)
|
|
}
|
|
v, ok := ep.vals[s]
|
|
if !ok {
|
|
return false, fmt.Errorf("failed to parse %s %#q", ep.Type, s)
|
|
}
|
|
return v, nil
|
|
}
|