Skip to content

Commit

Permalink
Convert direction from an enum into a discriminated union
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrepentantgeek committed Jul 4, 2021
1 parent fafcaca commit 99c38d9
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions hack/generator/pkg/conversions/direction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,49 @@

package conversions

import (
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
)

// Direction specifies the direction of conversion we're implementing with this function
type Direction int
type Direction interface {
// SelectString returns one of the provided strings, depending on the direction of conversion
SelectString(from string, to string) string
// SelectType returns one of the provided types, depending on the direction of conversion
SelectType(from astmodel.Type, to astmodel.Type) astmodel.Type
}

const (
var (
// ConvertFrom indicates the conversion is from the passed 'other', populating the receiver with properties from the other
ConvertFrom = Direction(1)
ConvertFrom Direction = &ConvertFromDirection{}
// ConvertTo indicates the conversion is to the passed 'other', populating the other with properties from the receiver
ConvertTo = Direction(2)
ConvertTo Direction = &ConvertToDirection{}
)

type ConvertFromDirection struct{}

var _ Direction = &ConvertFromDirection{}

// SelectString returns the string for conversion FROM
func (dir *ConvertFromDirection) SelectString(fromString string, _ string) string {
return fromString
}

// SelectType returns the type for conversion FROM
func (dir *ConvertFromDirection) SelectType(fromType astmodel.Type, _ astmodel.Type) astmodel.Type {
return fromType
}

type ConvertToDirection struct{}

var _ Direction = &ConvertToDirection{}

// SelectString returns the string for conversion TO
func (dir *ConvertToDirection) SelectString(_ string, toValue string) string {
return toValue
}

// SelectType returns the type for conversion TO
func (dir *ConvertToDirection) SelectType(_ astmodel.Type, toType astmodel.Type) astmodel.Type {
return toType
}

0 comments on commit 99c38d9

Please sign in to comment.