Skip to content

Commit

Permalink
fix: error when enum and useUnderlyingTypeMethods conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Mar 31, 2024
1 parent fcdeeca commit c38fe78
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions builder/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type Enum struct{}

// Matches returns true, if the builder can create handle the given types.
func (*Enum) Matches(ctx *MethodContext, source, target *xtype.Type) bool {
return isEnum(ctx, source, target)
}

func isEnum(ctx *MethodContext, source, target *xtype.Type) bool {
return ctx.Conf.Enum.Enabled &&
source.Enum(&ctx.Conf.Enum).OK &&
target.Enum(&ctx.Conf.Enum).OK
Expand Down
11 changes: 11 additions & 0 deletions builder/underlying.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package builder

import (
"fmt"

"github.com/dave/jennifer/jen"
"github.com/jmattheis/goverter/xtype"
)
Expand All @@ -20,6 +22,15 @@ func (*UseUnderlyingTypeMethods) Matches(ctx *MethodContext, source, target *xty

// Build creates conversion source code for the given source and target type.
func (*UseUnderlyingTypeMethods) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *xtype.JenID, *Error) {
if isEnum(ctx, source, target) {
return nil, nil, NewError(fmt.Sprintf(`The conversion between the types
%s
%s

Check warning on line 28 in builder/underlying.go

View check run for this annotation

Codecov / codecov/patch

builder/underlying.go#L26-L28

Added lines #L26 - L28 were not covered by tests
does qualify for enum conversion but also match an extend method via useUnderlyingTypeMethods.
You have to disable enum or useUnderlyingTypeMethods to resolve the setting conflict.`, source.String, target.String))

Check warning on line 31 in builder/underlying.go

View check run for this annotation

Codecov / codecov/patch

builder/underlying.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}

sourceUnderlying, targetUnderlying := findUnderlyingExtendMapping(ctx, source, target)

innerSource := source
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import GH from './GH.vue';

# Changelog

## unreleased

- Error when the settings [`enum`](reference/enum.md) and
[`useUnderlyingTypeMethods`](reference/useUnderlyingTypeMethods.md) conflict.
<GH issue="141" pr="142"/>

## v1.4.0

- Add [Enum Support](guide/enum.md) <GH issue="61" pr="136"/>. Can be disabled
Expand Down
37 changes: 37 additions & 0 deletions scenario/enum_underlying_conflict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
input:
input.go: |
package example

// goverter:converter
// goverter:extend ConvertUnderlying
type Converter interface {
// goverter:useUnderlyingTypeMethods
Convert(SqlColor) Color
}

func ConvertUnderlying(s string) string {
return ""
}

type SqlColor string
const SqlColorDefault SqlColor = "default"

type Color string
const ColorDefault Color = "default"
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(github.com/jmattheis/goverter/execution.SqlColor) github.com/jmattheis/goverter/execution.Color

| github.com/jmattheis/goverter/execution.SqlColor
|
source
target
|
| github.com/jmattheis/goverter/execution.Color

The conversion between the types
github.com/jmattheis/goverter/execution.SqlColor
github.com/jmattheis/goverter/execution.Color

does qualify for enum conversion but also match an extend method via useUnderlyingTypeMethods.
You have to disable enum or useUnderlyingTypeMethods to resolve the setting conflict.

0 comments on commit c38fe78

Please sign in to comment.