From 3224fed9d47b8435b461c3b0387967c092f1f894 Mon Sep 17 00:00:00 2001 From: Morlay Date: Fri, 6 Sep 2024 18:31:56 +0800 Subject: [PATCH] fix: concat comments in nest --- cmd/example/apis/org/zz_generated.enum.go | 18 +++++---- cmd/example/apis/org/zz_generated.operator.go | 11 ------ cmd/example/main.go | 2 + internal/otel/zz_generated.enum.go | 4 +- pkg/cli/cmd.go | 38 +++++++++++++------ pkg/cli/zz_generated.runtimedoc.go | 15 ++++++++ pkg/cron/job.go | 3 ++ pkg/cron/zz_generated.runtimedoc.go | 6 ++- 8 files changed, 66 insertions(+), 31 deletions(-) diff --git a/cmd/example/apis/org/zz_generated.enum.go b/cmd/example/apis/org/zz_generated.enum.go index 53027d2..0804245 100644 --- a/cmd/example/apis/org/zz_generated.enum.go +++ b/cmd/example/apis/org/zz_generated.enum.go @@ -7,6 +7,7 @@ package org import ( bytes "bytes" database_sql_driver "database/sql/driver" + fmt "fmt" github_com_octohelm_storage_pkg_enumeration "github.com/octohelm/storage/pkg/enumeration" github_com_pkg_errors "github.com/pkg/errors" @@ -20,11 +21,7 @@ func (Type) EnumValues() []any { } } func (v Type) MarshalText() ([]byte, error) { - str := v.String() - if str == "UNKNOWN" { - return nil, InvalidType - } - return []byte(str), nil + return []byte(v.String()), nil } func (v *Type) UnmarshalText(data []byte) error { @@ -44,6 +41,11 @@ func ParseTypeFromString(s string) (Type, error) { return TYPE__COMPANY, nil default: + var i Type + _, err := fmt.Sscanf(s, "UNKNOWN_%d", &i) + if err == nil { + return i, nil + } return TYPE_UNKNOWN, InvalidType } } @@ -55,8 +57,10 @@ func (v Type) String() string { case TYPE__COMPANY: return "COMPANY" - default: + case TYPE_UNKNOWN: return "UNKNOWN" + default: + return fmt.Sprintf("UNKNOWN_%d", v) } } @@ -80,7 +84,7 @@ func (v Type) Label() string { return "企事业单位" default: - return "UNKNOWN" + return fmt.Sprint(v) } } diff --git a/cmd/example/apis/org/zz_generated.operator.go b/cmd/example/apis/org/zz_generated.operator.go index c7f475a..5c722cf 100644 --- a/cmd/example/apis/org/zz_generated.operator.go +++ b/cmd/example/apis/org/zz_generated.operator.go @@ -6,7 +6,6 @@ package org import ( github_com_octohelm_courier_pkg_courier "github.com/octohelm/courier/pkg/courier" - github_com_octohelm_courier_pkg_statuserror "github.com/octohelm/courier/pkg/statuserror" ) func init() { @@ -33,16 +32,6 @@ func (*GetOrg) ResponseContent() any { return new(Detail) } -func (*GetOrg) ResponseErrors() []error { - return []error{ - &(github_com_octohelm_courier_pkg_statuserror.StatusErr{ - Code: 404, - Key: "NotFound", - Msg: "NotFound", - }), - } -} - func init() { R.Register(github_com_octohelm_courier_pkg_courier.NewRouter(&ListOrg{})) } diff --git a/cmd/example/main.go b/cmd/example/main.go index b84b0b5..2479ff7 100644 --- a/cmd/example/main.go +++ b/cmd/example/main.go @@ -4,6 +4,8 @@ import ( "context" "github.com/innoai-tech/infra/pkg/cli" + + _ "github.com/innoai-tech/infra/pkg/cron" ) var App = cli.NewApp( diff --git a/internal/otel/zz_generated.enum.go b/internal/otel/zz_generated.enum.go index 13c89af..4172f3a 100644 --- a/internal/otel/zz_generated.enum.go +++ b/internal/otel/zz_generated.enum.go @@ -5,6 +5,8 @@ DON'T EDIT THIS FILE package otel import ( + fmt "fmt" + github_com_pkg_errors "github.com/pkg/errors" ) @@ -43,6 +45,6 @@ func (v LogLevel) Label() string { return "warn" default: - return "UNKNOWN" + return fmt.Sprint(v) } } diff --git a/pkg/cli/cmd.go b/pkg/cli/cmd.go index 5222085..f6f8a73 100644 --- a/pkg/cli/cmd.go +++ b/pkg/cli/cmd.go @@ -52,10 +52,10 @@ func addConfigurator(c *C, fv reflect.Value, flags *pflag.FlagSet, name string, envPrefix = fmt.Sprintf("%s_", appName) } - collectFlagsFromConfigurator(c, flags, fv, name, envPrefix) + collectFlagsFromConfigurator(c, flags, fv, name, envPrefix, "") } -func collectFlagsFromConfigurator(c *C, flags *pflag.FlagSet, rv reflect.Value, prefix string, envPrefix string) { +func collectFlagsFromConfigurator(c *C, flags *pflag.FlagSet, rv reflect.Value, prefix string, envPrefix string, parentDoc string) { var docer CanRuntimeDoc if rv.CanAddr() { @@ -134,28 +134,44 @@ func collectFlagsFromConfigurator(c *C, flags *pflag.FlagSet, rv reflect.Value, flagName = prefix + "_" + flagName } + doc := parentDoc + + if docer != nil { + if lines, ok := docer.RuntimeDoc(); ok { + if d := strings.Join(lines, "\n"); d != "" { + if doc != "" { + doc += ": \n" + } + doc += d + } + } + + if lines, ok := docer.RuntimeDoc(ft.Name); ok { + if d := strings.Join(lines, "\n"); d != "" { + if doc != "" { + doc += ": \n" + } + doc += d + } + } + } + if ft.Type.Kind() == reflect.Struct && ff.Type() != "string" { if ft.Anonymous { - collectFlagsFromConfigurator(c, flags, fv, prefix, envPrefix) + collectFlagsFromConfigurator(c, flags, fv, prefix, envPrefix, doc) } else { - collectFlagsFromConfigurator(c, flags, fv, flagName, envPrefix) + collectFlagsFromConfigurator(c, flags, fv, flagName, envPrefix, doc) } continue } - if docer != nil { - lines, ok := docer.RuntimeDoc(ft.Name) - if ok { - ff.Desc = strings.Join(lines, "\n") - } - } - if can, ok := fv.Interface().(interface{ EnumValues() []any }); ok { ff.EnumValues = can.EnumValues() } ff.Name = camelcase.LowerKebabCase(flagName) ff.EnvVar = camelcase.UpperSnakeCase(fmt.Sprintf("%s%s", envPrefix, flagName)) + ff.Desc = doc c.flagVars = append(c.flagVars, ff) ff.Apply(flags) diff --git a/pkg/cli/zz_generated.runtimedoc.go b/pkg/cli/zz_generated.runtimedoc.go index 7d11b8a..ffb1c4f 100644 --- a/pkg/cli/zz_generated.runtimedoc.go +++ b/pkg/cli/zz_generated.runtimedoc.go @@ -31,6 +31,21 @@ func (v App) RuntimeDoc(names ...string) ([]string, bool) { return []string{}, true } +func (v Component) RuntimeDoc(names ...string) ([]string, bool) { + if len(names) > 0 { + switch names[0] { + case "Name": + return []string{}, true + case "Options": + return []string{}, true + + } + + return nil, false + } + return []string{}, true +} + func (v Info) RuntimeDoc(names ...string) ([]string, bool) { if len(names) > 0 { switch names[0] { diff --git a/pkg/cron/job.go b/pkg/cron/job.go index 9a379e0..629e5df 100644 --- a/pkg/cron/job.go +++ b/pkg/cron/job.go @@ -21,6 +21,9 @@ func (i IntervalSchedule) Next(t time.Time) time.Time { } type Job struct { + // cron job 配置 + // 支持 标准格式 + // 也支持 @every {duration} 等语义化格式 Cron string `flag:",omitempty"` schedule cron.Schedule diff --git a/pkg/cron/zz_generated.runtimedoc.go b/pkg/cron/zz_generated.runtimedoc.go index 9f93aee..ae1bb14 100644 --- a/pkg/cron/zz_generated.runtimedoc.go +++ b/pkg/cron/zz_generated.runtimedoc.go @@ -31,7 +31,11 @@ func (v Job) RuntimeDoc(names ...string) ([]string, bool) { if len(names) > 0 { switch names[0] { case "Cron": - return []string{}, true + return []string{ + "cron job 配置", + "支持 标准格式", + "也支持 @every {duration} 等语义化格式", + }, true }