From 99c38d9e8158747cb9d170efdf99cf22ffb539e3 Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Mon, 5 Jul 2021 11:32:51 +1200 Subject: [PATCH] Convert direction from an enum into a discriminated union --- hack/generator/pkg/conversions/direction.go | 45 +++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/hack/generator/pkg/conversions/direction.go b/hack/generator/pkg/conversions/direction.go index 1a80f64b273..1f27723396b 100644 --- a/hack/generator/pkg/conversions/direction.go +++ b/hack/generator/pkg/conversions/direction.go @@ -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 +}