From c38fe7861c03a721adee673f72de20c12f9c14d8 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sun, 31 Mar 2024 12:07:54 +0200 Subject: [PATCH] fix: error when enum and useUnderlyingTypeMethods conflict --- builder/enum.go | 4 +++ builder/underlying.go | 11 +++++++++ docs/changelog.md | 6 +++++ scenario/enum_underlying_conflict.md | 37 ++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 scenario/enum_underlying_conflict.md diff --git a/builder/enum.go b/builder/enum.go index 02feb734..b291910f 100644 --- a/builder/enum.go +++ b/builder/enum.go @@ -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 diff --git a/builder/underlying.go b/builder/underlying.go index 49e33b5c..c02e192c 100644 --- a/builder/underlying.go +++ b/builder/underlying.go @@ -1,6 +1,8 @@ package builder import ( + "fmt" + "github.com/dave/jennifer/jen" "github.com/jmattheis/goverter/xtype" ) @@ -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 + +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)) + } + sourceUnderlying, targetUnderlying := findUnderlyingExtendMapping(ctx, source, target) innerSource := source diff --git a/docs/changelog.md b/docs/changelog.md index 625cc35f..e6dee378 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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. + + ## v1.4.0 - Add [Enum Support](guide/enum.md) . Can be disabled diff --git a/scenario/enum_underlying_conflict.md b/scenario/enum_underlying_conflict.md new file mode 100644 index 00000000..57a11c6c --- /dev/null +++ b/scenario/enum_underlying_conflict.md @@ -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.