From 35da06272f8abe9e4e8ed6a8806d62e65fa7eeab Mon Sep 17 00:00:00 2001 From: Sharan <60929919+rustiever@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:10:19 +0530 Subject: [PATCH] feat(proto): add yaml marshal/unmarshal for enums (#263) Co-authored-by: sharan-rafay --- cmd/generate-enum/main.go | 24 ++++++++++++++++++- ...enum.go => paralusconditionstatus.enum.go} | 22 +++++++++++++++++ .../infrapb/v3/clusterconditiontype.enum.go | 22 +++++++++++++++++ .../types/infrapb/v3/clusternodestate.enum.go | 22 +++++++++++++++++ .../types/infrapb/v3/clustersharemode.enum.go | 22 +++++++++++++++++ .../infrapb/v3/clustertokenstate.enum.go | 22 +++++++++++++++++ .../types/infrapb/v3/clustertokentype.enum.go | 22 +++++++++++++++++ proto/types/sentry/bootstrapagentmode.enum.go | 22 +++++++++++++++++ .../types/sentry/bootstrapagentstate.enum.go | 22 +++++++++++++++++ .../sentry/bootstrapagenttemplatetype.enum.go | 22 +++++++++++++++++ proto/types/sentry/bootstrapagenttype.enum.go | 22 +++++++++++++++++ proto/types/sentry/bootstrapinfratype.enum.go | 22 +++++++++++++++++ .../sentry/bootstraptemplatehosttype.enum.go | 22 +++++++++++++++++ 13 files changed, 287 insertions(+), 1 deletion(-) rename proto/types/commonpb/v3/{rafayconditionstatus.enum.go => paralusconditionstatus.enum.go} (66%) diff --git a/cmd/generate-enum/main.go b/cmd/generate-enum/main.go index 81ce85a..99c9966 100644 --- a/cmd/generate-enum/main.go +++ b/cmd/generate-enum/main.go @@ -14,6 +14,7 @@ package {{ .PackageName }} import ( driver "database/sql/driver" bytes "bytes" + "fmt" ) // Scan converts database string to {{ .EnumName }} @@ -44,8 +45,29 @@ func (e *{{ .EnumName }}) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e {{ .EnumName }}) MarshalYAML() (interface{}, error) { + return {{ .EnumName }}_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *{{ .EnumName }}) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := {{ .EnumName }}_value[name] + if !ok { + return fmt.Errorf("invalid {{ .EnumName }}: %s", name) + } + + *e = {{ .EnumName }}(value) + return nil +} + // implement proto enum interface -func (e {{ .EnumName }}) IsEnum() { +func (e {{ .EnumName }}) IsEnum() { } ` diff --git a/proto/types/commonpb/v3/rafayconditionstatus.enum.go b/proto/types/commonpb/v3/paralusconditionstatus.enum.go similarity index 66% rename from proto/types/commonpb/v3/rafayconditionstatus.enum.go rename to proto/types/commonpb/v3/paralusconditionstatus.enum.go index 2aec9b8..ff1fc44 100644 --- a/proto/types/commonpb/v3/rafayconditionstatus.enum.go +++ b/proto/types/commonpb/v3/paralusconditionstatus.enum.go @@ -4,6 +4,7 @@ package commonv3 import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to ParalusConditionStatus @@ -40,6 +41,27 @@ func (e *ParalusConditionStatus) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e ParalusConditionStatus) MarshalYAML() (interface{}, error) { + return ParalusConditionStatus_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *ParalusConditionStatus) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := ParalusConditionStatus_value[name] + if !ok { + return fmt.Errorf("invalid ParalusConditionStatus: %s", name) + } + + *e = ParalusConditionStatus(value) + return nil +} + // implement proto enum interface func (e ParalusConditionStatus) IsEnum() { } diff --git a/proto/types/infrapb/v3/clusterconditiontype.enum.go b/proto/types/infrapb/v3/clusterconditiontype.enum.go index ad64e5c..92b1fba 100644 --- a/proto/types/infrapb/v3/clusterconditiontype.enum.go +++ b/proto/types/infrapb/v3/clusterconditiontype.enum.go @@ -4,6 +4,7 @@ package infrav3 import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to ClusterConditionType @@ -40,6 +41,27 @@ func (e *ClusterConditionType) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e ClusterConditionType) MarshalYAML() (interface{}, error) { + return ClusterConditionType_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *ClusterConditionType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := ClusterConditionType_value[name] + if !ok { + return fmt.Errorf("invalid ClusterConditionType: %s", name) + } + + *e = ClusterConditionType(value) + return nil +} + // implement proto enum interface func (e ClusterConditionType) IsEnum() { } diff --git a/proto/types/infrapb/v3/clusternodestate.enum.go b/proto/types/infrapb/v3/clusternodestate.enum.go index 1025eec..c08d394 100644 --- a/proto/types/infrapb/v3/clusternodestate.enum.go +++ b/proto/types/infrapb/v3/clusternodestate.enum.go @@ -4,6 +4,7 @@ package infrav3 import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to ClusterNodeState @@ -34,6 +35,27 @@ func (e *ClusterNodeState) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e ClusterNodeState) MarshalYAML() (interface{}, error) { + return ClusterNodeState_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *ClusterNodeState) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := ClusterNodeState_value[name] + if !ok { + return fmt.Errorf("invalid ClusterNodeState: %s", name) + } + + *e = ClusterNodeState(value) + return nil +} + // implement proto enum interface func (e ClusterNodeState) IsEnum() { } diff --git a/proto/types/infrapb/v3/clustersharemode.enum.go b/proto/types/infrapb/v3/clustersharemode.enum.go index 2fce802..a9d5a96 100644 --- a/proto/types/infrapb/v3/clustersharemode.enum.go +++ b/proto/types/infrapb/v3/clustersharemode.enum.go @@ -4,6 +4,7 @@ package infrav3 import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to ClusterShareMode @@ -34,6 +35,27 @@ func (e *ClusterShareMode) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e ClusterShareMode) MarshalYAML() (interface{}, error) { + return ClusterShareMode_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *ClusterShareMode) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := ClusterShareMode_value[name] + if !ok { + return fmt.Errorf("invalid ClusterShareMode: %s", name) + } + + *e = ClusterShareMode(value) + return nil +} + // implement proto enum interface func (e ClusterShareMode) IsEnum() { } diff --git a/proto/types/infrapb/v3/clustertokenstate.enum.go b/proto/types/infrapb/v3/clustertokenstate.enum.go index e1f64bc..5b5a899 100644 --- a/proto/types/infrapb/v3/clustertokenstate.enum.go +++ b/proto/types/infrapb/v3/clustertokenstate.enum.go @@ -4,6 +4,7 @@ package infrav3 import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to ClusterTokenState @@ -34,6 +35,27 @@ func (e *ClusterTokenState) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e ClusterTokenState) MarshalYAML() (interface{}, error) { + return ClusterTokenState_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *ClusterTokenState) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := ClusterTokenState_value[name] + if !ok { + return fmt.Errorf("invalid ClusterTokenState: %s", name) + } + + *e = ClusterTokenState(value) + return nil +} + // implement proto enum interface func (e ClusterTokenState) IsEnum() { } diff --git a/proto/types/infrapb/v3/clustertokentype.enum.go b/proto/types/infrapb/v3/clustertokentype.enum.go index ca18dee..a04f3cf 100644 --- a/proto/types/infrapb/v3/clustertokentype.enum.go +++ b/proto/types/infrapb/v3/clustertokentype.enum.go @@ -4,6 +4,7 @@ package infrav3 import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to ClusterTokenType @@ -34,6 +35,27 @@ func (e *ClusterTokenType) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e ClusterTokenType) MarshalYAML() (interface{}, error) { + return ClusterTokenType_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *ClusterTokenType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := ClusterTokenType_value[name] + if !ok { + return fmt.Errorf("invalid ClusterTokenType: %s", name) + } + + *e = ClusterTokenType(value) + return nil +} + // implement proto enum interface func (e ClusterTokenType) IsEnum() { } diff --git a/proto/types/sentry/bootstrapagentmode.enum.go b/proto/types/sentry/bootstrapagentmode.enum.go index d8144cb..db9a1e5 100644 --- a/proto/types/sentry/bootstrapagentmode.enum.go +++ b/proto/types/sentry/bootstrapagentmode.enum.go @@ -4,6 +4,7 @@ package sentry import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to BootstrapAgentMode @@ -34,6 +35,27 @@ func (e *BootstrapAgentMode) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e BootstrapAgentMode) MarshalYAML() (interface{}, error) { + return BootstrapAgentMode_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *BootstrapAgentMode) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := BootstrapAgentMode_value[name] + if !ok { + return fmt.Errorf("invalid BootstrapAgentMode: %s", name) + } + + *e = BootstrapAgentMode(value) + return nil +} + // implement proto enum interface func (e BootstrapAgentMode) IsEnum() { } diff --git a/proto/types/sentry/bootstrapagentstate.enum.go b/proto/types/sentry/bootstrapagentstate.enum.go index 07bb387..a8bddb1 100644 --- a/proto/types/sentry/bootstrapagentstate.enum.go +++ b/proto/types/sentry/bootstrapagentstate.enum.go @@ -4,6 +4,7 @@ package sentry import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to BootstrapAgentState @@ -34,6 +35,27 @@ func (e *BootstrapAgentState) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e BootstrapAgentState) MarshalYAML() (interface{}, error) { + return BootstrapAgentState_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *BootstrapAgentState) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := BootstrapAgentState_value[name] + if !ok { + return fmt.Errorf("invalid BootstrapAgentState: %s", name) + } + + *e = BootstrapAgentState(value) + return nil +} + // implement proto enum interface func (e BootstrapAgentState) IsEnum() { } diff --git a/proto/types/sentry/bootstrapagenttemplatetype.enum.go b/proto/types/sentry/bootstrapagenttemplatetype.enum.go index 96b57fd..fdc1576 100644 --- a/proto/types/sentry/bootstrapagenttemplatetype.enum.go +++ b/proto/types/sentry/bootstrapagenttemplatetype.enum.go @@ -4,6 +4,7 @@ package sentry import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to BootstrapAgentTemplateType @@ -34,6 +35,27 @@ func (e *BootstrapAgentTemplateType) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e BootstrapAgentTemplateType) MarshalYAML() (interface{}, error) { + return BootstrapAgentTemplateType_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *BootstrapAgentTemplateType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := BootstrapAgentTemplateType_value[name] + if !ok { + return fmt.Errorf("invalid BootstrapAgentTemplateType: %s", name) + } + + *e = BootstrapAgentTemplateType(value) + return nil +} + // implement proto enum interface func (e BootstrapAgentTemplateType) IsEnum() { } diff --git a/proto/types/sentry/bootstrapagenttype.enum.go b/proto/types/sentry/bootstrapagenttype.enum.go index e42f1d8..29683db 100644 --- a/proto/types/sentry/bootstrapagenttype.enum.go +++ b/proto/types/sentry/bootstrapagenttype.enum.go @@ -4,6 +4,7 @@ package sentry import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to BootstrapAgentType @@ -34,6 +35,27 @@ func (e *BootstrapAgentType) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e BootstrapAgentType) MarshalYAML() (interface{}, error) { + return BootstrapAgentType_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *BootstrapAgentType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := BootstrapAgentType_value[name] + if !ok { + return fmt.Errorf("invalid BootstrapAgentType: %s", name) + } + + *e = BootstrapAgentType(value) + return nil +} + // implement proto enum interface func (e BootstrapAgentType) IsEnum() { } diff --git a/proto/types/sentry/bootstrapinfratype.enum.go b/proto/types/sentry/bootstrapinfratype.enum.go index 43b3b86..0ea4564 100644 --- a/proto/types/sentry/bootstrapinfratype.enum.go +++ b/proto/types/sentry/bootstrapinfratype.enum.go @@ -4,6 +4,7 @@ package sentry import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to BootstrapInfraType @@ -34,6 +35,27 @@ func (e *BootstrapInfraType) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e BootstrapInfraType) MarshalYAML() (interface{}, error) { + return BootstrapInfraType_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *BootstrapInfraType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := BootstrapInfraType_value[name] + if !ok { + return fmt.Errorf("invalid BootstrapInfraType: %s", name) + } + + *e = BootstrapInfraType(value) + return nil +} + // implement proto enum interface func (e BootstrapInfraType) IsEnum() { } diff --git a/proto/types/sentry/bootstraptemplatehosttype.enum.go b/proto/types/sentry/bootstraptemplatehosttype.enum.go index 8e0ddff..38bc36f 100644 --- a/proto/types/sentry/bootstraptemplatehosttype.enum.go +++ b/proto/types/sentry/bootstraptemplatehosttype.enum.go @@ -4,6 +4,7 @@ package sentry import ( bytes "bytes" driver "database/sql/driver" + "fmt" ) // Scan converts database string to BootstrapTemplateHostType @@ -34,6 +35,27 @@ func (e *BootstrapTemplateHostType) UnmarshalJSON(b []byte) error { return nil } +// MarshalYAML implements the yaml.Marshaler interface +func (e BootstrapTemplateHostType) MarshalYAML() (interface{}, error) { + return BootstrapTemplateHostType_name[int32(e)], nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (e *BootstrapTemplateHostType) UnmarshalYAML(unmarshal func(interface{}) error) error { + var name string + if err := unmarshal(&name); err != nil { + return err + } + + value, ok := BootstrapTemplateHostType_value[name] + if !ok { + return fmt.Errorf("invalid BootstrapTemplateHostType: %s", name) + } + + *e = BootstrapTemplateHostType(value) + return nil +} + // implement proto enum interface func (e BootstrapTemplateHostType) IsEnum() { }