Skip to content

Commit

Permalink
fix: concat comments in nest
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Sep 6, 2024
1 parent 3234809 commit 3224fed
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
18 changes: 11 additions & 7 deletions cmd/example/apis/org/zz_generated.enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand All @@ -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)
}
}

Expand All @@ -80,7 +84,7 @@ func (v Type) Label() string {
return "企事业单位"

default:
return "UNKNOWN"
return fmt.Sprint(v)
}
}

Expand Down
11 changes: 0 additions & 11 deletions cmd/example/apis/org/zz_generated.operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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{}))
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"github.com/innoai-tech/infra/pkg/cli"

_ "github.com/innoai-tech/infra/pkg/cron"
)

var App = cli.NewApp(
Expand Down
4 changes: 3 additions & 1 deletion internal/otel/zz_generated.enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ DON'T EDIT THIS FILE
package otel

import (
fmt "fmt"

github_com_pkg_errors "github.com/pkg/errors"
)

Expand Down Expand Up @@ -43,6 +45,6 @@ func (v LogLevel) Label() string {
return "warn"

default:
return "UNKNOWN"
return fmt.Sprint(v)
}
}
38 changes: 27 additions & 11 deletions pkg/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pkg/cli/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cron/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pkg/cron/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand Down

0 comments on commit 3224fed

Please sign in to comment.