model/discovery/discovery.go
crusader f6eb351409 ing
2018-09-17 15:29:47 +09:00

49 lines
1002 B
Go

package discovery
import (
"bytes"
"encoding/json"
)
type DiscoveryModeType int
const (
DiscoveryModeTypePrivileged DiscoveryModeType = iota + 1
DiscoveryModeTypeUnprivileged
)
var (
discoveryModeTypeID = map[DiscoveryModeType]string{
DiscoveryModeTypePrivileged: "Privileged",
DiscoveryModeTypeUnprivileged: "Unprivileged",
}
discoveryModeTypeName = map[string]DiscoveryModeType{
"Privileged": DiscoveryModeTypePrivileged,
"Unprivileged": DiscoveryModeTypeUnprivileged,
}
)
func (e DiscoveryModeType) String() string {
return discoveryModeTypeID[e]
}
func (e *DiscoveryModeType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(discoveryModeTypeID[*e])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (e *DiscoveryModeType) UnmarshalJSON(b []byte) error {
// unmarshal as string
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
// lookup value
*e = discoveryModeTypeName[s]
return nil
}