Skip to content

add eskip json unmarshalling #471

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

aryszka
Copy link
Contributor

@aryszka aryszka commented Oct 23, 2017

Fixes #879

@grassator
Copy link
Contributor

Looks good (for my limited understanding of Go). As for the tests, it might not be very idiomatic, but it might be nice to have some kind generative tests, that ensure:

1. json -> internal -> json === json
2. internal -> json -> internal === internal

@aryszka
Copy link
Contributor Author

aryszka commented Oct 23, 2017

ok, thanks, will add such tests

@grassator
Copy link
Contributor

@aryszka is this still something we want to have, if so I can pick it up probably, otherwise lets close the PR.

@aryszka
Copy link
Contributor Author

aryszka commented Dec 11, 2018

@grassator yes, we need this 😄

@grassator
Copy link
Contributor

@aryszka I'm picking this up then.

@grassator grassator force-pushed the feature/eskip-from-json branch from 6d66bca to 01d8b85 Compare December 11, 2018 16:41
@grassator
Copy link
Contributor

@aryszka I've rebased on top of master and had to make a few changes on the go. Most of them are still keeping API compatible, the only really breaking change if someone was creating WatchClient on their own.

eskip/json.go Outdated
func (p *Predicate) MarshalJSON() ([]byte, error) {
return marshalNameArgs(p.Name, p.Args)
}

// UnmarshalJSON returns the JSON representation of a predicate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that you don’t do it the other way around?
Marshal and unmarshal have very similar doc strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the doc comment should rather say something like this:

"UnmarshalJSON parses a predicate object from its JSON representation."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to your version

eskip/json.go Outdated
}{
var buf bytes.Buffer
e := json.NewEncoder(&buf)
e.SetEscapeHTML(false) // why not working :(
Copy link
Contributor Author

@aryszka aryszka Dec 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was your comment :) I will check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this took a bit to figure out. The problem is that the setting only has effect if you are using an encoder directly. If you use json.Marshal there is no way to unset it.
https://golang.org/src/encoding/json/encode.go?s=6521:6546#L160

return WatchFile(fileName, eskip.ParseBytes)
}

func WatchFile(fileName string, parseFunc eskip.ParseFunc) *WatchClient {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name of this function should reflect what's the difference from the simple Watch(). My candidate is: WatchParser()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it WatchWithParser

@@ -334,6 +336,11 @@ func Parse(code string) ([]*Route, error) {
return routeDefinitions, nil
}

// Parses a route expression or a routing document to a set of route definitions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According the https://golang.org/doc/effective_go.html#commentary comments should start with function name:

Suggested change
// Parses a route expression or a routing document to a set of route definitions.
// ParseBytes parses a route expression or a routing document to a set of route definitions.

It some languages, to generate docs, function name is ignored, that's why doc comments starts with function name.

@marcinzaremba
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement wip work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable loading JSON
5 participants