Skip to content

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

Open
@SharkFourSix

Description

@SharkFourSix

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureCategorizes as related to a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions