Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please introduce MarshalINI() ([]byte, error) and UnmarshalINI([]byte) error interface for custom structs #353

Open
1 task done
SharkFourSix opened this issue Sep 27, 2024 · 0 comments
Labels
feature Categorizes as related to a new feature

Comments

@SharkFourSix
Copy link

Describe the feature

So far nobody (not even the documentation) has shown a way to marshal data types.

Describe the solution you'd like

Introduce marshaller interfaces like so:

type INIUnmarshaller interface {
    UnmarshalINI([]byte) error
}

type INIMarshaller interface {
    MarshalINI([]byte, error)
}

Describe alternatives you've considered

Alternative: Manually load data as string and then convert into custom type

Additional context

Usage:

type Permission int
const (
    None Permission = iota
    Read Permission
    Write
    Edit
    Group
)


type Config struct {
    Foo string
    Permission Permission
}

var permToStringMap = map[string]Permission = {
    "none": None,
    "read": Read,
    "write": Write,
    "edit": Edit,
    "group": Group,
} 

func (c *Permission) UnmarshalINI(p []byte) error {
    p, ok := permToStringMap[string(p)]
    if !ok {
        return fmt.Errorf("invalid permission `%v`", p)
    }
    *c = p
    return nil
}

config.ini

permission = "Group"

Code of Conduct

  • I agree to follow this project's Code of Conduct
@SharkFourSix SharkFourSix added the feature Categorizes as related to a new feature label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Categorizes as related to a new feature
Projects
None yet
Development

No branches or pull requests

1 participant