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

Cannot convert an object #141

Closed
n10v opened this issue Mar 20, 2017 · 1 comment
Closed

Cannot convert an object #141

n10v opened this issue Mar 20, 2017 · 1 comment

Comments

@n10v
Copy link
Contributor

n10v commented Mar 20, 2017

I make TOML support for nicksnyder/goi18n library, but become an issue when trying to marshal some objects: cannot convert type *translation.template to TomlTree. *translation.template have String method though, so we can use it for marshaling.

Code:

m, ok := v.(map[string]interface{})
if !ok {
    return nil, fmt.Errorf("invalid format for marshaling to TOML")
}
tree, err := toml.TreeFromMap(m)
if err != nil {
    return nil, err
}
s, err := tree.ToTomlString()
return []byte(s), err

Example of m:

map[person_unread_email_count:map[one:{{.Person}} has {{.Count}} unread email. other:{{.Person}} 
has{{.Count}} unread emails.] person_unread_email_count_timeframe:map[other:{{.Person}} has 
{{.Count}} unread email in the past {{.Timeframe}}.] program_greeting:map[other:Hello world] 
your_unread_email_count:map[one:You have {{.Count}} unread email. other:You have {{.Count}} 
unread emails.] d_days:map[other:this should get overwritten] my_height_in_meters:map[one:I am 
{{.Count}} meter tall. other:I am {{.Count}} meters tall.] person_greeting:map[other:Hello 
{{.Person}}]]

More information you can find here.

@n10v
Copy link
Contributor Author

n10v commented Mar 20, 2017

Sometimes I become failed to marshal en-us strings to toml: map key needs to be a string, not language.Plural, but language.Plural is just a string:

type Plural string

@n10v n10v mentioned this issue Mar 20, 2017
pelletier pushed a commit that referenced this issue Mar 21, 2017
* Use String() of key if it exists during TreeFromMap
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

No branches or pull requests

1 participant