diff --git a/builder/struct.go b/builder/struct.go index 0c60f787..68ae7b51 100644 --- a/builder/struct.go +++ b/builder/struct.go @@ -268,7 +268,7 @@ func mapField( Params: method.ParamsNone, ContextMatch: config.StructMethodContextRegex, CustomCall: nextIDCode, - }) + }, method.EmptyLocalOpts) if err != nil { return nil, nil, nil, nil, false, NewError(err.Error()).Lift(lift...) } diff --git a/config/converter.go b/config/converter.go index 6abce9ee..41a7e369 100644 --- a/config/converter.go +++ b/config/converter.go @@ -4,7 +4,6 @@ import ( "fmt" "go/types" "path/filepath" - "regexp" "strings" "github.com/jmattheis/goverter/config/parse" @@ -26,8 +25,7 @@ const ( ) var DefaultCommon = Common{ - Enum: enum.Config{Enabled: true}, - ArgContextRegex: regexp.MustCompile("^ctx|^context"), + Enum: enum.Config{Enabled: true}, } var DefaultConfigInterface = ConverterConfig{ @@ -124,12 +122,12 @@ func initConverter(loader *pkgload.PackageLoader, rawConverter *RawConverter) (* if rawConverter.InterfaceName != "" { c.ConverterConfig = DefaultConfigInterface - v, err := loader.GetOneRaw(c.Package, rawConverter.InterfaceName) + _, interfaceObj, err := loader.GetOneRaw(c.Package, rawConverter.InterfaceName) if err != nil { return nil, err } - c.typ = v.Type() + c.typ = interfaceObj.Type() c.Name = rawConverter.InterfaceName + "Impl" return c, nil } diff --git a/config/method.go b/config/method.go index 8ecbe5ef..336855a2 100644 --- a/config/method.go +++ b/config/method.go @@ -30,6 +30,7 @@ type Method struct { Location string updateParam string + localOpts method.LocalOpts } type FieldMapping struct { @@ -61,11 +62,11 @@ func parseMethods(ctx *context, rawConverter *RawConverter, c *Converter) error return nil } for name, lines := range rawConverter.Methods { - fun, err := ctx.Loader.GetOneRaw(c.Package, name) + _, fn, err := ctx.Loader.GetOneRaw(c.Package, name) if err != nil { return err } - def, err := parseMethod(ctx, c, fun, lines) + def, err := parseMethod(ctx, c, fn, lines) if err != nil { return err } @@ -80,6 +81,7 @@ func parseMethod(ctx *context, c *Converter, obj types.Object, rawMethod RawLine Fields: map[string]*FieldMapping{}, Location: rawMethod.Location, EnumMapping: &EnumMapping{Map: map[string]string{}}, + localOpts: method.LocalOpts{Context: map[string]bool{}}, } for _, value := range rawMethod.Lines { @@ -97,7 +99,7 @@ func parseMethod(ctx *context, c *Converter, obj types.Object, rawMethod RawLine ContextMatch: m.ArgContextRegex, Generated: true, UpdateParam: m.updateParam, - }) + }, m.localOpts) m.Definition = def @@ -137,6 +139,10 @@ func parseMethodLine(ctx *context, c *Converter, m *Method, value string) (err e } case "update": m.updateParam, err = parse.String(rest) + case "context": + var key string + key, err = parse.String(rest) + m.localOpts.Context[key] = true case "enum:map": fields := strings.Fields(rest) if len(fields) != 2 { diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index b2d7c7bd..bec04631 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -100,6 +100,7 @@ export default defineConfig({ collapsed: true, items: [ { text: "autoMap", link: "/reference/autoMap" }, + { text: "context", link: "/reference/context" }, { text: "default", link: "/reference/default" }, { text: "ignore", link: "/reference/ignore" }, { text: "map", link: "/reference/map" }, diff --git a/docs/changelog.md b/docs/changelog.md index 0d554760..6159f4c5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,8 +6,13 @@ import GH from './GH.vue'; ## unreleased -- Fix not setting `nil` on map when value is `nil`. - Add [`default:update`](/reference/default.md#default-update-yes-no) +- Add [`context`](./reference/context.md). See [Guide: Pass context to + functions](./guide/context.md) +- Remove default value of [`arg:context:regex`](./reference/arg.md) + - To get v1.6.0 behavior, configure: `goverter:arg:context:regex ^ctx|^context` + - The recommended way to configure context is to use [`context`](./reference/context.md). +- Fix not setting `nil` on map when value is `nil`. ## v1.6.0 diff --git a/docs/guide/context.md b/docs/guide/context.md index 330aff93..51c90190 100644 --- a/docs/guide/context.md +++ b/docs/guide/context.md @@ -1,9 +1,7 @@ # How to pass context to custom functions You can pass additional parameters to custom functions by defining them as -[context](../reference/signature.md#categories). This can be done by prefixing -the parameter names with `context` or `ctx`. Use this guide to get a broad -overview on how to use this feature. +[`context`](../reference/context.md). If we want to format a `time.Time` to `string` but have requirements so that the date format must be changeable at runtime. You can define the time format diff --git a/docs/reference/arg.md b/docs/reference/arg.md index 37c9aaea..4f126dbe 100644 --- a/docs/reference/arg.md +++ b/docs/reference/arg.md @@ -6,7 +6,10 @@ argument](./define-settings.md#cli), [conversion comment](./define-settings.md#conversion) or [method comment](./define-settings.md#method). This setting is -[inheritable](./define-settings.md#inheritance). Default `^ctx|^context`. +[inheritable](./define-settings.md#inheritance). Default _unset_. + +`arg:context:regex` allows you define a regex that automatically defines +arguments as [`context`](./context.md) if the name matches. ::: code-group <<< @../../example/context/regex/input.go diff --git a/docs/reference/context.md b/docs/reference/context.md new file mode 100644 index 00000000..92decec4 --- /dev/null +++ b/docs/reference/context.md @@ -0,0 +1,25 @@ +# Setting: context + +## context ARG + +`context ARG` is a can be defined as +[custom function comment](./define-settings.md#custom-function). + +`context` defines the `ARG` as context. "Context" are additional arguments that +may be used in other custom functions like [`default`](./default.md), +[`extend`](./extend.md), or +[`map CUSTOM`](./map.md#map-source-path-target-method). + + +::: details Example (click me) +::: code-group +<<< @../../example/context/database/input.go +<<< @../../example/context/database/generated/generated.go [generated/generated.go] +::: + + +::: details Example 2 (click me) +::: code-group +<<< @../../example/context/date-format/input.go +<<< @../../example/context/date-format/generated/generated.go [generated/generated.go] +::: diff --git a/docs/reference/define-settings.md b/docs/reference/define-settings.md index 4e915784..603456c2 100644 --- a/docs/reference/define-settings.md +++ b/docs/reference/define-settings.md @@ -80,6 +80,17 @@ var ( ) ``` +## Custom Function + +You can define settings for custom functions by prefixing them with `goverter:`. + +```go +// goverter:setting abc +func FormatInt(value int, abc context.Context) { + return strconv.Itoa(value) +} +``` + ### Inheritance Method settings can be inherited for all methods if they are defined on the CLI diff --git a/docs/reference/settings.md b/docs/reference/settings.md index 22e1e748..51511d4d 100644 --- a/docs/reference/settings.md +++ b/docs/reference/settings.md @@ -26,6 +26,7 @@ These settings can only be defined as [CLI argument](./define-settings.md#cli) o These settings can only be defined as [method comment](./define-settings.md#method). - [`autoMap PATH` automatically match fields from a sub struct to the target struct](./autoMap.md) +- [`context ARG` define an argument as context](./context.md) - [`default [PACKAGE:]FUNC` define default target value](./default.md) - [`enum:map SOURCE TARGET` define an enum value mapping](./enum.md#enum-map-source-target) - [`enum:transform ID CONFIG` use an enum value transformer](./enum.md#enum-transform-id-config) @@ -57,3 +58,9 @@ These settings can be defined as [CLI argument](./define-settings.md#cli), - [`useZeroValueOnPointerInconsistency [yes|no]` Use zero values for `*S` to `T` conversions](./useZeroValueOnPointerInconsistency.md) - [`wrapErrorsUsing [PACKAGE]` wrap errors using a custom implementation](./wrapErrorsUsing.md) - [`wrapErrors [yes,no]` wrap errors with extra information](./wrapErrors.md) + +## Custom Function + +These settings can only be defined as [custom function comment](./define-settings.md#custom-function). + +- [`context ARG` define an argument as context](./context.md) diff --git a/docs/reference/signature.md b/docs/reference/signature.md index f715e67b..302d47a1 100644 --- a/docs/reference/signature.md +++ b/docs/reference/signature.md @@ -34,15 +34,16 @@ Goverter groups **params** and **results** into these categories: 1. `source` is a **param** that will be converted to the `target` type. Any **param** that is not a `context` type is a `source` type. -1. `target` is the **first result** which `sources` are converted to. +1. `target` is the **first result** or the argument defined with + [`update`](./update.md) which `sources` are converted to. 1. `target_error` is the **second result** type which can be specified if the conversion fails. `target_error` must be of type [`error`](https://go.dev/tour/methods/19) -1. `context` are **[named](#named-paramsresults) params** where the name starts - with `ctx` or `context`. The regex can be adjusted via - [`arg:context:regex`](./arg.md#arg-context-regex). They are used in - [custom functions](#custom-function) for manual conversion. `context` types - aren't used for automatic conversion. +1. `context` are **[named](#named-paramsresults) params** where the argument is + defined with [`context`](./context.md). Or matching the regex + [`arg:context:regex`](./arg.md#arg-context-regex) They are used in [custom + functions](#custom-function) for manual conversion. `context` types aren't + used for automatic conversion. ### Default context @@ -87,10 +88,13 @@ type Converter interface { ConvertTwo(source A) (B, error) // A=source; B=target; error=target_error - ConvertThree(source A, ctx B) C + // goverter:context b + ConvertThree(source A, b B) C // A=source; B=context; B=target - ConvertFour(ctxOne A, source B, ctxSecond C) (D, error) + // goverter:context one + // goverter:context two + ConvertFour(one A, source B, two C) (D, error) // A=context; B=source; C=context; D=target; error=target_error } ``` @@ -115,6 +119,7 @@ type Converter interface { // A=source; B=target ConvertTwo(source A, target B) error // A=source; B=target; error=target_error + // goverter:context ctx ConvertThree(target A, source B, ctx C) // A=target; B=source; C=context } @@ -146,14 +151,15 @@ func ConvertThree(input A) B {/**/} func ConvertFour(input A) (B, error) {/**/} // A=source; B=target; error=target_error -func ConvertFour(ctxOne A, ctxTwo B) C {/**/} +// goverter:context one +// goverter:context two +func ConvertFour(one A, two B) C {/**/} // A=context; B=context; C=target -func ConvertFive(input A, ctxOne B, ctxTwo C) D {/**/} +// goverter:context one +// goverter:context two +func ConvertFive(input A, one B, two C) D {/**/} // A=source; B=context; C=context; D=target - -func ConvertSix(ctxOne A, source B ctxTwo C) (D, error) {/**/} -// A=context; B=source; C=context; D=target; error=target_error ``` ::: details Example (click to expand) @@ -180,13 +186,18 @@ func ConvertOne(input A) B {/**/} func ConvertTwo(input A) (B, error) {/**/} // A=source; B=target; error=target_error -func ConvertThree(input A, ctxOne B) C {/**/} +// goverter:context one +func ConvertThree(input A, one B) C {/**/} // A=source; B=context; C=target -func ConvertFour(input A, ctxOne B, ctxTwo C) D {/**/} +// goverter:context one +// goverter:context two +func ConvertFour(input A, one B, two C) D {/**/} // A=source; B=context; C=context; D=target -func ConvertFive(ctxOne A, source B ctxTwo C) (D, error) {/**/} +// goverter:context one +// goverter:context two +func ConvertFive(one A, source B, two C) (D, error) {/**/} // A=context; B=source; C=context; D=target; error=target_error ``` diff --git a/example/context/database/input.go b/example/context/database/input.go index 84cce3f9..6244cccc 100644 --- a/example/context/database/input.go +++ b/example/context/database/input.go @@ -3,11 +3,13 @@ package example // goverter:converter type Converter interface { // goverter:map ID Editable | QueryEditable - Convert(source PostInput, ctxDatabase Database) (PostOutput, error) + // goverter:context db + Convert(source PostInput, db Database) (PostOutput, error) } -func QueryEditable(id int, ctxDatabase Database) bool { - return ctxDatabase.AllowedToEdit(id) +// goverter:context db +func QueryEditable(id int, db Database) bool { + return db.AllowedToEdit(id) } type Database interface { diff --git a/example/context/date-format/input.go b/example/context/date-format/input.go index 0571fe3d..6ef32f43 100644 --- a/example/context/date-format/input.go +++ b/example/context/date-format/input.go @@ -5,11 +5,13 @@ import "time" // goverter:converter // goverter:extend FormatTime type Converter interface { - Convert(source map[string]Input, ctxFormat string) map[string]Output + // goverter:context dateFormat + Convert(source map[string]Input, dateFormat string) map[string]Output } -func FormatTime(t time.Time, ctxFormat string) string { - return t.Format(ctxFormat) +// goverter:context dateFormat +func FormatTime(t time.Time, dateFormat string) string { + return t.Format(dateFormat) } type Input struct { diff --git a/method/parse.go b/method/parse.go index 32631c2b..f10cdf3f 100644 --- a/method/parse.go +++ b/method/parse.go @@ -35,8 +35,14 @@ type ParseOpts struct { UpdateParam string } +type LocalOpts struct { + Context map[string]bool +} + +var EmptyLocalOpts = LocalOpts{Context: map[string]bool{}} + // Parse parses an function into a Definition. -func Parse(obj types.Object, opts *ParseOpts) (*Definition, error) { +func Parse(obj types.Object, opts *ParseOpts, localOpts LocalOpts) (*Definition, error) { methodDef := &Definition{ ID: obj.String(), OriginID: obj.String(), @@ -94,7 +100,7 @@ func Parse(obj types.Object, opts *ParseOpts) (*Definition, error) { default: return nil, formatErr("The signature one non 'error' result or multiple results is not supported for goverter:update signatures.") } - case opts.ContextMatch.MatchString(arg.Name): + case (opts.ContextMatch != nil && opts.ContextMatch.MatchString(arg.Name)) || localOpts.Context[arg.Name]: methodDef.Context[arg.Type.String] = arg.Type arg.Use = ArgUseContext case methodDef.Source == nil: diff --git a/pkgload/pkgload.go b/pkgload/pkgload.go index 1136848c..8bafad0e 100644 --- a/pkgload/pkgload.go +++ b/pkgload/pkgload.go @@ -2,10 +2,12 @@ package pkgload import ( "fmt" + "go/ast" "go/types" "regexp" "strings" + "github.com/jmattheis/goverter/config/parse" "github.com/jmattheis/goverter/method" "golang.org/x/tools/go/packages" ) @@ -13,6 +15,7 @@ import ( func New(workDir, buildTags string, paths []string) (*PackageLoader, error) { loader := &PackageLoader{ lookup: map[string]*packages.Package{}, + locals: map[string]map[string]method.LocalOpts{}, } err := loader.load(workDir, buildTags, paths) return loader, err @@ -20,6 +23,7 @@ func New(workDir, buildTags string, paths []string) (*PackageLoader, error) { type PackageLoader struct { lookup map[string]*packages.Package + locals map[string]map[string]method.LocalOpts } func (g *PackageLoader) GetMatching(cwd, fullMethod string, opts *method.ParseOpts) ([]*method.Definition, error) { @@ -62,7 +66,7 @@ func (g *PackageLoader) GetMatching(cwd, fullMethod string, opts *method.ParseOp } obj := scope.Lookup(name) - m, err := method.Parse(obj, opts) + m, err := method.Parse(obj, opts, g.localConfig(pkg, name)) if err == nil { matches = append(matches, m) } @@ -93,17 +97,50 @@ func (g *PackageLoader) getPkg(pkgName string) (*packages.Package, error) { return pkg, nil } -func (g *PackageLoader) GetOneRaw(pkgName, name string) (types.Object, error) { +func (g *PackageLoader) localConfig(pkg *packages.Package, name string) method.LocalOpts { + fns, ok := g.locals[pkg.PkgPath] + if !ok { + fns = map[string]method.LocalOpts{} + for _, file := range pkg.Syntax { + for _, decl := range file.Decls { + if fn, ok := decl.(*ast.FuncDecl); ok { + lines := parse.SettingLines(fn.Doc.Text()) + if len(lines) == 0 { + continue + } + + contexts := map[string]bool{} + for _, line := range lines { + if cmd, rest := parse.Command(line); cmd == "context" { + if ctx, err := parse.String(rest); err == nil { + contexts[ctx] = true + } + } + } + fns[fn.Name.Name] = method.LocalOpts{Context: contexts} + } + } + } + g.locals[pkg.PkgPath] = fns + } + fn, ok := fns[name] + if !ok { + return method.EmptyLocalOpts + } + return fn +} + +func (g *PackageLoader) GetOneRaw(pkgName, name string) (*packages.Package, types.Object, error) { pkg, err := g.getPkg(pkgName) if err != nil { - return nil, err + return pkg, nil, err } obj := pkg.Types.Scope().Lookup(name) if obj == nil { - return nil, fmt.Errorf("%q does not exist in package %q", name, pkgName) + return pkg, nil, fmt.Errorf("%q does not exist in package %q", name, pkgName) } - return obj, nil + return pkg, obj, nil } func (g *PackageLoader) GetOne(cwd, fullMethod string, opts *method.ParseOpts) (*method.Definition, error) { @@ -115,12 +152,12 @@ func (g *PackageLoader) GetOne(cwd, fullMethod string, opts *method.ParseOpts) ( } func (g *PackageLoader) getOneParsed(pkgName, name string, opts *method.ParseOpts) (*method.Definition, error) { - obj, err := g.GetOneRaw(pkgName, name) + pkg, obj, err := g.GetOneRaw(pkgName, name) if err != nil { return nil, err } - def, err := method.Parse(obj, opts) + def, err := method.Parse(obj, opts, g.localConfig(pkg, name)) if err != nil { return nil, err } @@ -130,7 +167,7 @@ func (g *PackageLoader) getOneParsed(pkgName, name string, opts *method.ParseOpt // loadPackages is used to load extend packages, with caching support. func (g *PackageLoader) load(workDir, buildTags string, paths []string) error { packagesCfg := &packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo, + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax, Dir: workDir, } if buildTags != "" { diff --git a/scenario/context_error_duplicated_type.yml b/scenario/context_error_duplicated_type.yml index 4c04ae0c..5b5531cb 100644 --- a/scenario/context_error_duplicated_type.yml +++ b/scenario/context_error_duplicated_type.yml @@ -5,9 +5,12 @@ input: // goverter:converter // goverter:extend DoLookup type Converter interface { + // goverter:context ctx1 + // goverter:context ctx2 Convert(source []string, ctx1 map[string]Output, ctx2 map[string]Output) []Output } + // goverter:context ctx func DoLookup(id string, ctx map[string]Output) Output { return ctx[id] } diff --git a/scenario/context_error_missing.yml b/scenario/context_error_missing.yml index 596424fa..0ec4e50e 100644 --- a/scenario/context_error_missing.yml +++ b/scenario/context_error_missing.yml @@ -8,6 +8,7 @@ input: Convert(source []string) []Output } + // goverter:context ctx func DoLookup(id string, ctx map[string]Output) Output { return ctx[id] } diff --git a/scenario/context_error_not_satisfies.yml b/scenario/context_error_not_satisfies.yml index 68451b87..9ec3666a 100644 --- a/scenario/context_error_not_satisfies.yml +++ b/scenario/context_error_not_satisfies.yml @@ -5,9 +5,11 @@ input: // goverter:converter // goverter:extend DoLookup type Converter interface { + // goverter:context ctx Convert(source []string, ctx map[string]int) []Output } + // goverter:context ctx func DoLookup(id string, ctx map[string]Output) Output { return ctx[id] } @@ -18,7 +20,7 @@ input: } error: |- Error while creating converter method: - @workdir/input.go:6 + @workdir/input.go:7 func (github.com/jmattheis/goverter/execution.Converter).Convert(source []string, ctx map[string]int) []github.com/jmattheis/goverter/execution.Output [source] []string [context] map[string]int diff --git a/scenario/context_error_not_satisfies2.yml b/scenario/context_error_not_satisfies2.yml index 7a93dc96..eed5aada 100644 --- a/scenario/context_error_not_satisfies2.yml +++ b/scenario/context_error_not_satisfies2.yml @@ -6,13 +6,16 @@ input: // goverter:extend DoLookup // goverter:extend DoOther type Converter interface { + // goverter:context ctx Convert(source []string, ctx map[string]int) []Output } + // goverter:context ctx func DoLookup(id string, ctx map[string]Output) Output { return ctx[id] } + // goverter:context ctx func DoOther(id string, ctx map[string]string) Output { return Output{} } @@ -23,7 +26,7 @@ input: } error: |- Error while creating converter method: - @workdir/input.go:7 + @workdir/input.go:8 func (github.com/jmattheis/goverter/execution.Converter).Convert(source []string, ctx map[string]int) []github.com/jmattheis/goverter/execution.Output [source] []string [context] map[string]int diff --git a/scenario/context_error_partly_satisfied.yml b/scenario/context_error_partly_satisfied.yml index a7c03352..6bf676c6 100644 --- a/scenario/context_error_partly_satisfied.yml +++ b/scenario/context_error_partly_satisfied.yml @@ -5,9 +5,12 @@ input: // goverter:converter // goverter:extend DoLookup type Converter interface { + // goverter:context ctx Convert(source []string, ctx map[string]Output) []Output } + // goverter:context ctx + // goverter:context ctx2 func DoLookup(id string, ctx map[string]Output, ctx2 map[string]int) Output { return ctx[id] } @@ -18,7 +21,7 @@ input: } error: |- Error while creating converter method: - @workdir/input.go:6 + @workdir/input.go:7 func (github.com/jmattheis/goverter/execution.Converter).Convert(source []string, ctx map[string]github.com/jmattheis/goverter/execution.Output) []github.com/jmattheis/goverter/execution.Output [source] []string [context] map[string]github.com/jmattheis/goverter/execution.Output diff --git a/scenario/context_extend_necessary.yml b/scenario/context_extend_necessary.yml index 0fb08447..960fd558 100644 --- a/scenario/context_extend_necessary.yml +++ b/scenario/context_extend_necessary.yml @@ -6,13 +6,17 @@ input: // goverter:extend LookupInt // goverter:extend LookupBool type Converter interface { + // goverter:context ctxInt + // goverter:context ctxBool Convert(source []Input, ctxInt map[string]int, ctxBool map[string]bool) []Output } + // goverter:context ctx func LookupBool(id string, ctx map[string]bool) bool { return ctx[id] } + // goverter:context ctx func LookupInt(id string, ctx map[string]int) int { return ctx[id] } diff --git a/scenario/context_extend_one.yml b/scenario/context_extend_one.yml index 2d61909a..a60b08d5 100644 --- a/scenario/context_extend_one.yml +++ b/scenario/context_extend_one.yml @@ -5,9 +5,11 @@ input: // goverter:converter // goverter:extend DoLookup type Converter interface { + // goverter:context ctxLookup Convert(source []string, ctxLookup map[string]Output) []Output } + // goverter:context ctx func DoLookup(id string, ctx map[string]Output) Output { return ctx[id] } diff --git a/scenario/context_extend_two.yml b/scenario/context_extend_two.yml index b105d8a0..b116d779 100644 --- a/scenario/context_extend_two.yml +++ b/scenario/context_extend_two.yml @@ -5,9 +5,13 @@ input: // goverter:converter // goverter:extend DoLookup type Converter interface { + // goverter:context ctxOther + // goverter:context ctxLookup Convert(source []string, ctxOther map[string]int, ctxLookup map[string]Output) []Output } + // goverter:context ctx + // goverter:context ctx2 func DoLookup(id string, ctx map[string]Output, ctx2 map[string]int) Output { return ctx[id] } diff --git a/scenario/context_extend_two2.yml b/scenario/context_extend_two2.yml index 93ad3447..cb03173d 100644 --- a/scenario/context_extend_two2.yml +++ b/scenario/context_extend_two2.yml @@ -5,9 +5,13 @@ input: // goverter:converter // goverter:extend DoLookup type Converter interface { + // goverter:context ctxOther + // goverter:context ctxLookup Convert(source []string, ctxOther map[string]int, ctxLookup map[string]Output) []Output } + // goverter:context ctx + // goverter:context ctx2 func DoLookup(ctx map[string]Output, id string, ctx2 map[string]int) Output { return ctx[id] } diff --git a/scenario/context_nested_notneeded.yml b/scenario/context_nested_notneeded.yml index 7a50fcaa..104a0c6e 100644 --- a/scenario/context_nested_notneeded.yml +++ b/scenario/context_nested_notneeded.yml @@ -4,6 +4,7 @@ input: // goverter:converter type Converter interface { + // goverter:context ctx Convert(source DBModel, ctx string) ApiModel } diff --git a/scenario/context_override_extend.yml b/scenario/context_override_extend.yml index e95df352..a171e750 100644 --- a/scenario/context_override_extend.yml +++ b/scenario/context_override_extend.yml @@ -6,6 +6,7 @@ input: // goverter:extend Two // goverter:extend One type Conv1 interface { + // goverter:context ctx Convert(source []string, ctx map[string]Output) []Output } @@ -13,9 +14,11 @@ input: // goverter:extend One // goverter:extend Two type Conv2 interface { + // goverter:context ctx Convert(source []string, ctx map[string]Output) []Output } + // goverter:context ctx func One(id string, ctx map[string]Output) Output { return ctx[id] } func Two(id string) Output { return Output{} } diff --git a/scenario/delegate_context.yml b/scenario/delegate_context.yml index b291a73a..7837fdf0 100644 --- a/scenario/delegate_context.yml +++ b/scenario/delegate_context.yml @@ -5,6 +5,7 @@ input: // goverter:converter // goverter:extend InputToOutput type Converter interface { + // goverter:context ctx Convert(source Input, ctx string) Output } @@ -16,6 +17,7 @@ input: ID int Age string } + // goverter:context ctx func InputToOutput(source Input, ctx string) Output { return Output{} } diff --git a/scenario/delegate_context_missing.yml b/scenario/delegate_context_missing.yml index 8479c8c2..1c298958 100644 --- a/scenario/delegate_context_missing.yml +++ b/scenario/delegate_context_missing.yml @@ -16,6 +16,7 @@ input: ID int Age string } + // goverter:context ctx func InputToOutput(source Input, ctx string) Output { return Output{} } diff --git a/scenario/duplicate_signature_context.yml b/scenario/duplicate_signature_context.yml index 18d248e8..e4405c6e 100644 --- a/scenario/duplicate_signature_context.yml +++ b/scenario/duplicate_signature_context.yml @@ -4,6 +4,7 @@ input: // goverter:converter type Converter interface { + // goverter:context ctx Convert(source Input, ctx string) Output Convert2(source Input) Output } diff --git a/scenario/duplicate_signature_different_context.yml b/scenario/duplicate_signature_different_context.yml index 990e32ca..8de41133 100644 --- a/scenario/duplicate_signature_different_context.yml +++ b/scenario/duplicate_signature_different_context.yml @@ -4,7 +4,9 @@ input: // goverter:converter type Converter interface { + // goverter:context ctx Convert(source Input, ctx string) Output + // goverter:context ctx Convert2(source Input, ctx int) Output } diff --git a/scenario/duplicate_signature_overlapping_context.yml b/scenario/duplicate_signature_overlapping_context.yml index 8e5dbcf2..802c33dd 100644 --- a/scenario/duplicate_signature_overlapping_context.yml +++ b/scenario/duplicate_signature_overlapping_context.yml @@ -4,7 +4,10 @@ input: // goverter:converter type Converter interface { + // goverter:context ctxI + // goverter:context ctxS Convert(source Input, ctxI int, ctxS string) Output + // goverter:context ctxI Convert2(source Input, ctxI int) Output } diff --git a/scenario/duplicate_signature_overlapping_context2.yml b/scenario/duplicate_signature_overlapping_context2.yml index 0bfcdb9b..c0517b6d 100644 --- a/scenario/duplicate_signature_overlapping_context2.yml +++ b/scenario/duplicate_signature_overlapping_context2.yml @@ -4,7 +4,10 @@ input: // goverter:converter type Converter interface { + // goverter:context ctxI Convert(source Input, ctxI int) Output + // goverter:context ctxI + // goverter:context ctxS Convert2(source Input, ctxI int, ctxS string) Output } diff --git a/scenario/duplicate_signature_same_context.yml b/scenario/duplicate_signature_same_context.yml index 3ee4ae44..94d30d47 100644 --- a/scenario/duplicate_signature_same_context.yml +++ b/scenario/duplicate_signature_same_context.yml @@ -4,7 +4,9 @@ input: // goverter:converter type Converter interface { + // goverter:context ctx Convert(source Input, ctx string) Output + // goverter:context ctx Convert2(source Input, ctx string) Output }