Skip to content

Commit

Permalink
do not remove new lines or change names in paramgen
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Feb 28, 2024
1 parent 58737e4 commit 37a25a9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
9 changes: 2 additions & 7 deletions paramgen/internal/paramgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
14 changes: 7 additions & 7 deletions paramgen/internal/paramgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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.",
},
},
},
Expand All @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion paramgen/internal/testdata/complex/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 37a25a9

Please sign in to comment.