Skip to content

Commit

Permalink
Use new Direction to simplify consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrepentantgeek committed Jul 4, 2021
1 parent 99c38d9 commit 98acd7f
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions hack/generator/pkg/conversions/property_assignment_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,10 @@ func (fn *PropertyAssignmentFunction) Equals(f astmodel.Function) bool {

// AsFunc renders this function as an AST for serialization to a Go source file
func (fn *PropertyAssignmentFunction) AsFunc(generationContext *astmodel.CodeGenerationContext, receiver astmodel.TypeName) *dst.FuncDecl {
var description string
switch fn.direction {
case ConvertFrom:
description = fmt.Sprintf("populates our %s from the provided source %s", receiver.Name(), fn.otherDefinition.Name().Name())
case ConvertTo:
description = fmt.Sprintf("populates the provided destination %s from our %s", fn.otherDefinition.Name().Name(), receiver.Name())
default:
panic(fmt.Sprintf("unexpected conversion direction %q", fn.direction))
}

description := fn.direction.SelectString(
fmt.Sprintf("populates our %s from the provided source %s", receiver.Name(), fn.otherDefinition.Name().Name()),
fmt.Sprintf("populates the provided destination %s from our %s", fn.otherDefinition.Name().Name(), receiver.Name()))

// We always use a pointer receiver so we can modify it
receiverType := astmodel.NewOptionalType(receiver).AsType(generationContext)
Expand Down Expand Up @@ -268,19 +263,14 @@ func (fn *PropertyAssignmentFunction) generateAssignments(
// createConversions iterates through the properties on our receiver type, matching them up with
// our other type and generating conversions where possible
func (fn *PropertyAssignmentFunction) createConversions(receiver astmodel.TypeDefinition) error {
var sourceEndpoints map[string]ReadableConversionEndpoint
var destinationEndpoints map[string]WritableConversionEndpoint
// When converting FROM, otherDefinition.Type() is our source
// When converting TO, receiver.Type() is our source
// and conversely for our destination
sourceType := fn.direction.SelectType(fn.otherDefinition.Type(), receiver.Type())
destinationType := fn.direction.SelectType(receiver.Type(), fn.otherDefinition.Type())

switch fn.direction {
case ConvertFrom:
sourceEndpoints = fn.createReadableEndpoints(fn.otherDefinition.Type())
destinationEndpoints = fn.createWritableEndpoints(receiver.Type())
case ConvertTo:
sourceEndpoints = fn.createReadableEndpoints(receiver.Type())
destinationEndpoints = fn.createWritableEndpoints(fn.otherDefinition.Type())
default:
panic(fmt.Sprintf("unexpected conversion direction %q", fn.direction))
}
sourceEndpoints := fn.createReadableEndpoints(sourceType)
destinationEndpoints := fn.createWritableEndpoints(destinationType)

// Flag receiver and parameter names as used
fn.knownLocals.Add(fn.receiverName)
Expand All @@ -301,8 +291,8 @@ func (fn *PropertyAssignmentFunction) createConversions(receiver astmodel.TypeDe
return errors.Wrapf(
err,
"creating conversion to %s by %s",
sourceEndpoint,
destinationEndpoint)
destinationEndpoint,
sourceEndpoint)
} else if conv != nil {
// A conversion was created, keep it for later
fn.conversions[destinationName] = conv
Expand Down Expand Up @@ -401,13 +391,7 @@ func (fn *PropertyAssignmentFunction) createConversion(

func nameOfPropertyAssignmentFunction(name astmodel.TypeName, direction Direction, idFactory astmodel.IdentifierFactory) string {
nameOfOtherType := idFactory.CreateIdentifier(name.Name(), astmodel.Exported)
switch direction {
case ConvertTo:
return "AssignPropertiesTo" + nameOfOtherType

case ConvertFrom:
return "AssignPropertiesFrom" + nameOfOtherType
}

panic(fmt.Sprintf("unexpected conversion direction %q", direction))
return direction.SelectString(
"AssignPropertiesFrom"+nameOfOtherType,
"AssignPropertiesTo"+nameOfOtherType)
}

0 comments on commit 98acd7f

Please sign in to comment.