@@ -6083,6 +6083,105 @@ func TestParseJsonStringValue(t *testing.T) {
60836083 })
60846084}
60856085
6086+ // issue #5033, string type
6087+ func TestUnmarshalFromEnvString (t * testing.T ) {
6088+ t .Setenv ("STRING_ENV" , "dev" )
6089+
6090+ t .Run ("by value" , func (t * testing.T ) {
6091+ type (
6092+ Env string
6093+ Config struct {
6094+ Env Env `json:",env=STRING_ENV,default=prod"`
6095+ }
6096+ )
6097+
6098+ var c Config
6099+ if assert .NoError (t , UnmarshalJsonMap (map [string ]any {}, & c )) {
6100+ assert .Equal (t , Env ("dev" ), c .Env )
6101+ }
6102+ })
6103+
6104+ t .Run ("by ptr" , func (t * testing.T ) {
6105+ type (
6106+ Env string
6107+ Config struct {
6108+ Env * Env `json:",env=STRING_ENV,default=prod"`
6109+ }
6110+ )
6111+
6112+ var c Config
6113+ if assert .NoError (t , UnmarshalJsonMap (map [string ]any {}, & c )) {
6114+ assert .Equal (t , Env ("dev" ), * c .Env )
6115+ }
6116+ })
6117+ }
6118+
6119+ // issue #5033, bool type
6120+ func TestUnmarshalFromEnvBool (t * testing.T ) {
6121+ t .Setenv ("BOOL_ENV" , "true" )
6122+
6123+ t .Run ("by value" , func (t * testing.T ) {
6124+ type (
6125+ Env bool
6126+ Config struct {
6127+ Env Env `json:",env=BOOL_ENV,default=false"`
6128+ }
6129+ )
6130+
6131+ var c Config
6132+ if assert .NoError (t , UnmarshalJsonMap (map [string ]any {}, & c )) {
6133+ assert .Equal (t , Env (true ), c .Env )
6134+ }
6135+ })
6136+
6137+ t .Run ("by ptr" , func (t * testing.T ) {
6138+ type (
6139+ Env bool
6140+ Config struct {
6141+ Env * Env `json:",env=BOOL_ENV,default=false"`
6142+ }
6143+ )
6144+
6145+ var c Config
6146+ if assert .NoError (t , UnmarshalJsonMap (map [string ]any {}, & c )) {
6147+ assert .Equal (t , Env (true ), * c .Env )
6148+ }
6149+ })
6150+ }
6151+
6152+ // issue #5033, customized int type
6153+ func TestUnmarshalFromEnvInt (t * testing.T ) {
6154+ t .Setenv ("INT_ENV" , "2" )
6155+
6156+ t .Run ("by value" , func (t * testing.T ) {
6157+ type (
6158+ Env int
6159+ Config struct {
6160+ Env Env `json:",env=INT_ENV,default=0"`
6161+ }
6162+ )
6163+
6164+ var c Config
6165+ if assert .NoError (t , UnmarshalJsonMap (map [string ]any {}, & c )) {
6166+ assert .Equal (t , Env (2 ), c .Env )
6167+ }
6168+ })
6169+
6170+ t .Run ("by ptr" , func (t * testing.T ) {
6171+ type (
6172+ Env int
6173+ Config struct {
6174+ Env * Env `json:",env=INT_ENV,default=0"`
6175+ }
6176+ )
6177+
6178+ var c Config
6179+ if assert .NoError (t , UnmarshalJsonMap (map [string ]any {}, & c )) {
6180+ assert .Equal (t , Env (2 ), * c .Env )
6181+ }
6182+ })
6183+ }
6184+
60866185func BenchmarkDefaultValue (b * testing.B ) {
60876186 for i := 0 ; i < b .N ; i ++ {
60886187 var a struct {
0 commit comments