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

Better strategy to parse metadata #92

Closed
cuducos opened this issue Aug 5, 2021 · 3 comments · Fixed by #93
Closed

Better strategy to parse metadata #92

cuducos opened this issue Aug 5, 2021 · 3 comments · Fixed by #93

Comments

@cuducos
Copy link
Contributor

cuducos commented Aug 5, 2021

Is your feature request related to a problem? Please describe.

We have this TODO:

// FIXME: This only works because we currently only have one option (theme),

Describe the solution you'd like

And I think that using *string instead of string would help us differentiate something not set in the header from things set to an empty string.

I added new, better examples below, so collapsed this block ; )

package main

import (
	"encoding/json"
	"fmt"
)

type Meta struct {
	Theme *string `json:"theme"`
}

var meta Meta

func printTheme(t Meta) {
	if meta.Theme == nil {
		fmt.Println("Theme not set")
	} else {
		fmt.Printf("Theme is %s", *meta.Theme)
	}
}

func main() {
	json.Unmarshal([]byte("{}"), &meta)
	printTheme(meta)

	json.Unmarshal([]byte(`{"theme": "dark"}`), &meta)
	printTheme(meta)
}

This outputs:

Theme not set
Theme is dark
@cuducos
Copy link
Contributor Author

cuducos commented Aug 5, 2021

Maybe a better use case example:

package main

import (
	"encoding/json"
	"fmt"
)

type Meta struct {
	Theme *string `json:"theme"`
}

var meta1 Meta
var meta2 Meta
var meta3 Meta
var meta4 Meta

func printTheme(m *Meta, j string) {
	json.Unmarshal([]byte(j), m)

	if m.Theme == nil {
		fmt.Printf("Theme not set for %s\n", j)
	} else {
		fmt.Printf("Theme is %s for %s\n", *m.Theme, j)
	}
}

func main() {
	printTheme(&meta1, "{}")
	printTheme(&meta2, `{"theme": "dark"}`)
	printTheme(&meta3, `{"theme": ""}`)
	printTheme(&meta4, `{"notATheme": "dark"}`)
}

Output:

Theme not set for {}
Theme is dark for {"theme": "dark"}
Theme is  for {"theme": ""}
Theme not set for {"notATheme": "dark"}

@maaslalani
Copy link
Owner

This is a great idea!

@maaslalani
Copy link
Owner

maaslalani commented Aug 5, 2021

@cuducos do you want me to implement this or would you like to while adding the author/date metadata. Looks like you have a good handle on this though!

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 a pull request may close this issue.

2 participants