Skip to content

Commit

Permalink
Add getIntParam, NewEntry, NewEntrySchema
Browse files Browse the repository at this point in the history
Signed-off-by: Enis Inan <[email protected]>
  • Loading branch information
ekinanp committed Mar 6, 2020
1 parent 9743abf commit 6ba54ba
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 23 deletions.
8 changes: 8 additions & 0 deletions api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ func invalidBoolParam(name, value string) *errorResponse {
)}
}

func invalidIntParam(name, value string) *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.InvalidInt,
fmt.Sprintf("Invalid int value '%v' given for %v parameter", value, name),
apitypes.ErrorFields{"value": value},
)}
}

func invalidPathsResponse() *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.InvalidPaths,
Expand Down
33 changes: 13 additions & 20 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,6 @@ import (
"github.com/puppetlabs/wash/plugin"
)

func toAPIEntry(e plugin.Entry) apitypes.Entry {
return apitypes.Entry{
TypeID: plugin.TypeID(e),
Name: plugin.Name(e),
CName: plugin.CName(e),
Actions: plugin.SupportedActionsOf(e),
Attributes: plugin.Attributes(e),
Metadata: plugin.PartialMetadata(e),
}
}

func toAPIEntrySchema(s *plugin.EntrySchema) *apitypes.EntrySchema {
if s == nil {
return nil
}
return &apitypes.EntrySchema{
EntrySchema: (*s),
}
}

func findEntry(ctx context.Context, root plugin.Entry, segments []string) (plugin.Entry, *errorResponse) {
path := strings.Join(segments, "/")
curEntry, err := plugin.FindEntry(ctx, root, segments)
Expand Down Expand Up @@ -152,3 +132,16 @@ func getBoolParam(u *url.URL, key string) (bool, *errorResponse) {
}
return false, nil
}

// return is (n, found, err)
func getIntParam(u *url.URL, key string) (int, bool, *errorResponse) {
val := u.Query().Get(key)
if val != "" {
n, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return 0, false, invalidIntParam(key, val)
}
return int(n), true, nil
}
return 0, false, nil
}
38 changes: 38 additions & 0 deletions api/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,44 @@ func (suite *HelpersTestSuite) TestGetBoolParam() {
suite.Error(err)
}

func (suite *HelpersTestSuite) TestGetIntParam() {
var u url.URL
// tc => testCase
type tc struct {
query string
expectedVal int
expectedFound bool
expectedErrRegex string
}
tcs := []tc{
tc{
query: "",
expectedFound: false,
},
tc{
query: "param=10",
expectedVal: 10,
expectedFound: true,
},
tc{
query: "param=foo",
expectedErrRegex: ".*int.*",
},
}
for _, tc := range tcs {
u.RawQuery = tc.query
val, found, err := getIntParam(&u, "param")
if tc.expectedErrRegex != "" {
suite.Regexp(tc.expectedErrRegex, err)
} else {
suite.Equal(tc.expectedFound, found)
if found {
suite.Equal(tc.expectedVal, val)
}
}
}
}

func TestHelpers(t *testing.T) {
suite.Run(t, new(HelpersTestSuite))
}
4 changes: 3 additions & 1 deletion api/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"net/http"

apitypes "github.com/puppetlabs/wash/api/types"
)

// swagger:route GET /fs/info info entryInfo
Expand All @@ -30,7 +32,7 @@ var infoHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *erro

jsonEncoder := json.NewEncoder(w)
// TODO: Include the entry's full metadata?
apiEntry := toAPIEntry(entry)
apiEntry := apitypes.NewEntry(entry)
apiEntry.Path = path
if err := jsonEncoder.Encode(&apiEntry); err != nil {
return unknownErrorResponse(fmt.Errorf("Could not marshal %v: %v", path, err))
Expand Down
2 changes: 1 addition & 1 deletion api/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var listHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *erro

result := make([]apitypes.Entry, 0, entries.Len())
entries.Range(func(_ string, entry plugin.Entry) bool {
apiEntry := toAPIEntry(entry)
apiEntry := apitypes.NewEntry(entry)
apiEntry.Path = path + "/" + apiEntry.CName
result = append(result, apiEntry)
return true
Expand Down
2 changes: 1 addition & 1 deletion api/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var schemaHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *er
if err != nil {
return unknownErrorResponse(err)
}
apiEntrySchema := toAPIEntrySchema(schema)
apiEntrySchema := apitypes.NewEntrySchema(schema)
if err := jsonEncoder.Encode(apiEntrySchema); err != nil {
return unknownErrorResponse(fmt.Errorf("Could not marshal schema for %v: %v", path, err))
}
Expand Down
11 changes: 11 additions & 0 deletions api/types/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ type Entry struct {
Metadata plugin.JSONObject `json:"metadata"`
}

func NewEntry(e plugin.Entry) Entry {
return Entry{
TypeID: plugin.TypeID(e),
Name: plugin.Name(e),
CName: plugin.CName(e),
Actions: plugin.SupportedActionsOf(e),
Attributes: plugin.Attributes(e),
Metadata: plugin.PartialMetadata(e),
}
}

// Supports returns true if e supports the given action, false
// otherwise.
func (e *Entry) Supports(action plugin.Action) bool {
Expand Down
9 changes: 9 additions & 0 deletions api/types/entrySchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ type EntrySchema struct {
graph *linkedhashmap.Map
}

func NewEntrySchema(s *plugin.EntrySchema) *EntrySchema {
if s == nil {
return nil
}
return &EntrySchema{
EntrySchema: (*s),
}
}

// MarshalJSON marshals the entry's schema to JSON. It takes
// a value receiver so that the entry schema's still marshalled
// when it's referenced as an interface{} object. See
Expand Down
1 change: 1 addition & 0 deletions api/types/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ const (
OutOfBounds = "puppetlabs.wash/out-of-bounds"
NonWashPath = "puppetlabs.wash/non-wash-path"
InvalidBool = "puppetlabs.wash/invalid-bool"
InvalidInt = "puppetlabs.wash/invalid-int"
)

0 comments on commit 6ba54ba

Please sign in to comment.