Skip to content

Add Marshal and Unmarshal - #441

Open
unknwon wants to merge 2 commits into
mainfrom
marshal-unmarshal
Open

unknwon wants to merge 2 commits into
mainfrom
marshal-unmarshal

Conversation

@unknwon

@unknwon unknwon commented Nov 15, 2026

Copy link
Copy Markdown
Member

Describe the pull request

Adds encoding/json-style functions for the common case:

data, err := ini.Marshal(&cfg)
err = ini.Unmarshal(data, &cfg)
  • Unmarshal(data, v, opts...) is StrictMapTo(v, data, opts...). It's strict because, like json.Unmarshal, it should return an error for a value that doesn't fit its field.
  • Marshal(v, opts...) calls ReflectFrom on Empty(opts...) and returns what WriteTo writes.

Both take LoadOptions, so NameMapping, and StructTag once #439 merges, apply in both directions. Marshal uses the default write formatting. Callers who need Indent, LineBreak and so on can call ReflectFrom and Write themselves, which the doc comment says. Accepting both option types would have needed a third option type or a split signature, which didn't seem worth it.

Link to the issue: closes #337

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code.

AI usage

Written by Amp (an AI coding agent) at the maintainer's request. Validation: go test ./... and go vet. ExampleMarshal and ExampleUnmarshal show the output and the strict error, and TestMarshalUnmarshal checks that options apply both ways and that a non-pointer is an error.

Unmarshal maps INI data to a struct as StrictMapTo does, and Marshal
reflects a struct into an empty file and writes it, so INI works like
encoding/json for the common case (#337).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Marshal and unmarshal functions

1 participant