Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.

Commit aadc4d3

Browse files
committed
skip prefix in envconf annotation if prefix set to '-' or '_'
1 parent d5dc929 commit aadc4d3

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

cmd/typical-rest-server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ func main() {
1717
if err := typapp.StartApp(app.Start, app.Shutdown); err != nil {
1818
logrus.Fatal(err.Error())
1919
}
20+
2021
}

pkg/typcfg/envconfig_annot.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,18 @@ func createFields(structDecl *typgen.StructDecl, prefix string) []*Field {
180180
// CreateField create new instance of field
181181
func CreateField(prefix string, field *typgen.Field) *Field {
182182
// NOTE: mimic kelseyhightower/envconfig struct tags
183-
184183
name := field.Get("envconfig")
185184
if name == "" {
186185
name = strings.ToUpper(field.Names[0])
187186
}
188187

188+
key := name
189+
if prefix != "" {
190+
key = fmt.Sprintf("%s_%s", prefix, name)
191+
}
192+
189193
return &Field{
190-
Key: fmt.Sprintf("%s_%s", prefix, name),
194+
Key: key,
191195
Default: field.Get("default"),
192196
Required: field.Get("required") == "true",
193197
}
@@ -200,7 +204,10 @@ func getCtorName(annot *typgen.Directive) string {
200204
func getPrefix(annot *typgen.Directive) string {
201205
prefix := annot.TagParam.Get("prefix")
202206
if prefix == "" {
203-
prefix = strings.ToUpper(annot.GetName())
207+
return strings.ToUpper(annot.GetName())
208+
}
209+
if prefix == "-" || prefix == "_" {
210+
return ""
204211
}
205212
return prefix
206213
}

pkg/typcfg/envconfig_annot_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,34 @@ func TestCfgAnnotation_Annotate_GenerateDotEnvAndUsageDoc(t *testing.T) {
108108
},
109109
},
110110
},
111+
{
112+
TagName: "@envconfig",
113+
TagParam: `prefix:"-"`,
114+
Decl: &typgen.Decl{
115+
File: typgen.File{Package: "mypkg"},
116+
Type: &typgen.StructDecl{
117+
TypeDecl: typgen.TypeDecl{Name: "SomeSample"},
118+
Fields: []*typgen.Field{
119+
{Names: []string{"SomeField3"}, Type: "string", StructTag: `default:"some-text"`},
120+
{Names: []string{"SomeField4"}, Type: "int", StructTag: `default:"9876"`},
121+
},
122+
},
123+
},
124+
},
125+
{
126+
TagName: "@envconfig",
127+
TagParam: `prefix:"_"`,
128+
Decl: &typgen.Decl{
129+
File: typgen.File{Package: "mypkg"},
130+
Type: &typgen.StructDecl{
131+
TypeDecl: typgen.TypeDecl{Name: "SomeSample"},
132+
Fields: []*typgen.Field{
133+
{Names: []string{"SomeField5"}, Type: "string", StructTag: `default:"some-text"`},
134+
{Names: []string{"SomeField6"}, Type: "int", StructTag: `default:"9876"`},
135+
},
136+
},
137+
},
138+
},
111139
}
112140

113141
require.NoError(t, a.Process(c, directives))
@@ -119,11 +147,15 @@ func TestCfgAnnotation_Annotate_GenerateDotEnvAndUsageDoc(t *testing.T) {
119147
require.Equal(t, `some-template`, string(b))
120148

121149
b, _ = ioutil.ReadFile(a.GenDotEnv)
122-
require.Equal(t, "SS_SOMEFIELD1=some-text\nSS_SOMEFIELD2=9876\n", string(b))
150+
require.Equal(t, `SOMEFIELD3=some-text
151+
SOMEFIELD4=9876
152+
SOMEFIELD5=some-text
153+
SOMEFIELD6=9876
154+
SS_SOMEFIELD1=some-text
155+
SS_SOMEFIELD2=9876
156+
`, string(b))
123157
require.Equal(t, "some-text", os.Getenv("SS_SOMEFIELD1"))
124158
require.Equal(t, "9876", os.Getenv("SS_SOMEFIELD2"))
125-
126-
require.Equal(t, "> Generate @envconfig to folder/some-target\n> go build -o /bin/goimports golang.org/x/tools/cmd/goimports\n> New keys added in '.env33': SS_SOMEFIELD1 SS_SOMEFIELD2\n> Generate 'some-usage.md'\n", out.String())
127159
}
128160

129161
func TestCfgAnnotation_Annotate_Predefined(t *testing.T) {

0 commit comments

Comments
 (0)