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

Implements '*'(expand all) field selector. Closes: #147 #228

Merged
merged 3 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,29 @@ $ http -b :8080/api/users/ar6eimekj5lfktka9mt0/posts fields=='meta{title,body}'
]
```

Also `all fields` expansion is supported:
```sh
$ http -b :8080/api/users/ar6eimekj5lfktka9mt0/posts fields=='*,user{*}'
[
{
"_etag": "ar6eimukj5lfl07r0uv0",
"id": "ar6eimukj5lfl07r0ugz",
"created": "2015-07-27T21:46:55.355857401+02:00",
"updated": "2015-07-27T21:46:55.355857989+02:00",
"user": {
"id": "ar6eimukj5lfl07gzb0b",
"created": "2015-07-24T21:46:55.355857401+02:00",
"updated": "2015-07-24T21:46:55.355857989+02:00",
"name": "John Snow",
},
"meta": {
"title": "test",
"body": "example"
}
}
]
```

#### Field Aliasing

It's also possible to rename fields in the response using aliasing. To create an alias, prefix the field name by the wanted alias separated by a colon (`:`):
Expand Down
6 changes: 3 additions & 3 deletions resource/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ type refChecker struct {
}

// ReferenceChecker implements the schema.ReferenceChecker interface.
func (rc refChecker) ReferenceChecker(path string) schema.FieldValidator {
func (rc refChecker) ReferenceChecker(path string) (schema.FieldValidator, schema.Validator) {
rsc, exists := rc.index.GetResource(path, nil)
if !exists {
return nil
return nil, nil
}
validator := rsc.Schema().Fields["id"].Validator

Expand All @@ -134,7 +134,7 @@ func (rc refChecker) ReferenceChecker(path string) schema.FieldValidator {
return nil, err
}
return id, nil
})
}), rsc.Validator()
}

// assertNotBound asserts a given resource name is not already bound.
Expand Down
11 changes: 6 additions & 5 deletions schema/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ func (tc fieldSerializerTestCase) Run(t *testing.T) {
}

type fakeReferenceChecker map[string]struct {
IDs []interface{}
Validator schema.FieldValidator
IDs []interface{}
Validator schema.FieldValidator
SchemaValidator schema.Validator
}

func (rc fakeReferenceChecker) Compile() error {
Expand All @@ -172,10 +173,10 @@ func (rc fakeReferenceChecker) Compile() error {
return nil
}

func (rc fakeReferenceChecker) ReferenceChecker(path string) schema.FieldValidator {
func (rc fakeReferenceChecker) ReferenceChecker(path string) (schema.FieldValidator, schema.Validator) {
rsc, ok := rc[path]
if !ok {
return nil
return nil, nil
}
return schema.FieldValidatorFunc(func(value interface{}) (interface{}, error) {
var id interface{}
Expand All @@ -197,5 +198,5 @@ func (rc fakeReferenceChecker) ReferenceChecker(path string) schema.FieldValidat
}
}
return nil, errors.New("not found")
})
}), rsc.SchemaValidator
}
20 changes: 12 additions & 8 deletions schema/alloff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ func TestAllOfValidator(t *testing.T) {
},
ReferenceChecker: fakeReferenceChecker{
"foo": {
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
"bar": {
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
},
Input: "foo1",
Expand Down Expand Up @@ -96,12 +98,14 @@ func TestAllOfQueryValidator(t *testing.T) {
},
ReferenceChecker: fakeReferenceChecker{
"foo": {
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
"bar": {
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
},
Input: "foo1",
Expand Down
24 changes: 14 additions & 10 deletions schema/anyof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestAnyOfCompile(t *testing.T) {
{
Name: "{Reference{Path:valid}}",
Compiler: &schema.AnyOf{&schema.Reference{Path: "items"}},
ReferenceChecker: fakeReferenceChecker{"items": {IDs: []interface{}{1, 2, 3}, Validator: &schema.Integer{}}},
ReferenceChecker: fakeReferenceChecker{"items": {IDs: []interface{}{1, 2, 3}, Validator: &schema.Integer{}, SchemaValidator: &schema.Schema{}}},
},
{
Name: "{Reference{Path:invalid}}",
Compiler: &schema.AnyOf{&schema.Reference{Path: "foobar"}},
ReferenceChecker: fakeReferenceChecker{"items": {IDs: []interface{}{1, 2, 3}, Validator: &schema.Integer{}}},
ReferenceChecker: fakeReferenceChecker{"items": {IDs: []interface{}{1, 2, 3}, Validator: &schema.Integer{}, SchemaValidator: &schema.Schema{}}},
Error: "can't find resource 'foobar'",
},
}
Expand Down Expand Up @@ -97,12 +97,14 @@ func TestAnyOfValidate(t *testing.T) {
},
ReferenceChecker: fakeReferenceChecker{
"foo": {
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
"bar": {
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
},
Input: "foo1",
Expand Down Expand Up @@ -148,12 +150,14 @@ func TestAnyOfQueryValidate(t *testing.T) {
},
ReferenceChecker: fakeReferenceChecker{
"foo": {
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
IDs: []interface{}{"foo1"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
"bar": {
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
IDs: []interface{}{"bar1", "bar2", "bar3"},
Validator: &schema.String{},
SchemaValidator: &schema.Schema{},
},
},
Input: "foo1",
Expand Down
2 changes: 1 addition & 1 deletion schema/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Compiler interface {
type ReferenceChecker interface {
// ReferenceChecker should return a FieldValidator that can be used for validate that a referenced ID exists and
// is of the right format. If there is no resource matching path, nil should e returned.
ReferenceChecker(path string) FieldValidator
ReferenceChecker(path string) (FieldValidator, Validator)
}

// ReferenceCheckerFunc is an adapter that allows ordinary functions to be used as reference checkers.
Expand Down
4 changes: 2 additions & 2 deletions schema/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestDictCompile(t *testing.T) {
{
Name: "{Values.Validator:Reference{Path:valid}}",
Compiler: &schema.Dict{Values: schema.Field{Validator: &schema.Reference{Path: "foo"}}},
ReferenceChecker: fakeReferenceChecker{"foo": {}},
ReferenceChecker: fakeReferenceChecker{"foo": {SchemaValidator: &schema.Schema{}}},
},
{
Name: "{Values.Validator:Reference{Path:invalid}}",
Compiler: &schema.Dict{Values: schema.Field{Validator: &schema.Reference{Path: "bar"}}},
ReferenceChecker: fakeReferenceChecker{"foo": {}},
ReferenceChecker: fakeReferenceChecker{"foo": {SchemaValidator: &schema.Schema{}}},
Error: "can't find resource 'bar'",
},
}
Expand Down
8 changes: 4 additions & 4 deletions schema/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func TestObjectCompile(t *testing.T) {
Compiler: &schema.Object{Schema: &schema.Schema{Fields: schema.Fields{
"foo": {Validator: &schema.Reference{Path: "bar"}},
}}},
ReferenceChecker: fakeReferenceChecker{"bar": {}},
ReferenceChecker: fakeReferenceChecker{"bar": {SchemaValidator: &schema.Schema{}}},
},
{
Name: `{Schema:{"foo":Reference{Path:invalid}}}`,
Compiler: &schema.Object{Schema: &schema.Schema{Fields: schema.Fields{
"foo": {Validator: &schema.Reference{Path: "foobar"}},
}}},
ReferenceChecker: fakeReferenceChecker{"bar": {}},
ReferenceChecker: fakeReferenceChecker{"bar": {SchemaValidator: &schema.Schema{}}},
Error: "foo: can't find resource 'foobar'",
},
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestObjectValidate(t *testing.T) {
"foo": {Validator: &schema.Reference{Path: "bar"}},
}}},
ReferenceChecker: fakeReferenceChecker{
"bar": {IDs: []interface{}{"a", "b"}, Validator: &schema.String{}},
"bar": {IDs: []interface{}{"a", "b"}, Validator: &schema.String{}, SchemaValidator: &schema.Schema{}},
},
Input: map[string]interface{}{"foo": "a"},
Expect: map[string]interface{}{"foo": "a"},
Expand All @@ -92,7 +92,7 @@ func TestObjectValidate(t *testing.T) {
"foo": {Validator: &schema.Reference{Path: "bar"}},
}}},
ReferenceChecker: fakeReferenceChecker{
"bar": {IDs: []interface{}{"a", "b"}, Validator: &schema.String{}},
"bar": {IDs: []interface{}{"a", "b"}, Validator: &schema.String{}, SchemaValidator: &schema.Schema{}},
},
Input: map[string]interface{}{"foo": "c"},
Error: "foo is [not found]",
Expand Down
4 changes: 2 additions & 2 deletions schema/query/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type ProjectionField struct {
}

// Validate validates the projection against the provided validator.
func (p Projection) Validate(validator schema.Validator) error {
func (p Projection) Validate(fg schema.FieldGetter) error {
for _, pf := range p {
if err := pf.Validate(validator); err != nil {
if err := pf.Validate(fg); err != nil {
return err
}
}
Expand Down
Loading