feat: provider support job mode
This commit is contained in:
14
.vscode/launch.json
vendored
14
.vscode/launch.json
vendored
@@ -5,7 +5,19 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"name": "provider",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}",
|
||||
"args": [
|
||||
"gen",
|
||||
"provider",
|
||||
"/projects/learn/demo01",
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "swag",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
|
||||
@@ -18,12 +18,10 @@ func CommandGenProvider(root *cobra.Command) {
|
||||
Use: "provider",
|
||||
Aliases: []string{"p"},
|
||||
Short: "Generate providers",
|
||||
Long: `
|
||||
// @provider
|
||||
Long: `// @provider
|
||||
// @provider:[except|only] [returnType] [group]
|
||||
// when except add tag: inject:"false"
|
||||
// when only add tag: inject:"true"
|
||||
`,
|
||||
// when only add tag: inject:"true"`,
|
||||
RunE: commandGenProviderE,
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ type InjectParam struct {
|
||||
type Provider struct {
|
||||
StructName string
|
||||
ReturnType string
|
||||
Mode string
|
||||
ProviderGroup string
|
||||
NeedPrepareFunc bool
|
||||
InjectParams map[string]InjectParam
|
||||
@@ -140,20 +141,47 @@ func Parse(source string) []Provider {
|
||||
if !strings.HasPrefix(docMark, "@provider") {
|
||||
continue
|
||||
}
|
||||
mode, returnType, group := parseDoc(docMark)
|
||||
if group != "" {
|
||||
provider.ProviderGroup = group
|
||||
}
|
||||
// log.Infof("mode: %s, returnType: %s, group: %s", mode, returnType, group)
|
||||
|
||||
if returnType == "#" {
|
||||
// mode, returnType, group := parseDoc(docMark)
|
||||
// // log.Infof("mode: %s, returnType: %s, group: %s", mode, returnType, group)
|
||||
|
||||
// if returnType == "#" {
|
||||
// provider.ReturnType = "*" + provider.StructName
|
||||
// } else {
|
||||
// provider.ReturnType = returnType
|
||||
// }
|
||||
|
||||
// onlyMode := mode == "only"
|
||||
// exceptMode := mode == "except"
|
||||
// log.Infof("[%s] %s => ONLY: %+v, EXCEPT: %+v, Type: %s, Group: %s", source, declType.Name.Name, onlyMode, exceptMode, provider.ReturnType, provider.ProviderGroup)
|
||||
|
||||
providerDoc := parseProvider(docMark)
|
||||
log.Infof("[%s] %s %+v", source, declType.Name.Name, providerDoc)
|
||||
provider.ProviderGroup = providerDoc.Group
|
||||
provider.ReturnType = providerDoc.ReturnType
|
||||
if provider.ReturnType == "" {
|
||||
provider.ReturnType = "*" + provider.StructName
|
||||
} else {
|
||||
provider.ReturnType = returnType
|
||||
}
|
||||
onlyMode := mode == "only"
|
||||
exceptMode := mode == "except"
|
||||
log.Infof("[%s] %s => ONLY: %+v, EXCEPT: %+v, Type: %s, Group: %s", source, declType.Name.Name, onlyMode, exceptMode, provider.ReturnType, provider.ProviderGroup)
|
||||
|
||||
if providerDoc.Mode == "job" {
|
||||
provider.Mode = "job"
|
||||
|
||||
jobPkg := gomod.GetModuleName() + "/providers/job"
|
||||
|
||||
provider.Imports["git.ipao.vip/rogeecn/atom/contracts"] = ""
|
||||
provider.Imports["github.com/riverqueue/river"] = ""
|
||||
provider.Imports[jobPkg] = ""
|
||||
|
||||
provider.ProviderGroup = "atom.GroupInitial"
|
||||
provider.ReturnType = "contracts.Initial"
|
||||
|
||||
provider.InjectParams["__job"] = InjectParam{
|
||||
Star: "*",
|
||||
Type: "Job",
|
||||
Package: jobPkg,
|
||||
PackageAlias: "job",
|
||||
}
|
||||
}
|
||||
|
||||
for _, field := range structType.Fields.List {
|
||||
if field.Names == nil {
|
||||
@@ -164,13 +192,11 @@ func Parse(source string) []Provider {
|
||||
provider.NeedPrepareFunc = true
|
||||
}
|
||||
|
||||
if onlyMode {
|
||||
if providerDoc.IsOnly {
|
||||
if field.Tag == nil || !strings.Contains(field.Tag.Value, `inject:"true"`) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if exceptMode {
|
||||
} else {
|
||||
if field.Tag != nil && strings.Contains(field.Tag.Value, `inject:"false"`) {
|
||||
continue
|
||||
}
|
||||
@@ -251,30 +277,62 @@ func Parse(source string) []Provider {
|
||||
return providers
|
||||
}
|
||||
|
||||
func parseDoc(doc string) (string, string, string) {
|
||||
// @provider:[except|only] [returnType] [group]
|
||||
doc = strings.TrimLeft(doc[len("@provider"):], ":")
|
||||
if !strings.HasPrefix(doc, "except") && !strings.HasPrefix(doc, "only") {
|
||||
doc = "except " + doc
|
||||
}
|
||||
|
||||
doc = strings.ReplaceAll(doc, "\t", " ")
|
||||
cmds := strings.Split(doc, " ")
|
||||
cmds = lo.Filter(cmds, func(item string, idx int) bool {
|
||||
return strings.TrimSpace(item) != ""
|
||||
})
|
||||
|
||||
if len(cmds) == 0 {
|
||||
return "except", "#", ""
|
||||
}
|
||||
|
||||
if len(cmds) == 1 {
|
||||
return cmds[0], "#", ""
|
||||
}
|
||||
|
||||
if len(cmds) == 2 {
|
||||
return cmds[0], cmds[1], ""
|
||||
}
|
||||
|
||||
return cmds[0], cmds[1], cmds[2]
|
||||
// @provider(mode):[except|only] [returnType] [group]
|
||||
type ProviderDescribe struct {
|
||||
IsOnly bool
|
||||
Mode string // job
|
||||
ReturnType string
|
||||
Group string
|
||||
}
|
||||
|
||||
func (p ProviderDescribe) String() {
|
||||
// log.Infof("[%s] %s => ONLY: %+v, EXCEPT: %+v, Type: %s, Group: %s", source, declType.Name.Name, onlyMode, exceptMode, provider.ReturnType, provider.ProviderGroup)
|
||||
}
|
||||
|
||||
// @provider
|
||||
// @provider(job)
|
||||
// @provider(job):except
|
||||
// @provider:except
|
||||
// @provider:only
|
||||
// @provider returnType
|
||||
// @provider returnType group
|
||||
// @provider(job) returnType group
|
||||
func parseProvider(doc string) ProviderDescribe {
|
||||
result := ProviderDescribe{IsOnly: false}
|
||||
|
||||
// Remove @provider prefix
|
||||
doc = strings.TrimSpace(strings.TrimPrefix(doc, "@provider"))
|
||||
|
||||
// Handle empty case
|
||||
if doc == "" {
|
||||
return result
|
||||
}
|
||||
|
||||
// Handle :except and :only
|
||||
if strings.Contains(doc, ":except") {
|
||||
result.IsOnly = false
|
||||
doc = strings.Replace(doc, ":except", "", 1)
|
||||
} else if strings.Contains(doc, ":only") {
|
||||
result.IsOnly = true
|
||||
doc = strings.Replace(doc, ":only", "", 1)
|
||||
}
|
||||
|
||||
// Handle mode in parentheses
|
||||
if strings.Contains(doc, "(") && strings.Contains(doc, ")") {
|
||||
start := strings.Index(doc, "(")
|
||||
end := strings.Index(doc, ")")
|
||||
result.Mode = doc[start+1 : end]
|
||||
doc = doc[:start] + doc[end+1:]
|
||||
}
|
||||
|
||||
// Handle remaining parts (returnType and group)
|
||||
parts := strings.Fields(strings.TrimSpace(doc))
|
||||
if len(parts) >= 1 {
|
||||
result.ReturnType = parts[0]
|
||||
}
|
||||
if len(parts) >= 2 {
|
||||
result.Group = parts[1]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -19,7 +19,9 @@ func Provide(opts ...opt.Option) error {
|
||||
) ({{.ReturnType}}, error) {
|
||||
obj := &{{.StructName}}{
|
||||
{{- range $key, $param := .InjectParams }}
|
||||
{{- if ne $key "__job"}}
|
||||
{{$key}}: {{$key}},
|
||||
{{- end}}
|
||||
{{- end }}
|
||||
}
|
||||
{{- if .NeedPrepareFunc }}
|
||||
@@ -27,6 +29,12 @@ func Provide(opts ...opt.Option) error {
|
||||
return nil, err
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{- if eq .Mode "job"}}
|
||||
if err := river.AddWorkerSafely(__job.Workers, obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{- end }}
|
||||
return obj, nil
|
||||
}{{if .ProviderGroup}}, {{.ProviderGroup}}{{end}}); err != nil {
|
||||
return err
|
||||
|
||||
@@ -56,4 +56,3 @@ func Header[T any](name string) func(fiber.Ctx) (*T, error) {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ func Func1[P1 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func2[P1 any, P2 any](
|
||||
func Func2[P1, P2 any](
|
||||
f func(fiber.Ctx, P1, P2) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -42,7 +42,7 @@ func Func2[P1 any, P2 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func3[P1 any, P2 any, P3 any](
|
||||
func Func3[P1, P2, P3 any](
|
||||
f func(fiber.Ctx, P1, P2, P3) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -66,7 +66,7 @@ func Func3[P1 any, P2 any, P3 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func4[P1 any, P2 any, P3 any, P4 any](
|
||||
func Func4[P1, P2, P3, P4 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -98,7 +98,7 @@ func Func4[P1 any, P2 any, P3 any, P4 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func5[P1 any, P2 any, P3 any, P4 any, P5 any](
|
||||
func Func5[P1, P2, P3, P4, P5 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4, P5) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -131,7 +131,7 @@ func Func5[P1 any, P2 any, P3 any, P4 any, P5 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func6[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any](
|
||||
func Func6[P1, P2, P3, P4, P5, P6 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4, P5, P6) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -169,7 +169,7 @@ func Func6[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func7[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any](
|
||||
func Func7[P1, P2, P3, P4, P5, P6, P7 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -212,7 +212,7 @@ func Func7[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func8[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any, P8 any](
|
||||
func Func8[P1, P2, P3, P4, P5, P6, P7, P8 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7, P8) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -260,7 +260,7 @@ func Func8[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any, P8 any](
|
||||
}
|
||||
}
|
||||
|
||||
func Func9[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any, P8 any, P9 any](
|
||||
func Func9[P1, P2, P3, P4, P5, P6, P7, P8, P9 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7, P8, P9) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
@@ -313,7 +313,7 @@ func Func9[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any, P8 any, P9 an
|
||||
}
|
||||
}
|
||||
|
||||
func Func10[P1 any, P2 any, P3 any, P4 any, P5 any, P6 any, P7 any, P8 any, P9 any, P10 any](
|
||||
func Func10[P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 any](
|
||||
f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) error,
|
||||
pf1 func(fiber.Ctx) (P1, error),
|
||||
pf2 func(fiber.Ctx) (P2, error),
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package {{.Name}}
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user