diff --git a/example_text_marshaling_test.go b/example_text_marshaling_test.go new file mode 100644 index 00000000..4d4f3cc3 --- /dev/null +++ b/example_text_marshaling_test.go @@ -0,0 +1,37 @@ +package toml_test + +import ( + "fmt" + "log" + "strconv" + + "github.com/pelletier/go-toml/v2" +) + +type customInt int + +func (i *customInt) UnmarshalText(b []byte) error { + x, err := strconv.ParseInt(string(b), 10, 32) + if err != nil { + return err + } + *i = customInt(x * 100) + return nil +} + +type doc struct { + Value customInt +} + +func ExampleUnmarshal_textUnmarshal() { + var x doc + + data := []byte(`value = "42"`) + err := toml.Unmarshal(data, &x) + if err != nil { + log.Fatal(err) + } + fmt.Println(x) + // Output: + // {4200} +} diff --git a/unmarshaler_test.go b/unmarshaler_test.go index 1cc17d0e..8e6cbd52 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -90,7 +90,6 @@ func ExampleUnmarshal() { fmt.Println("version:", cfg.Version) fmt.Println("name:", cfg.Name) fmt.Println("tags:", cfg.Tags) - // Output: // version: 2 // name: go-toml