Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated GetDescription function and ReadEnv. #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions cleanenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,13 @@ func readEnvVars(cfg interface{}, update bool) error {
}

if rawValue == nil && meta.required && meta.isFieldValueZero() {
err := fmt.Errorf("field %q is required but the value is not provided",
meta.fieldName)
envList := ""
for _, env := range meta.envList {
envList = envList + env + " "
}
envList = strings.Trim(envList, " ")
err := fmt.Errorf("field %q is required but the value is not provided. Envs names: %q",
meta.fieldName, envList)
Comment on lines +316 to +322
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really don't need this part, because the value can come from any source - env, yaml, toml, json, etc. The only thing we know is that the value is empty, and there is a message about it.

return err
}

Expand Down Expand Up @@ -512,8 +517,10 @@ func GetDescription(cfg interface{}, headerText *string) (string, error) {
}
elemDescription += fmt.Sprintf("\n \t%s", m.description)
if m.defValue != nil {
elemDescription += fmt.Sprintf(" (default %q)", *m.defValue)
elemDescription += fmt.Sprintf("\n \tDefault: %q", *m.defValue)
}
elemDescription += fmt.Sprintf("\n \tRequired: %t", m.required)

description += elemDescription
}
}
Expand Down
60 changes: 30 additions & 30 deletions cleanenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,9 @@ func TestGetDescription(t *testing.T) {
cfg: &testSingleEnv{},
header: nil,
want: "Environment variables:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo" +
"\n THREE int\n \tthree",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n THREE int\n \tthree\n \tRequired: false",
wantErr: false,
},

Expand All @@ -704,10 +704,10 @@ func TestGetDescription(t *testing.T) {
cfg: &testSeveralEnv{},
header: nil,
want: "Environment variables:" +
"\n ONE int\n \tone" +
"\n ENO int (alternative to ONE)\n \tone" +
"\n TWO int\n \ttwo" +
"\n OWT int (alternative to TWO)\n \ttwo",
"\n ONE int\n \tone\n \tRequired: false" +
"\n ENO int (alternative to ONE)\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n OWT int (alternative to TWO)\n \ttwo\n \tRequired: false",
wantErr: false,
},

Expand All @@ -716,9 +716,9 @@ func TestGetDescription(t *testing.T) {
cfg: &testDefaultEnv{},
header: nil,
want: "Environment variables:" +
"\n ONE int\n \tone (default \"1\")" +
"\n TWO int\n \ttwo (default \"2\")" +
"\n THREE int\n \tthree (default \"3\")",
"\n ONE int\n \tone\n \tDefault: \"1\"\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tDefault: \"2\"\n \tRequired: false" +
"\n THREE int\n \tthree\n \tDefault: \"3\"\n \tRequired: false",
wantErr: false,
},

Expand All @@ -727,8 +727,8 @@ func TestGetDescription(t *testing.T) {
cfg: &testDeep{},
header: nil,
want: "Environment variables:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false",
wantErr: false,
},

Expand All @@ -745,9 +745,9 @@ func TestGetDescription(t *testing.T) {
cfg: &testSingleEnv{},
header: &header,
want: "test header:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo" +
"\n THREE int\n \tthree",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n THREE int\n \tthree\n \tRequired: false",
wantErr: false,
},

Expand All @@ -764,11 +764,11 @@ func TestGetDescription(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := GetDescription(tt.cfg, tt.header)
if (err != nil) != tt.wantErr {
t.Errorf("wrong error behavior %v, wantErr %v", err, tt.wantErr)
t.Errorf("case %q: wrong error behavior %v, wantErr %v", tt.name, err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("wrong description text %s, want %s", got, tt.want)
t.Errorf("case %q: wrong description text:\n %s\n want:\n %s", tt.name, got, tt.want)
}
})
}
Expand All @@ -794,19 +794,19 @@ func TestFUsage(t *testing.T) {
headerText: nil,
usageTexts: nil,
want: "Environment variables:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo" +
"\n THREE int\n \tthree\n",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n THREE int\n \tthree\n \tRequired: false\n",
},

{
name: "custom header",
headerText: &customHeader,
usageTexts: nil,
want: "test header:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo" +
"\n THREE int\n \tthree\n",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n THREE int\n \tthree\n \tRequired: false\n",
},

{
Expand All @@ -819,9 +819,9 @@ func TestFUsage(t *testing.T) {
},
want: "test1\ntest2\ntest3\n" +
"\nEnvironment variables:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo" +
"\n THREE int\n \tthree\n",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n THREE int\n \tthree\n \tRequired: false\n",
},

{
Expand All @@ -834,9 +834,9 @@ func TestFUsage(t *testing.T) {
},
want: "test1\ntest2\ntest3\n" +
"\ntest header:" +
"\n ONE int\n \tone" +
"\n TWO int\n \ttwo" +
"\n THREE int\n \tthree\n",
"\n ONE int\n \tone\n \tRequired: false" +
"\n TWO int\n \ttwo\n \tRequired: false" +
"\n THREE int\n \tthree\n \tRequired: false\n",
},
}
for _, tt := range tests {
Expand All @@ -856,7 +856,7 @@ func TestFUsage(t *testing.T) {
got := string(gotRaw)

if got != tt.want {
t.Errorf("wrong output %v, want %v", got, tt.want)
t.Errorf("wrong output\n %v, want\n %v", got, tt.want)
}
})
}
Expand Down
7 changes: 0 additions & 7 deletions example/simple_config/config.yml

This file was deleted.

24 changes: 21 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ func ExampleGetDescription() {
//Output: Environment variables:
// ONE int64
// first parameter
// Required: false
// TWO float64
// second parameter
// Required: false
// THREE string
// third parameter
// Required: false
}

// ExampleGetDescription_defaults builds a description text from structure tags with description of default values
Expand All @@ -51,11 +54,17 @@ func ExampleGetDescription_defaults() {
fmt.Println(text)
//Output: Environment variables:
// ONE int64
// first parameter (default "1")
// first parameter
// Default: "1"
// Required: false
// TWO float64
// second parameter (default "2.2")
// second parameter
// Default: "2.2"
// Required: false
// THREE string
// third parameter (default "test")
// third parameter
// Default: "test"
// Required: false
}

// ExampleGetDescription_variableList builds a description text from structure tags with description of alternative variables
Expand All @@ -75,10 +84,13 @@ func ExampleGetDescription_variableList() {
//Output: Environment variables:
// ONE int64
// first found parameter
// Required: false
// TWO int64 (alternative to ONE)
// first found parameter
// Required: false
// THREE int64 (alternative to ONE)
// first found parameter
// Required: false
}

// ExampleGetDescription_customHeaderText builds a description text from structure tags with custom header string
Expand All @@ -102,10 +114,13 @@ func ExampleGetDescription_customHeaderText() {
//Output: Custom header text:
// ONE int64
// first parameter
// Required: false
// TWO float64
// second parameter
// Required: false
// THREE string
// third parameter
// Required: false
}

// ExampleUpdateEnv updates variables in the configuration structure.
Expand Down Expand Up @@ -250,8 +265,11 @@ func ExampleUsage() {
// My sweet variables:
// ONE int64
// first parameter
// Required: false
// TWO float64
// second parameter
// Required: false
// THREE string
// third parameter
// Required: false
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/ilyakaznacheev/cleanenv
module github.com/mashinapetro/cleanenv
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change back to ilyakaznacheev


require (
github.com/BurntSushi/toml v0.3.1
Expand Down