Skip to content

Commit

Permalink
Make it easier to generate a debug description of a type (#1928)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Christopher <[email protected]>
  • Loading branch information
theunrepentantgeek and matthchr authored Oct 27, 2021
1 parent 0189ce2 commit f7b6c6c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func (builder *ConversionFunctionBuilder) BuildConversion(params ConversionParam
}

types := builder.CodeGenerationContext.GetAllReachableTypes()
var sourceSB strings.Builder
params.SourceType.WriteDebugDescription(&sourceSB, types)
var destinationSB strings.Builder
params.DestinationType.WriteDebugDescription(&destinationSB, types)
panic(fmt.Sprintf("don't know how to perform conversion for %s -> %s", sourceSB.String(), destinationSB.String()))
msg := fmt.Sprintf(
"don't know how to perform conversion for %s -> %s",
DebugDescription(params.SourceType, types),
DebugDescription(params.DestinationType, types))
panic(msg)
}

// IdentityConvertComplexOptionalProperty handles conversion for optional properties with complex elements
Expand Down Expand Up @@ -311,9 +311,10 @@ func IdentityConvertComplexMapProperty(builder *ConversionFunctionBuilder, param
}

if _, ok := destinationType.KeyType().(*PrimitiveType); !ok {
var keyDescription strings.Builder
destinationType.KeyType().WriteDebugDescription(&keyDescription, nil)
panic(fmt.Sprintf("map had non-primitive key type: %s", keyDescription.String()))
msg := fmt.Sprintf(
"map had non-primitive key type: %s",
DebugDescription(destinationType.KeyType(), nil))
panic(msg)
}

depth := params.CountArraysAndMapsInConversionContext()
Expand Down
6 changes: 6 additions & 0 deletions v2/tools/generator/internal/astmodel/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ func TypeEquals(left, right Type, overrides ...EqualityOverrides) bool {

return left.Equals(right, override)
}

func DebugDescription(t Type, types Types) string {
var builder strings.Builder
t.WriteDebugDescription(&builder, types)
return builder.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package storage

import (
"fmt"
"strings"

"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/interfaces"
Expand Down Expand Up @@ -61,14 +60,10 @@ func (p *PropertyConverter) ConvertProperty(property *astmodel.PropertyDefinitio
}

// No conversion found

var typeDescription strings.Builder
property.PropertyType().WriteDebugDescription(&typeDescription, p.types)

return nil, fmt.Errorf(
"failed to find a conversion for property %s (%s)",
property.PropertyName(),
typeDescription.String())
astmodel.DebugDescription(property.PropertyType(), p.types))
}

// stripAllValidations removes all validations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (b *PropertyBagMemberType) String() string {
// builder receives the full description, including nested types
// types is a dictionary for resolving named types
func (b *PropertyBagMemberType) WriteDebugDescription(builder *strings.Builder, types astmodel.Types) {
builder.WriteString("Optional[")
builder.WriteString("Bag[")
b.element.WriteDebugDescription(builder, types)
builder.WriteString("]")
}
Expand Down
12 changes: 2 additions & 10 deletions v2/tools/generator/internal/conversions/property_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package conversions

import (
"go/token"
"strings"

"github.com/dave/dst"
"github.com/pkg/errors"
Expand Down Expand Up @@ -149,17 +148,10 @@ func CreateTypeConversion(
}

// No conversion found, we need to generate a useful error message

var debugDescriptionOfDestination strings.Builder
destinationEndpoint.Type().WriteDebugDescription(&debugDescriptionOfDestination, conversionContext.Types())

var debugDescriptionOfSource strings.Builder
sourceEndpoint.Type().WriteDebugDescription(&debugDescriptionOfSource, conversionContext.Types())

err := errors.Errorf(
"no conversion found to assign %q from %q",
debugDescriptionOfDestination.String(),
debugDescriptionOfSource.String())
astmodel.DebugDescription(destinationEndpoint.Type(), conversionContext.Types()),
astmodel.DebugDescription(sourceEndpoint.Type(), conversionContext.Types()))

return nil, err
}
Expand Down

0 comments on commit f7b6c6c

Please sign in to comment.