diff --git a/discovery/discovery.go b/discovery/discovery.go new file mode 100644 index 0000000..56c488f --- /dev/null +++ b/discovery/discovery.go @@ -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 +}