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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/eskip/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
prettyFlag = "pretty"
indentStrFlag = "indent"
jsonFlag = "json"
readJSONFlag = "read-json"

defaultEtcdUrls = "http://127.0.0.1:2379,http://127.0.0.1:4001"
defaultEtcdPrefix = "/skipper"
Expand Down Expand Up @@ -76,7 +77,8 @@ var (
appendFileArg string
pretty bool
indentStr string
printJson bool
printJSON bool
readJSON bool
)

var (
Expand Down Expand Up @@ -111,7 +113,8 @@ func initFlags() {

flags.BoolVar(&pretty, prettyFlag, false, prettyUsage)
flags.StringVar(&indentStr, indentStrFlag, " ", indentStrUsage)
flags.BoolVar(&printJson, jsonFlag, false, jsonUsage)
flags.BoolVar(&printJSON, jsonFlag, false, jsonUsage)
flags.BoolVar(&readJSON, readJSONFlag, false, readJsonUsage)
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions cmd/eskip/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
prettyUsage = "prints routes in a more readable format"
indentStrUsage = "indent string used in pretty printing. Must match regexp \\s"
jsonUsage = "prints routes as JSON"
readJsonUsage = "reads routes as JSON"

// command line help (1):
help1 = `Usage: eskip <command> [media flags] [--] [file]
Expand Down
16 changes: 1 addition & 15 deletions cmd/eskip/load.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright 2015 Zalando SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand Down Expand Up @@ -113,7 +99,7 @@ func printCmd(a cmdArgs) error {
return err
}

if printJson {
if printJSON {
e := json.NewEncoder(stdout)
e.SetEscapeHTML(false)
if err := e.Encode(lr.routes); err != nil {
Expand Down
25 changes: 22 additions & 3 deletions cmd/eskip/readclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func createReadClient(m *medium) (readClient, error) {
return &stdinReader{reader: os.Stdin}, nil

case file:
return eskipfile.Open(m.path)
if readJSON {
return eskipfile.LoadFile(m.path, eskip.ParseJSON)
}

return eskipfile.LoadFile(m.path, eskip.ParseBytes)

case inline:
return &inlineReader{routes: m.eskip}, nil
Expand Down Expand Up @@ -85,7 +89,12 @@ func (r *stdinReader) LoadAndParseAll() ([]*eskip.RouteInfo, error) {
return nil, err
}

routes, err := eskip.Parse(string(doc))
var routes []*eskip.Route
if readJSON {
routes, err = eskip.ParseJSON(doc)
} else {
routes, err = eskip.ParseBytes(doc)
}

if err != nil {
return nil, err
Expand All @@ -95,10 +104,20 @@ func (r *stdinReader) LoadAndParseAll() ([]*eskip.RouteInfo, error) {
}

func (r *inlineReader) LoadAndParseAll() ([]*eskip.RouteInfo, error) {
routes, err := eskip.Parse(r.routes)
var (
routes []*eskip.Route
err error
)
if readJSON {
routes, err = eskip.ParseJSON([]byte(r.routes))
} else {
routes, err = eskip.Parse(r.routes)
}

if err != nil {
return nil, err
}

return routesToRouteInfos(routes), nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/skipper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const (
innkeeperPreRouteFiltersUsage = "filters to be prepended to each route loaded from Innkeeper"
innkeeperPostRouteFiltersUsage = "filters to be appended to each route loaded from Innkeeper"
routesFileUsage = "file containing route definitions"
routesFileJSONUsage = "file containing static route definitions in JSON format. If routes-file is also set, routes-file-json is ignored"
inlineRoutesUsage = "inline routes in eskip format"
sourcePollTimeoutUsage = "polling timeout of the routing data sources, in milliseconds"
waitFirstRouteLoadUsage = "prevent starting the listener before the first batch of routes were loaded"
Expand Down Expand Up @@ -263,6 +264,7 @@ var (
innkeeperPreRouteFilters string
innkeeperPostRouteFilters string
routesFile string
routesFileJSON string
inlineRoutes string
sourcePollTimeout int64
waitFirstRouteLoad bool
Expand Down Expand Up @@ -394,6 +396,7 @@ func init() {
flag.StringVar(&innkeeperPreRouteFilters, "innkeeper-pre-route-filters", "", innkeeperPreRouteFiltersUsage)
flag.StringVar(&innkeeperPostRouteFilters, "innkeeper-post-route-filters", "", innkeeperPostRouteFiltersUsage)
flag.StringVar(&routesFile, "routes-file", "", routesFileUsage)
flag.StringVar(&routesFileJSON, "routes-file-json", "", routesFileJSONUsage)
flag.StringVar(&inlineRoutes, "inline-routes", "", inlineRoutesUsage)
flag.Int64Var(&sourcePollTimeout, "source-poll-timeout", defaultSourcePollTimeout, sourcePollTimeoutUsage)
flag.BoolVar(&waitFirstRouteLoad, "wait-first-route-load", false, waitFirstRouteLoadUsage)
Expand Down Expand Up @@ -599,6 +602,7 @@ func main() {
InnkeeperPreRouteFilters: innkeeperPreRouteFilters,
InnkeeperPostRouteFilters: innkeeperPostRouteFilters,
WatchRoutesFile: routesFile,
WatchRoutesFileJSON: routesFileJSON,
InlineRoutes: inlineRoutes,
SourcePollTimeout: time.Duration(sourcePollTimeout) * time.Millisecond,
WaitFirstRouteLoad: waitFirstRouteLoad,
Expand Down
7 changes: 7 additions & 0 deletions eskip/eskip.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ type RouteInfo struct {
ParseError error
}

type ParseFunc func(data []byte) ([]*Route, error)

func (t BackendType) String() string {
switch t {
case NetworkBackend:
Expand Down Expand Up @@ -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.

func ParseBytes(data []byte) ([]*Route, error) {
return Parse(string(data))
}

func partialParse(f string, partialToRoute func(string) string) (*parsedRoute, error) {
rs, err := parse(partialToRoute(f))
if err != nil {
Expand Down
Loading