From 37a25a92f6c304523024e845b25c4062d764fa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Ma=C5=BEgon?= Date: Wed, 28 Feb 2024 20:25:45 +0100 Subject: [PATCH] do not remove new lines or change names in paramgen --- paramgen/internal/paramgen.go | 9 ++------- paramgen/internal/paramgen_test.go | 14 +++++++------- paramgen/internal/testdata/complex/specs.go | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/paramgen/internal/paramgen.go b/paramgen/internal/paramgen.go index 134e087..42f2845 100644 --- a/paramgen/internal/paramgen.go +++ b/paramgen/internal/paramgen.go @@ -551,10 +551,7 @@ func (p *parameterParser) formatFieldComment(f *ast.Field, fieldName, paramName // fallback to line comment doc = f.Comment } - c := strings.ReplaceAll(doc.Text(), fieldName, paramName) - if len(c) == 0 { - return c - } + c := doc.Text() whitespacePrefix := "" for _, r := range c { @@ -568,10 +565,8 @@ func (p *parameterParser) formatFieldComment(f *ast.Field, fieldName, paramName c = strings.TrimPrefix(c, whitespacePrefix) // get rid of whitespace in front of all other lines c = strings.ReplaceAll(c, "\n"+whitespacePrefix, "\n") - // get rid of new lines and use a space instead - c = strings.ReplaceAll(c, "\n", " ") // trim space (get rid of any eventual new lines at the end) - c = strings.Trim(c, " ") + c = strings.TrimRight(c, " \n") return c } diff --git a/paramgen/internal/paramgen_test.go b/paramgen/internal/paramgen_test.go index 3bb69e4..377fca5 100644 --- a/paramgen/internal/paramgen_test.go +++ b/paramgen/internal/paramgen_test.go @@ -37,14 +37,14 @@ func TestParseSpecificationSuccess(t *testing.T) { want: map[string]config.Parameter{ "foo": { Default: "bar", - Description: "foo is a required field in the global config with the name \"foo\" and default value \"bar\".", + Description: "MyGlobalString is a required field in the global config with the name\n\"foo\" and default value \"bar\".", Type: config.ParameterTypeString, Validations: []config.Validation{ config.ValidationRequired{}, }, }, "myString": { - Description: "myString my string description", + Description: "MyString my string description", Type: config.ParameterTypeString, }, "myBool": {Type: config.ParameterTypeBool}, @@ -85,21 +85,21 @@ func TestParseSpecificationSuccess(t *testing.T) { want: map[string]config.Parameter{ "global.duration": { Default: "1s", - Description: "duration does not have a name so the type name is used.", + Description: "Duration does not have a name so the type name is used.", Type: config.ParameterTypeDuration, }, "nestMeHere.anotherNested": { Type: config.ParameterTypeInt, - Description: "nestMeHere.anotherNested is also nested under nestMeHere. This is a block comment.", + Description: "AnotherNested is also nested under nestMeHere.\nThis is a block comment.", }, "nestMeHere.formatThisName": { Type: config.ParameterTypeFloat, Default: "this is not a float", - Description: "formatThisName should become \"formatThisName\". Default is not a float but that's not a problem, paramgen does not validate correctness.", + Description: "FORMATThisName should stay \"FORMATThisName\". Default is not a float\nbut that's not a problem, paramgen does not validate correctness.", }, "customType": { Type: config.ParameterTypeDuration, - Description: "customType uses a custom type that is convertible to a supported type. Line comments are allowed.", + Description: "CustomType uses a custom type that is convertible to a supported type. Line comments are allowed.", }, }, }, @@ -114,7 +114,7 @@ func TestParseSpecificationSuccess(t *testing.T) { }, "my-param": { Type: config.ParameterTypeInt, - Description: "my-param i am a parameter comment", + Description: "Param1 i am a parameter comment", Default: "3", Validations: []config.Validation{ config.ValidationRequired{}, diff --git a/paramgen/internal/testdata/complex/specs.go b/paramgen/internal/testdata/complex/specs.go index 87fb85e..a6aea79 100644 --- a/paramgen/internal/testdata/complex/specs.go +++ b/paramgen/internal/testdata/complex/specs.go @@ -22,7 +22,7 @@ type SourceConfig struct { Global GlobalConfig `json:"global"` // Nested structs can be used to create namespaces Nested struct { - // FORMATThisName should become "formatThisName". Default is not a float + // FORMATThisName should stay "FORMATThisName". Default is not a float // but that's not a problem, paramgen does not validate correctness. FORMATThisName float32 `default:"this is not a float"` // unexported fields should be ignored.