2023-05-06 21:00:42 +00:00
|
|
|
package env
|
2021-05-09 15:51:38 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-06 21:00:42 +00:00
|
|
|
"encoding/json"
|
2021-05-09 15:51:38 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-08-15 15:37:49 +00:00
|
|
|
type subStruct struct {
|
2023-10-07 21:32:15 +00:00
|
|
|
MyParam int `json:"myParam"`
|
2022-08-15 15:37:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 15:51:38 +00:00
|
|
|
type mapEntry struct {
|
2023-10-07 21:32:15 +00:00
|
|
|
MyValue string `json:"myValue"`
|
|
|
|
MyStruct subStruct `json:"myStruct"`
|
2021-05-09 15:51:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
type myDuration time.Duration
|
|
|
|
|
|
|
|
func (d *myDuration) UnmarshalJSON(b []byte) error {
|
|
|
|
var in string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
du, err := time.ParseDuration(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*d = myDuration(du)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
|
|
func (d *myDuration) UnmarshalEnv(_ string, v string) error {
|
|
|
|
return d.UnmarshalJSON([]byte(`"` + v + `"`))
|
2023-05-06 21:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 14:47:10 +00:00
|
|
|
type mySubStruct struct {
|
2023-10-07 21:32:15 +00:00
|
|
|
URL string `json:"url"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2023-06-30 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 15:51:38 +00:00
|
|
|
type testStruct struct {
|
2023-10-07 21:32:15 +00:00
|
|
|
MyString string `json:"myString"`
|
|
|
|
MyInt int `json:"myInt"`
|
|
|
|
MyFloat float64 `json:"myFloat"`
|
|
|
|
MyBool bool `json:"myBool"`
|
|
|
|
MyDuration myDuration `json:"myDuration"`
|
|
|
|
MyMap map[string]*mapEntry `json:"myMap"`
|
|
|
|
MySlice []string `json:"mySlice"`
|
|
|
|
MySliceEmpty []string `json:"mySliceEmpty"`
|
|
|
|
MySliceSubStruct []mySubStruct `json:"mySliceSubStruct"`
|
|
|
|
MySliceSubStructEmpty []mySubStruct `json:"mySliceSubStructEmpty"`
|
2021-05-09 15:51:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
func TestLoad(t *testing.T) {
|
2021-05-09 15:51:38 +00:00
|
|
|
os.Setenv("MYPREFIX_MYSTRING", "testcontent")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSTRING")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYINT", "123")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYINT")
|
|
|
|
|
Add additional Raspberry Pi Camera parameters (#1198)
* rpicamera: add rpiCameraHFlip and rpiCameraVFlip parameters
* rpicamera: add rpiCameraBrightness, rpiCameraContrast,
rpiCameraSaturation, rpiCameraSharpness, rpiCameraExposure,
rpiCameraAWB, rpiCameraDenoise, rpiCameraShutter, rpiCameraMetering,
rpiCameraGain, rpiCameraEV, rpiCameraROI, rpiCameraTuningFile
* support float values in config file
2022-10-24 13:08:30 +00:00
|
|
|
os.Setenv("MYPREFIX_MYFLOAT", "15.2")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYFLOAT")
|
|
|
|
|
2021-05-09 15:51:38 +00:00
|
|
|
os.Setenv("MYPREFIX_MYBOOL", "yes")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYBOOL")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYDURATION", "22s")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYDURATION")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYMAP_MYKEY", "")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYMAP_MYKEY")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYMAP_MYKEY2_MYVALUE", "asd")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYMAP_MYKEY2_MYVALUE")
|
|
|
|
|
2022-08-15 15:37:49 +00:00
|
|
|
os.Setenv("MYPREFIX_MYMAP_MYKEY2_MYSTRUCT_MYPARAM", "456")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYMAP_MYKEY2_MYSTRUCT_MYPARAM")
|
|
|
|
|
2022-12-15 23:50:47 +00:00
|
|
|
os.Setenv("MYPREFIX_MYSLICE", "val1,val2")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICE")
|
|
|
|
|
2022-12-30 17:40:27 +00:00
|
|
|
os.Setenv("MYPREFIX_MYSLICEEMPTY", "")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICEEMPTY")
|
|
|
|
|
2023-06-30 14:47:10 +00:00
|
|
|
os.Setenv("MYPREFIX_MYSLICESUBSTRUCT_0_URL", "url1")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICESUBSTRUCT_0_URL")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYSLICESUBSTRUCT_0_USERNAME", "user1")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICESUBSTRUCT_0_USERNAME")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYSLICESUBSTRUCT_0_PASSWORD", "pass1")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICESUBSTRUCT_0_PASSWORD")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYSLICESUBSTRUCT_1_URL", "url2")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICESUBSTRUCT_1_URL")
|
|
|
|
|
|
|
|
os.Setenv("MYPREFIX_MYSLICESUBSTRUCT_1_PASSWORD", "pass2")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICESUBSTRUCT_1_PASSWORD")
|
|
|
|
|
2023-07-16 22:12:05 +00:00
|
|
|
os.Setenv("MYPREFIX_MYSLICESUBSTRUCTEMPTY", "")
|
|
|
|
defer os.Unsetenv("MYPREFIX_MYSLICESUBSTRUCTEMPTY")
|
|
|
|
|
2021-05-09 15:51:38 +00:00
|
|
|
var s testStruct
|
2023-05-06 21:00:42 +00:00
|
|
|
err := Load("MYPREFIX", &s)
|
2021-05-09 15:51:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "testcontent", s.MyString)
|
|
|
|
require.Equal(t, 123, s.MyInt)
|
Add additional Raspberry Pi Camera parameters (#1198)
* rpicamera: add rpiCameraHFlip and rpiCameraVFlip parameters
* rpicamera: add rpiCameraBrightness, rpiCameraContrast,
rpiCameraSaturation, rpiCameraSharpness, rpiCameraExposure,
rpiCameraAWB, rpiCameraDenoise, rpiCameraShutter, rpiCameraMetering,
rpiCameraGain, rpiCameraEV, rpiCameraROI, rpiCameraTuningFile
* support float values in config file
2022-10-24 13:08:30 +00:00
|
|
|
require.Equal(t, 15.2, s.MyFloat)
|
2021-05-09 15:51:38 +00:00
|
|
|
require.Equal(t, true, s.MyBool)
|
2023-05-06 21:00:42 +00:00
|
|
|
require.Equal(t, 22*myDuration(time.Second), s.MyDuration)
|
2021-05-09 15:51:38 +00:00
|
|
|
|
|
|
|
_, ok := s.MyMap["mykey"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
|
|
|
|
v, ok := s.MyMap["mykey2"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, "asd", v.MyValue)
|
2022-08-15 15:37:49 +00:00
|
|
|
require.Equal(t, 456, v.MyStruct.MyParam)
|
2022-12-15 23:50:47 +00:00
|
|
|
|
|
|
|
require.Equal(t, []string{"val1", "val2"}, s.MySlice)
|
2022-12-30 17:40:27 +00:00
|
|
|
require.Equal(t, []string{}, s.MySliceEmpty)
|
2023-06-30 14:47:10 +00:00
|
|
|
|
|
|
|
require.Equal(t, []mySubStruct{
|
|
|
|
{
|
|
|
|
URL: "url1",
|
|
|
|
Username: "user1",
|
|
|
|
Password: "pass1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "url2",
|
|
|
|
Password: "pass2",
|
|
|
|
},
|
|
|
|
}, s.MySliceSubStruct)
|
2023-07-16 22:12:05 +00:00
|
|
|
|
|
|
|
require.Equal(t, []mySubStruct{}, s.MySliceSubStructEmpty)
|
2021-05-09 15:51:38 +00:00
|
|
|
}
|