From b02776ec03453956000861de4fab5034e8076884 Mon Sep 17 00:00:00 2001 From: crusader Date: Mon, 17 Sep 2018 15:28:05 +0900 Subject: [PATCH] DiscoveryModeEnum added --- discovery/discovery.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 discovery/discovery.go 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 +}