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

Custom Decoder would be nice!! #39

Open
paracha3 opened this issue Feb 25, 2020 · 5 comments
Open

Custom Decoder would be nice!! #39

paracha3 opened this issue Feb 25, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers proposal
Milestone

Comments

@paracha3
Copy link

No description provided.

@ilyakaznacheev ilyakaznacheev added enhancement New feature or request good first issue Good for newcomers labels Mar 30, 2020
@ilyakaznacheev
Copy link
Owner

Hi @paracha3. Could you please give more context about how you want to use such a feature?

@vlaabra
Copy link

vlaabra commented May 11, 2020

type Config struct {
    Status enum.Status `yaml:"status" decoder:"Dec"`
}

func(*Config) Dec() {}

🤔

@ilyakaznacheev
Copy link
Owner

@vlaabra I need to think about it. Probably some decoder like json.Unmarshaler would be the better approach, but I still don't know about the usage.

Are there any examples of popular decoders, which could be used like this?

@vlaabra
Copy link

vlaabra commented May 12, 2020

import "encoding/hex"
import "go.mongodb.org/mongo-driver/bson/primitive"

type Config struct {
    Decimal primitive.Decimal128 `yaml:"decimal" decoder:"StringToDecimal"` // 12345
    Hex     []byte               `yaml:"hex" decoder:"StringToHex"`         // 1234FF
}

func (c *Config) StringToDecimal(s interface{}) (err error) {
	c.Decimal, err = primitive.ParseDecimal128(fmt.Sprint(s))
	return
}

func (c *Config) StringToHex(s interface{}) (err error) {
	c.Hex, err = hex.DecodeString(fmt.Sprint(s))
	return
}

i need`t, just suggestion

unlike yaml, json calls UnmarshalText and UnmarshalJSON methods if they implementing its interface

@ilyakaznacheev ilyakaznacheev added this to the v1.3.0 milestone May 19, 2020
@ilyakaznacheev
Copy link
Owner

Okay, looks good, we'll add this

@ilyakaznacheev ilyakaznacheev self-assigned this Jul 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers proposal
Projects
None yet
Development

No branches or pull requests

3 participants