Skip to content

Commit

Permalink
Add example on how to use TextUnmarshaler (#867)
Browse files Browse the repository at this point in the history
Ref #865
  • Loading branch information
pelletier committed May 16, 2023
1 parent 55ca4e3 commit 8c2c9cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
37 changes: 37 additions & 0 deletions example_text_marshaling_test.go
Original file line number Diff line number Diff line change
@@ -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 change: 0 additions & 1 deletion unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c2c9cc

Please sign in to comment.