-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// https://github.com/go-kratos/kratos/pull/2811 | ||
|
||
package libcodec | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/go-kratos/kratos/v2/encoding" | ||
) | ||
|
||
func init() { //nolint: gochecknoinits // required by the encoding package | ||
encoding.RegisterCodec(tomlCodec{}) | ||
} | ||
|
||
// tomlCodec is a Codec implementation with toml. | ||
type tomlCodec struct{} | ||
|
||
func (c tomlCodec) Marshal(v interface{}) ([]byte, error) { | ||
buf := &bytes.Buffer{} | ||
encoder := toml.NewEncoder(buf) | ||
|
||
if err := encoder.Encode(v); err != nil { | ||
return nil, err | ||
} | ||
|
||
data := buf.Bytes() | ||
return data, nil | ||
} | ||
|
||
func (c tomlCodec) Unmarshal(data []byte, v interface{}) error { | ||
return toml.Unmarshal(data, v) | ||
} | ||
|
||
func (c tomlCodec) Name() string { | ||
return string(TOML) | ||
} |
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,95 @@ | ||
// https://github.com/go-kratos/kratos/pull/2811 | ||
|
||
package libcodec_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tuihub/librarian/internal/lib/libcodec" | ||
) | ||
|
||
func TestCodec_Unmarshal(t *testing.T) { | ||
type User struct { | ||
Name string `toml:"name"` | ||
Email string `toml:"email"` | ||
} | ||
tests := []struct { | ||
data string | ||
value interface{} | ||
}{ | ||
{ | ||
data: "v = \"John Doe\"", | ||
value: map[string]interface{}{"v": "John Doe"}, | ||
}, | ||
{ | ||
data: "v = 100", | ||
value: map[string]interface{}{"v": 100}, | ||
}, | ||
{ | ||
data: "v = 100_100", | ||
value: map[string]interface{}{"v": 100100}, | ||
}, | ||
{ | ||
data: "v = 1.1", | ||
value: map[string]interface{}{"v": 1.1}, | ||
}, | ||
{ | ||
data: "v = true", | ||
value: map[string]interface{}{"v": true}, | ||
}, | ||
{ | ||
data: "v = [\"apple\", \"banana\", \"cherry\"]", | ||
value: map[string]interface{}{"": []string{"apple", "banana", "cherry"}}, | ||
}, | ||
{ | ||
data: "v = 1.618e+01", | ||
value: map[string]interface{}{"v": 16.18}, | ||
}, | ||
{ | ||
data: "v = 0xDEADBEEF", | ||
value: map[string]interface{}{"v": 3735928559}, | ||
}, | ||
{ | ||
data: "v = 0b1101_0101", | ||
value: map[string]interface{}{"v": 213}, | ||
}, | ||
{ | ||
data: "v = 0o755", | ||
value: map[string]interface{}{"v": 493}, | ||
}, | ||
{ | ||
data: "v = 2022-04-16T12:13:14Z", | ||
value: map[string]interface{}{"v": time.Date(2022, time.April, 16, 12, 13, 14, 0, time.UTC)}, | ||
}, | ||
{ | ||
data: "v = { name = \"John\", email = \"[email protected]\"}", | ||
value: map[string]interface{}{"v": User{ | ||
Name: "John", | ||
Email: "[email protected]", | ||
}}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
v := reflect.ValueOf(tt.value).Type() | ||
value := reflect.New(v) | ||
err := libcodec.Unmarshal(libcodec.TOML, []byte(tt.data), value.Interface()) | ||
if err != nil { | ||
t.Fatalf("(codec{}).Unmarshal should not return err: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func TestCodec_Marshal(t *testing.T) { | ||
value := map[string]string{"v": "hi"} | ||
got, err := libcodec.Marshal(libcodec.TOML, value) | ||
if err != nil { | ||
t.Fatalf("should not return err") | ||
} | ||
// t.Logf("got: %v", string(got)) | ||
if string(got) != "v = \"hi\"\n" { | ||
t.Fatalf("want v = \"hi\", return %s", string(got)) | ||
} | ||
} |