feat(proto): add yaml marshal/unmarshal for enums (#263)

Co-authored-by: sharan-rafay <sharan.r@rafay.co>
This commit is contained in:
Sharan
2023-10-06 16:10:19 +05:30
committed by GitHub
parent 58593a839c
commit 35da06272f
13 changed files with 287 additions and 1 deletions

View File

@@ -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() {
}
`

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}

View File

@@ -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() {
}