Skip to content

Commit

Permalink
feat: add TOML support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Dec 13, 2023
1 parent 3414829 commit c677567
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/caiyunapp/oap
go 1.20

require (
github.com/pelletier/go-toml/v2 v2.1.1
github.com/philchia/agollo/v4 v4.1.5
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.3.0
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/philchia/agollo/v4 v4.1.5 h1:6FyH9ex5CKPCNyNyuXvOx2yKN5lWRT0sUu2lpSTmleE=
github.com/philchia/agollo/v4 v4.1.5/go.mod h1:SBdQmfqqu/XCWJ1MDzYcCL3X+p3VJ+uQBy0nxxqjexg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 5 additions & 2 deletions oap.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Package oap is a library for loading Apollo configuration into a struct.
// It supports JSON and YAML format. You can also use custom unmarshal for struct type filed.
// It supports JSON/YAML/TOML format.
// You can also use custom unmarshal for struct type filed.
package oap

import (
Expand All @@ -8,6 +9,7 @@ import (
"reflect"
"strings"

"github.com/pelletier/go-toml/v2"
"github.com/philchia/agollo/v4"
"gopkg.in/yaml.v3"
)
Expand All @@ -22,10 +24,11 @@ type UnmarshalFunc func([]byte, interface{}) error
var registryForUnmarshal = map[string]UnmarshalFunc{
"json": json.Unmarshal,
"yaml": yaml.Unmarshal,
"toml": toml.Unmarshal,
}

// You can use custom unmarshal for struct type filed.
// Package oap provides built-in support for JSON&YAML.
// Package oap provides built-in support for JSON/YAML/TOML.
func SetUnmarshalFunc(name string, f UnmarshalFunc) {
registryForUnmarshal[name] = f
}
Expand Down
25 changes: 25 additions & 0 deletions oap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,28 @@ func ExampleDecode() {

fmt.Println(config.Foo)
}

func TestDecodeTOMLStruct(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

type user struct {
Name string `toml:"name"`
Age int `toml:"age"`
}

config := struct {
User user `apollo:"user,toml"`
UserPtr *user `apollo:"user_ptr,toml"`
}{}

client := NewMockClient(ctrl)
client.EXPECT().GetString(gomock.Eq("user")).Return("name = \"Alice\"\nage = 18")
client.EXPECT().GetString(gomock.Eq("user_ptr")).Return("name = \"Alice\"\nage = 18")

err := oap.Decode(&config, client, make(map[string][]agollo.OpOption))
require.NoError(t, err)

assert.Equal(t, user{Name: "Alice", Age: 18}, config.User)
assert.Equal(t, &user{Name: "Alice", Age: 18}, config.UserPtr)
}

0 comments on commit c677567

Please sign in to comment.