Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
84 lines
2 KiB
Go
84 lines
2 KiB
Go
package scaffolder
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gobuffalo/genny/v2"
|
|
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/multiformatname"
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/templates/field"
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/templates/query"
|
|
)
|
|
|
|
// AddQuery adds a new query to scaffolded app.
|
|
func (s Scaffolder) AddQuery(
|
|
ctx context.Context,
|
|
moduleName,
|
|
queryName,
|
|
description string,
|
|
reqFields,
|
|
resFields []string,
|
|
paginated bool,
|
|
) error {
|
|
// If no module is provided, we add the type to the app's module
|
|
if moduleName == "" {
|
|
moduleName = s.modpath.Package
|
|
}
|
|
mfName, err := multiformatname.NewName(moduleName, multiformatname.NoNumber)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
moduleName = mfName.LowerCase
|
|
|
|
name, err := multiformatname.NewName(queryName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := checkComponentValidity(s.appPath, moduleName, name, true); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check and parse provided request fields
|
|
if ok := containsCustomTypes(reqFields); ok {
|
|
return errors.New("query request params can't contain custom type")
|
|
}
|
|
parsedReqFields, err := field.ParseFields(reqFields, checkGoReservedWord)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check and parse provided response fields
|
|
if err := checkCustomTypes(ctx, s.appPath, s.modpath.Package, s.protoDir, moduleName, resFields); err != nil {
|
|
return err
|
|
}
|
|
parsedResFields, err := field.ParseFields(resFields, checkGoReservedWord)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var (
|
|
g *genny.Generator
|
|
opts = &query.Options{
|
|
AppName: s.modpath.Package,
|
|
ProtoDir: s.protoDir,
|
|
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
|
|
ModulePath: s.modpath.RawPath,
|
|
ModuleName: moduleName,
|
|
QueryName: name,
|
|
ReqFields: parsedReqFields,
|
|
ResFields: parsedResFields,
|
|
Description: description,
|
|
Paginated: paginated,
|
|
}
|
|
)
|
|
|
|
// Scaffold
|
|
g, err = query.NewGenerator(opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.Run(g)
|
|
}
|