model/discovery/discovery.go

49 lines
1002 B
Go
Raw Permalink Normal View History

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