feat: provider support job mode

This commit is contained in:
Rogee
2024-12-27 20:52:18 +08:00
parent 2fbc3d9fee
commit cdcf20ed33
7 changed files with 131 additions and 60 deletions

14
.vscode/launch.json vendored
View File

@@ -5,7 +5,19 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Launch Package", "name": "provider",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"gen",
"provider",
"/projects/learn/demo01",
]
},
{
"name": "swag",
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",

View File

@@ -18,12 +18,10 @@ func CommandGenProvider(root *cobra.Command) {
Use: "provider", Use: "provider",
Aliases: []string{"p"}, Aliases: []string{"p"},
Short: "Generate providers", Short: "Generate providers",
Long: ` Long: `// @provider
// @provider
// @provider:[except|only] [returnType] [group] // @provider:[except|only] [returnType] [group]
// when except add tag: inject:"false" // when except add tag: inject:"false"
// when only add tag: inject:"true" // when only add tag: inject:"true"`,
`,
RunE: commandGenProviderE, RunE: commandGenProviderE,
} }

View File

@@ -49,6 +49,7 @@ type InjectParam struct {
type Provider struct { type Provider struct {
StructName string StructName string
ReturnType string ReturnType string
Mode string
ProviderGroup string ProviderGroup string
NeedPrepareFunc bool NeedPrepareFunc bool
InjectParams map[string]InjectParam InjectParams map[string]InjectParam
@@ -140,20 +141,47 @@ func Parse(source string) []Provider {
if !strings.HasPrefix(docMark, "@provider") { if !strings.HasPrefix(docMark, "@provider") {
continue 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 provider.ReturnType = "*" + provider.StructName
} else {
provider.ReturnType = returnType
} }
onlyMode := mode == "only"
exceptMode := mode == "except" if providerDoc.Mode == "job" {
log.Infof("[%s] %s => ONLY: %+v, EXCEPT: %+v, Type: %s, Group: %s", source, declType.Name.Name, onlyMode, exceptMode, provider.ReturnType, provider.ProviderGroup) 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 { for _, field := range structType.Fields.List {
if field.Names == nil { if field.Names == nil {
@@ -164,13 +192,11 @@ func Parse(source string) []Provider {
provider.NeedPrepareFunc = true provider.NeedPrepareFunc = true
} }
if onlyMode { if providerDoc.IsOnly {
if field.Tag == nil || !strings.Contains(field.Tag.Value, `inject:"true"`) { if field.Tag == nil || !strings.Contains(field.Tag.Value, `inject:"true"`) {
continue continue
} }
} } else {
if exceptMode {
if field.Tag != nil && strings.Contains(field.Tag.Value, `inject:"false"`) { if field.Tag != nil && strings.Contains(field.Tag.Value, `inject:"false"`) {
continue continue
} }
@@ -251,30 +277,62 @@ func Parse(source string) []Provider {
return providers return providers
} }
func parseDoc(doc string) (string, string, string) { // @provider(mode):[except|only] [returnType] [group]
// @provider:[except|only] [returnType] [group] type ProviderDescribe struct {
doc = strings.TrimLeft(doc[len("@provider"):], ":") IsOnly bool
if !strings.HasPrefix(doc, "except") && !strings.HasPrefix(doc, "only") { Mode string // job
doc = "except " + doc ReturnType string
Group string
} }
doc = strings.ReplaceAll(doc, "\t", " ") func (p ProviderDescribe) String() {
cmds := strings.Split(doc, " ") // log.Infof("[%s] %s => ONLY: %+v, EXCEPT: %+v, Type: %s, Group: %s", source, declType.Name.Name, onlyMode, exceptMode, provider.ReturnType, provider.ProviderGroup)
cmds = lo.Filter(cmds, func(item string, idx int) bool {
return strings.TrimSpace(item) != ""
})
if len(cmds) == 0 {
return "except", "#", ""
} }
if len(cmds) == 1 { // @provider
return cmds[0], "#", "" // @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
} }
if len(cmds) == 2 { // Handle :except and :only
return cmds[0], cmds[1], "" 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)
} }
return cmds[0], cmds[1], cmds[2] // 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
} }

View File

@@ -19,14 +19,22 @@ func Provide(opts ...opt.Option) error {
) ({{.ReturnType}}, error) { ) ({{.ReturnType}}, error) {
obj := &{{.StructName}}{ obj := &{{.StructName}}{
{{- range $key, $param := .InjectParams }} {{- range $key, $param := .InjectParams }}
{{- if ne $key "__job"}}
{{$key}}: {{$key}}, {{$key}}: {{$key}},
{{- end}} {{- end}}
{{- end }}
} }
{{- if .NeedPrepareFunc }} {{- if .NeedPrepareFunc }}
if err := obj.Prepare(); err != nil { if err := obj.Prepare(); err != nil {
return nil, err return nil, err
} }
{{- end }} {{- end }}
{{- if eq .Mode "job"}}
if err := river.AddWorkerSafely(__job.Workers, obj); err != nil {
return nil, err
}
{{- end }}
return obj, nil return obj, nil
}{{if .ProviderGroup}}, {{.ProviderGroup}}{{end}}); err != nil { }{{if .ProviderGroup}}, {{.ProviderGroup}}{{end}}); err != nil {
return err return err

View File

@@ -56,4 +56,3 @@ func Header[T any](name string) func(fiber.Ctx) (*T, error) {
return p, nil return p, nil
} }
} }

View File

@@ -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, f func(fiber.Ctx, P1, P2) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4, P5) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4, P5, P6) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7, P8) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7, P8, P9) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, 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, f func(fiber.Ctx, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) error,
pf1 func(fiber.Ctx) (P1, error), pf1 func(fiber.Ctx) (P1, error),
pf2 func(fiber.Ctx) (P2, error), pf2 func(fiber.Ctx) (P2, error),

View File

@@ -1,10 +1,6 @@
package {{.Name}} package {{.Name}}
import ( import (
"time"
log "github.com/sirupsen/logrus"
"git.ipao.vip/rogeecn/atom/container" "git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/utils/opt" "git.ipao.vip/rogeecn/atom/utils/opt"
) )