diff --git a/config/converter.go b/config/converter.go index b5247dfb..0a87aa63 100644 --- a/config/converter.go +++ b/config/converter.go @@ -28,6 +28,7 @@ type ConverterConfig struct { OutputFile string OutputPackage string Extend []*method.Definition + Comments []string } func parseGlobal(loader *pkgload.PackageLoader, global RawLines) (*ConverterConfig, error) { @@ -89,6 +90,8 @@ func parseConverterLine(c *Converter, loader *pkgload.PackageLoader, value strin c.OutputFile, err = parseString(rest) case "output:package": c.OutputPackage, err = parseString(rest) + case "struct:comment": + c.Comments = append(c.Comments, rest) case "extend": var methods []*method.Definition methods, err = parseExtend(loader, c, strings.Fields(rest)) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index bc165755..91acd703 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -16,6 +16,7 @@ - [name](config/name.md) - [output](config/output.md) - [skipCopySameType](config/skipCopySameType.md) + - [struct](config/struct.md) - [useZeroValueOnPointerInconsistency](config/useZeroValueOnPointerInconsistency.md) - [wrapErrors](config/wrapErrors.md) - [Migrations](migrations.md) diff --git a/docs/config/README.md b/docs/config/README.md index 922a3bb8..c195b9d3 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -7,24 +7,26 @@ settings](config/nested.md). These settings can only be defined as [CLI argument](config/define.md#cli) or [converter comment](config/define.md#converter). +- [`converter` marker comment for conversion interfaces](config/converter.md) +- [`extend [PACKAGE:]TYPE...` add custom methods for conversions](config/extend.md) - [`name NAME` rename generated struct](config/name.md) - [`output:file FILE` set the output directory for a converter](config/output.md#file) - [`output:package [PACKAGE:]NAME` set the output package for a converter](config/output.md#package) -- [`extend [PACKAGE:]TYPE...` add custom methods for conversions](config/extend.md) -- [`converter` marker comment for conversion interfaces](config/converter.md) +- [`struct:comment COMMENT` add comments to generated struct](config/struct.md#structcomment-comment) ### Method: These settings can only be defined as [method comment](config/define.md#method). +- [`autoMap PATH` automatically match fields from a sub struct to the target struct](config/autoMap.md) +- [`ignore FIELD...` ignore fields for a struct](config/ignore.md) - [`map [SOURCE-PATH] TARGET [| METHOD]` struct mappings](config/map.md) - [`map SOURCE-FIELD TARGET` define a field mapping](config/map.md#map-source-field-target) - [`map SOURCE-PATH TARGET` define a nested field mapping](config/map.md#map-source-path-target) - [`map . TARGET` map the source type to the target field](config/map.md#map-dot-target) - [`map [SOURCE-PATH] TARGET| METHOD` map the SOURCE-PATH to the TARGET field by using METHOD](config/map.md#map-source-path-target-method) -- [`ignore FIELD...` ignore fields for a struct](config/ignore.md) -- [`autoMap PATH` automatically match fields from a sub struct to the target struct](config/autoMap.md) + #### Method (inheritable) @@ -33,9 +35,9 @@ These settings can be defined as [CLI argument](config/define.md#cli), [method comment](config/define.md#method) and are [inheritable](config/define.md#inheritance). -- [`wrapErrors [yes,no]` wrap errors with extra information](config/wrapErrors.md) -- [`skipCopySameType [yes,no]` skip copying types when the source and target type are the same](config/skipCopySameType.md) -- [`matchIgnoreCase [yes,no]` case-insensitive field matching](config/matchIgnoreCase.md) -- [`ignoreUnexported [yes,no]` ignore unexported struct fields](config/ignoreUnexported.md) - [`ignoreMissing [yes,no]` ignore missing struct fields](config/ignoreMissing.md) +- [`ignoreUnexported [yes,no]` ignore unexported struct fields](config/ignoreUnexported.md) +- [`matchIgnoreCase [yes,no]` case-insensitive field matching](config/matchIgnoreCase.md) +- [`skipCopySameType [yes,no]` skip copying types when the source and target type are the same](config/skipCopySameType.md) - [`useZeroValueOnPointerInconsistency [yes|no]` Use zero values for `*S` to `T` conversions](config/useZeroValueOnPointerInconsistency.md) +- [`wrapErrors [yes,no]` wrap errors with extra information](config/wrapErrors.md) diff --git a/docs/config/struct.md b/docs/config/struct.md new file mode 100644 index 00000000..99d113d6 --- /dev/null +++ b/docs/config/struct.md @@ -0,0 +1,62 @@ +## struct:comment COMMENT + +`struct:comment COMMENT` can be defined as [CLI argument](config/define.md#cli) +or [converter comment](config/define.md#converter). + +`struct:comment` instructs goverter to add a comment line to the generated +struct. It can be configured multiple times to add multiline comments. Prefix +your COMMENT with `//` to force single line comment style. + + + +#### **input.go** + +```go +// goverter:converter +// goverter:struct:comment // MyConverterImpl +// goverter:struct:comment // +// goverter:struct:comment // More detailed +type MultipleSingleLine interface { + Convert(Input) Output +} + +// goverter:converter +// goverter:struct:comment single comment +type SingleComment interface { + Convert(Input) Output +} + +// goverter:converter +// goverter:struct:comment MyConverterImpl +// goverter:struct:comment +// goverter:struct:comment More detailed +type MultiLine interface { + Convert(Input) Output +} + +type Input struct { Name string } +type Output struct { Name string } +``` + +#### **generated/generated.go** + +```go +import example "goverter/example" + +/* +MyConverterImpl + +More detailed +*/ +type MultiLineImpl struct{} + +// MyConverterImpl +// +// More detailed +type MultipleSingleLineImpl struct{} + +// single comment +type SingleCommentImpl struct{} +``` + + diff --git a/generator/generate.go b/generator/generate.go index 7fbe570f..740b2b7f 100644 --- a/generator/generate.go +++ b/generator/generate.go @@ -1,6 +1,8 @@ package generator import ( + "strings" + "github.com/dave/jennifer/jen" "github.com/jmattheis/goverter/builder" "github.com/jmattheis/goverter/config" @@ -49,6 +51,9 @@ func generateConverter(converter *config.Converter, c Config, f *jen.File) error return err } + if len(converter.Comments) > 0 { + f.Comment(strings.Join(converter.Comments, "\n")) + } f.Type().Id(converter.Name).Struct() if err := gen.buildMethods(f); err != nil { diff --git a/scenario/struct_comment.yml b/scenario/struct_comment.yml new file mode 100644 index 00000000..c5bd1143 --- /dev/null +++ b/scenario/struct_comment.yml @@ -0,0 +1,68 @@ +input: + input.go: | + package example + + // goverter:struct:comment // MyConverterImpl + // goverter:struct:comment // + // goverter:struct:comment // More detailed + // goverter:converter + type MultipleSingleLine interface { + Convert(Input) Output + } + + // goverter:converter + // goverter:struct:comment single comment + type SingleComment interface { + Convert(Input) Output + } + + // goverter:converter + // goverter:struct:comment MyConverterImpl + // goverter:struct:comment + // goverter:struct:comment More detailed + type MultiLine interface { + Convert(Input) Output + } + + type Input struct { Name string } + type Output struct { Name string } +success: + - generated/generated.go: | + // Code generated by github.com/jmattheis/goverter, DO NOT EDIT. + + package generated + + import execution "github.com/jmattheis/goverter/execution" + + /* + MyConverterImpl + + More detailed + */ + type MultiLineImpl struct{} + + func (c *MultiLineImpl) Convert(source execution.Input) execution.Output { + var exampleOutput execution.Output + exampleOutput.Name = source.Name + return exampleOutput + } + + // MyConverterImpl + // + // More detailed + type MultipleSingleLineImpl struct{} + + func (c *MultipleSingleLineImpl) Convert(source execution.Input) execution.Output { + var exampleOutput execution.Output + exampleOutput.Name = source.Name + return exampleOutput + } + + // single comment + type SingleCommentImpl struct{} + + func (c *SingleCommentImpl) Convert(source execution.Input) execution.Output { + var exampleOutput execution.Output + exampleOutput.Name = source.Name + return exampleOutput + }