diff --git a/core/constants/network-crypto.go b/core/constants/network-crypto.go new file mode 100644 index 0000000..5a140d2 --- /dev/null +++ b/core/constants/network-crypto.go @@ -0,0 +1,49 @@ +package constants + +import ( + "encoding/json" + "fmt" +) + +type CryptoType int + +const ( + CryptoTypeTLS CryptoType = iota +) + +var ( + cryptoTypeID = map[CryptoType]string{ + CryptoTypeTLS: "TLS", + } + + cryptoTypeName = map[string]CryptoType{ + "TLS": CryptoTypeTLS, + } +) + +func (ct CryptoType) String() string { + return cryptoTypeID[ct] +} + +func (ct CryptoType) MarshalJSON() ([]byte, error) { + value, ok := cryptoTypeID[ct] + if !ok { + return nil, fmt.Errorf("Invalid EnumType[%s] value", ct) + } + return json.Marshal(value) +} + +func (ct CryptoType) UnmarshalJSON(b []byte) error { + var s string + err := json.Unmarshal(b, &s) + if err != nil { + return err + } + + value, ok := cryptoTypeName[s] + if !ok { + return fmt.Errorf("Invalid EnumType[%s] value", s) + } + ct = value + return nil +} diff --git a/core/constants/network.go b/core/constants/network-port.go similarity index 100% rename from core/constants/network.go rename to core/constants/network-port.go diff --git a/discovery/model/Port.go b/discovery/model/Port.go index cbc02a2..2ae3097 100644 --- a/discovery/model/Port.go +++ b/discovery/model/Port.go @@ -5,15 +5,16 @@ import ( "github.com/google/gopacket" + "git.loafle.net/overflow/commons-go/core/constants" "git.loafle.net/overflow/commons-go/core/util" ) type Port struct { Host *Host `json:"host,omitempty"` - ID json.Number `json:"id,Number,omitempty"` - PortType string `json:"portType,omitempty"` - PortNumber json.Number `json:"portNumber,omitempty"` + ID json.Number `json:"id,Number,omitempty"` + PortType constants.PortType `json:"portType,omitempty"` + PortNumber json.Number `json:"portNumber,omitempty"` DiscoveredDate util.Timestamp `json:"discoveredDate,omitempty"` diff --git a/discovery/model/Service.go b/discovery/model/Service.go index 437978e..08a4b07 100644 --- a/discovery/model/Service.go +++ b/discovery/model/Service.go @@ -3,15 +3,16 @@ package model import ( "encoding/json" + "git.loafle.net/overflow/commons-go/core/constants" "git.loafle.net/overflow/commons-go/core/util" ) type Service struct { Port *Port `json:"port,omitempty"` - ID json.Number `json:"id,Number,omitempty"` - CryptoType string `json:"cryptoType,omitempty"` - ServiceName string `json:"serviceName,omitempty"` + ID json.Number `json:"id,Number,omitempty"` + CryptoType constants.CryptoType `json:"cryptoType,omitempty"` + ServiceName string `json:"serviceName,omitempty"` DiscoveredDate util.Timestamp `json:"discoveredDate,omitempty"` }