-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom unmarshaling of strings (#29)
* Add support for custom unmarshaling of strings Implemented an interface for custom unmarshaling of strings which allows users to define their own custom type unmarshaling methods. Updated fig_test.go and fig.go to reflect these changes. This update provides a flexible way for users to handle configs with custom types. * Add "listener_type" to server configuration Added "listener_type" field to the server configuration in JSON, YAML, and TOML files. The new field helps initialising the ListenerType field, which is now outside the Server struct in the fig test go file, with the "tcp" value. * Refactor listener placement in fig test The "listener_type" field has moved out of the Server struct and is now directly under the server configuration in the fig test go file. This change simplifies the initialization of the ListenerType field with the "tcp" value in JSON, YAML, and TOML configuration files. * Update setDefaultValue function in fig.go The function setDefaultValue in fig.go has been modified to call setValue unless the value satisfies the StringUnmarshaler interface. * Add custom configuration and test files A new custom configuration file, config.yaml, and a corresponding test file, custom_test.go, have been created. This is to serve as example for the custom UnmarshalString interface * Add ListenerType in Server struct in fig_test.go This is to make sure that `Test_fig_Load_UseStrict` won't fail due to the previously changed server.* files in `testdata/` * Moved string unmarshaling from setDefaultValue to setValue This is so that setEnv can benefit from that functionality as well * Add validity check and error wrapping in setValue function Added a check to ensure the reflect.Value is valid before attempting unmarshal. Also, wrapped error message for failed unmarshalling for clearer debugging. These changes will enhance error handling and debugging. * Remove 'listener_type' attribute from server configuration files again, following kkyr's suggestion * Repositioned UnmarshalString function and updated error logging in fig_test.go The UnmarshalString function for the ListenerType has been moved higher up in the fig_test.go code. Updates to error logging formats have also been made for better readability, while unnecessary attributes in the server configuration have been removed. * Update UnmarshalString function and enhance error logging in fig.go The UnmarshalString function in fig.go was repositioned for efficiency. An error message for unexpected issues during string unmarshalling was also added.
- Loading branch information
Showing
5 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
app: | ||
environment: dev | ||
|
||
server: | ||
port: 443 | ||
read_timeout: 1m | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package custom | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/kkyr/fig" | ||
) | ||
|
||
type ListenerType uint | ||
|
||
const ( | ||
ListenerUnix ListenerType = iota | ||
ListenerTCP | ||
ListenerTLS | ||
) | ||
|
||
type Config struct { | ||
App struct { | ||
Environment string `fig:"environment" validate:"required"` | ||
} `fig:"app"` | ||
Server struct { | ||
Host string `fig:"host" default:"0.0.0.0"` | ||
Port int `fig:"port" default:"80"` | ||
Listener ListenerType `fig:"listener_type" default:"tcp"` | ||
} `fig:"server"` | ||
} | ||
|
||
func ExampleLoad() { | ||
var cfg Config | ||
err := fig.Load(&cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(cfg.App.Environment) | ||
fmt.Println(cfg.Server.Host) | ||
fmt.Println(cfg.Server.Port) | ||
fmt.Println(cfg.Server.Listener) | ||
|
||
// Output: | ||
// dev | ||
// 0.0.0.0 | ||
// 443 | ||
// tcp | ||
} | ||
|
||
func (l *ListenerType) UnmarshalString(v string) error { | ||
switch strings.ToLower(v) { | ||
case "unix": | ||
*l = ListenerUnix | ||
case "tcp": | ||
*l = ListenerTCP | ||
case "tls": | ||
*l = ListenerTLS | ||
default: | ||
return fmt.Errorf("unknown listener type: %s", v) | ||
} | ||
return nil | ||
} | ||
|
||
func (l ListenerType) String() string { | ||
switch l { | ||
case ListenerUnix: | ||
return "unix" | ||
case ListenerTCP: | ||
return "tcp" | ||
case ListenerTLS: | ||
return "tls" | ||
default: | ||
return "unknown" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
host = "0.0.0.0" | ||
|
||
[logger] | ||
log_level = "debug" | ||
log_level = "debug" |