Add example on how to use TextUnmarshaler (#867)

Ref #865
This commit is contained in:
Thomas Pelletier
2023-05-16 08:17:21 -04:00
committed by GitHub
parent 55ca4e35e4
commit 8c2c9cc986
2 changed files with 37 additions and 1 deletions
+37
View File
@@ -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}
}
-1
View File
@@ -90,7 +90,6 @@ func ExampleUnmarshal() {
fmt.Println("version:", cfg.Version) fmt.Println("version:", cfg.Version)
fmt.Println("name:", cfg.Name) fmt.Println("name:", cfg.Name)
fmt.Println("tags:", cfg.Tags) fmt.Println("tags:", cfg.Tags)
// Output: // Output:
// version: 2 // version: 2
// name: go-toml // name: go-toml