Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
// ini, delim and comment struct tags adjust each field. A type that
// implements [Marshaler] or [Unmarshaler] formats or parses its own values.
//
// [Marshal] and [Unmarshal] convert between a struct and INI text in one
// call, as encoding/json does.
//
// # Writing
//
// [File.Write] and [File.SaveTo] write a File back with its comments and in
Expand Down
2 changes: 2 additions & 0 deletions docs/migrate-v1-to-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ The package variables that changed every file's output are now options of `File.

`WriteTo` stays, so `*File` is still an `io.WriterTo`.

New: `Marshal(&v)` and `Unmarshal(data, &v)` convert between a struct and INI text in one call, as `encoding/json` does.

### Sections and keys

| v1 | v2 |
Expand Down
33 changes: 33 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,39 @@ func ExampleSection_MapTo() {
// 9090 30s
}

func ExampleMarshal() {
var cfg Config
cfg.AppName = "demo"
cfg.Server.Port = 8080
cfg.Server.Hosts = []string{"a.example.com", "b.example.com"}

data, err := ini.Marshal(&cfg)
if err != nil {
panic(err)
}
fmt.Print(string(data))
// Output:
// app_name = demo
//
// [server]
// ; Port to listen on
// Port = 8080
// Hosts = a.example.com,b.example.com
}

// Unmarshal returns an error for a value that doesn't fit its field.
func ExampleUnmarshal() {
var cfg Config
err := ini.Unmarshal([]byte("app_name = demo\n[server]\nPort = 8080\nTimeout = 1m\n"), &cfg)
fmt.Println(cfg.AppName, cfg.Server.Port, cfg.Server.Timeout, err)

err = ini.Unmarshal([]byte("[server]\nPort = eighty\n"), &cfg)
fmt.Println(err)
// Output:
// demo 8080 1m0s <nil>
// map to field "server": set field "Port": strconv.ParseInt: parsing "eighty": invalid syntax
}

func ExampleFile_Write() {
f := ini.Empty()
f.Section("").Key("name").SetValue("demo")
Expand Down
22 changes: 22 additions & 0 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,28 @@ func StrictMapTo(v, source any, opts ...LoadOption) error {
return cfg.StrictMapTo(v)
}

// Unmarshal parses the INI data and maps it to v, a pointer to a struct, as
// StrictMapTo does. Like json.Unmarshal, it returns an error for a value
// that doesn't fit its field.
func Unmarshal(data []byte, v any, opts ...LoadOption) error {
return StrictMapTo(v, data, opts...)
}

// Marshal returns the INI encoding of v, a pointer to a struct, as
// File.ReflectFrom fills an empty file loaded with opts and File.WriteTo
// writes it. To set the formatting, call ReflectFrom and Write yourself.
func Marshal(v any, opts ...LoadOption) ([]byte, error) {
f := Empty(opts...)
if err := f.ReflectFrom(v); err != nil {
return nil, err
}
buf, err := f.writeToBuffer(nil)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// encodeSlice does the opposite thing as decodeSlice.
func encodeSlice(key *Key, field reflect.Value, delim string, allowShadow bool) error {
if field.Len() == 0 {
Expand Down
22 changes: 22 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1800,3 +1800,25 @@ func (*testUnmarshalOnlyList) UnmarshalINI([]byte) error { panic("called") }
func (testMarshalOnlyStruct) MarshalINI() ([]byte, error) { panic("called") }

func (testMarshalOnlyMap) MarshalINI() ([]byte, error) { panic("called") }

func TestMarshalUnmarshal(t *testing.T) {
type config struct {
AppName string
Server struct{ ListenPort int }
}
var in config
in.AppName = "demo"
in.Server.ListenPort = 8080

// Options reach both directions.
data, err := Marshal(&in, NameMapping(SnakeCase))
require.NoError(t, err)
assert.Equal(t, "APP_NAME = demo\n\n[SERVER]\nLISTEN_PORT = 8080\n", string(data))

var out config
require.NoError(t, Unmarshal(data, &out, NameMapping(SnakeCase)))
assert.Equal(t, in, out)

_, err = Marshal(in)
assert.Error(t, err)
}
Loading