model/discovery/discovery.go
2018-09-17 15:28:05 +09:00

49 lines
1002 B
Go

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
}