-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example on how to use TextUnmarshaler (#867)
Ref #865
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
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
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} | ||
} |
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