Generate Mongo repository code from repository spec
This commit is contained in:
parent
3808684ed0
commit
a24a9f81d7
10 changed files with 470 additions and 19 deletions
internal/spec
|
@ -32,15 +32,15 @@ type Operation interface {
|
|||
// FindOperation is a method specification for find operations
|
||||
type FindOperation struct {
|
||||
Mode QueryMode
|
||||
Query Query
|
||||
Query QuerySpec
|
||||
}
|
||||
|
||||
// Query is a condition of querying the database
|
||||
type Query struct {
|
||||
// QuerySpec is a condition of querying the database
|
||||
type QuerySpec struct {
|
||||
Fields []string
|
||||
}
|
||||
|
||||
// NumberOfArguments returns number of arguments required to perform the query
|
||||
func (q Query) NumberOfArguments() int {
|
||||
func (q QuerySpec) NumberOfArguments() int {
|
||||
return len(q.Fields)
|
||||
}
|
||||
|
|
|
@ -58,12 +58,12 @@ func (p repositoryInterfaceParser) parseFindMethod(method code.Method, tokens []
|
|||
return MethodSpec{}, err
|
||||
}
|
||||
|
||||
query, err := p.parseQuery(tokens)
|
||||
querySpec, err := p.parseQuery(tokens)
|
||||
if err != nil {
|
||||
return MethodSpec{}, err
|
||||
}
|
||||
|
||||
if query.NumberOfArguments()+1 != len(method.Params) {
|
||||
if querySpec.NumberOfArguments()+1 != len(method.Params) {
|
||||
return MethodSpec{}, errors.New("method parameter not supported")
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ func (p repositoryInterfaceParser) parseFindMethod(method code.Method, tokens []
|
|||
Returns: method.Returns,
|
||||
Operation: FindOperation{
|
||||
Mode: mode,
|
||||
Query: query,
|
||||
Query: querySpec,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
@ -111,13 +111,13 @@ func (p repositoryInterfaceParser) extractFindReturns(returns []code.Type) (Quer
|
|||
return "", errors.New("method return not supported")
|
||||
}
|
||||
|
||||
func (p repositoryInterfaceParser) parseQuery(tokens []string) (Query, error) {
|
||||
func (p repositoryInterfaceParser) parseQuery(tokens []string) (QuerySpec, error) {
|
||||
if len(tokens) == 0 {
|
||||
return Query{}, errors.New("method name not supported")
|
||||
return QuerySpec{}, errors.New("method name not supported")
|
||||
}
|
||||
|
||||
if len(tokens) == 1 && tokens[0] == "All" {
|
||||
return Query{}, nil
|
||||
return QuerySpec{}, nil
|
||||
}
|
||||
|
||||
if tokens[0] == "One" {
|
||||
|
@ -128,7 +128,7 @@ func (p repositoryInterfaceParser) parseQuery(tokens []string) (Query, error) {
|
|||
}
|
||||
|
||||
if tokens[0] == "And" {
|
||||
return Query{}, errors.New("method name not supported")
|
||||
return QuerySpec{}, errors.New("method name not supported")
|
||||
}
|
||||
var queryFields []string
|
||||
var aggregatedToken string
|
||||
|
@ -141,9 +141,9 @@ func (p repositoryInterfaceParser) parseQuery(tokens []string) (Query, error) {
|
|||
}
|
||||
}
|
||||
if aggregatedToken == "" {
|
||||
return Query{}, errors.New("method name not supported")
|
||||
return QuerySpec{}, errors.New("method name not supported")
|
||||
}
|
||||
queryFields = append(queryFields, aggregatedToken)
|
||||
|
||||
return Query{Fields: queryFields}, nil
|
||||
return QuerySpec{Fields: queryFields}, nil
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ func TestParseRepositoryInterface(t *testing.T) {
|
|||
},
|
||||
Operation: spec.FindOperation{
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.Query{Fields: []string{"ID"}},
|
||||
Query: spec.QuerySpec{Fields: []string{"ID"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -98,7 +98,7 @@ func TestParseRepositoryInterface(t *testing.T) {
|
|||
Operation: spec.FindOperation{
|
||||
|
||||
Mode: spec.QueryModeOne,
|
||||
Query: spec.Query{Fields: []string{"PhoneNumber"}},
|
||||
Query: spec.QuerySpec{Fields: []string{"PhoneNumber"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -137,7 +137,7 @@ func TestParseRepositoryInterface(t *testing.T) {
|
|||
},
|
||||
Operation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.Query{Fields: []string{"City"}},
|
||||
Query: spec.QuerySpec{Fields: []string{"City"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -214,7 +214,7 @@ func TestParseRepositoryInterface(t *testing.T) {
|
|||
},
|
||||
Operation: spec.FindOperation{
|
||||
Mode: spec.QueryModeMany,
|
||||
Query: spec.Query{Fields: []string{"City", "Gender"}},
|
||||
Query: spec.QuerySpec{Fields: []string{"City", "Gender"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue