DiscoveryModeEnum added

This commit is contained in:
crusader 2018-09-17 15:28:05 +09:00
parent bd7c853bdc
commit b02776ec03

48
discovery/discovery.go Normal file
View File

@ -0,0 +1,48 @@
package discovery
import (
"bytes"
"encoding/json"
)
type DiscoveryModeEnum int
const (
DiscoveryModeEnumPrivileged DiscoveryModeEnum = iota + 1
DiscoveryModeEnumUnprivileged
)
var (
discoveryModeEnumID = map[DiscoveryModeEnum]string{
DiscoveryModeEnumPrivileged: "Privileged",
DiscoveryModeEnumUnprivileged: "Unprivileged",
}
discoveryModeEnumName = map[string]DiscoveryModeEnum{
"Privileged": DiscoveryModeEnumPrivileged,
"Unprivileged": DiscoveryModeEnumUnprivileged,
}
)
func (e DiscoveryModeEnum) String() string {
return discoveryModeEnumID[e]
}
func (e *DiscoveryModeEnum) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(discoveryModeEnumID[*e])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (e *DiscoveryModeEnum) UnmarshalJSON(b []byte) error {
// unmarshal as string
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
// lookup value
*e = discoveryModeEnumName[s]
return nil
}