Skip to content

Commit

Permalink
feat: merge new codes from origin
Browse files Browse the repository at this point in the history
fix: swagger doc gen
  • Loading branch information
suyuan32 committed Sep 13, 2022
2 parents b8a0d5d + 3e96994 commit d1ab664
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 90 deletions.
6 changes: 4 additions & 2 deletions core/stores/cache/cachenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"sync"
"time"
Expand Down Expand Up @@ -130,7 +131,7 @@ func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interfa
return err
}

return c.rds.SetexCtx(ctx, key, string(data), int(expire.Seconds()))
return c.rds.SetexCtx(ctx, key, string(data), int(math.Ceil(expire.Seconds())))
}

// String returns a string that represents the cacheNode.
Expand Down Expand Up @@ -275,5 +276,6 @@ func (c cacheNode) processCache(ctx context.Context, key, data string, v interfa
}

func (c cacheNode) setCacheWithNotFound(ctx context.Context, key string) error {
return c.rds.SetexCtx(ctx, key, notFoundPlaceholder, int(c.aroundDuration(c.notFoundExpiry).Seconds()))
seconds := int(math.Ceil(c.aroundDuration(c.notFoundExpiry).Seconds()))
return c.rds.SetexCtx(ctx, key, notFoundPlaceholder, seconds)
}
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -465,6 +468,7 @@ github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9F
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -568,6 +572,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 h1:k/gmLsJDWwWqbLCur2yWnJzwQEKRcAHXo6seXGuSwWw=
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.etcd.io/etcd/api/v3 v3.5.4 h1:OHVyt3TopwtUQ2GKdd5wu3PmmipR4FTwCqoEjSyRdIc=
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4 h1:lrneYvz923dvC14R54XcA7FXoZ3mlGZAgmwhfm7HqOg=
Expand Down Expand Up @@ -606,6 +611,9 @@ go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
Expand Down
3 changes: 3 additions & 0 deletions readme-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ go-zero 已被许多公司用于生产部署,接入场景如在线教育、电
>75. 叮当跳动
>76. Keep
>77. simba innovation
>78. ZeroCMF
>79. 安徽寻梦投资发展集团
>80. 广州腾思信息科技有限公司

如果贵公司也已使用 go-zero,欢迎在 [登记地址](https://github.com/zeromicro/go-zero/issues/602) 登记,仅仅为了推广,不做其它用途。

Expand Down
103 changes: 90 additions & 13 deletions tools/goctl/api/docgen/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"bytes"
_ "embed"
"fmt"
"go/format"
"html/template"
"io"
"strconv"
"strings"

"github.com/zeromicro/go-zero/core/stringx"
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/api/util"
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
"github.com/zeromicro/go-zero/tools/goctl/util"
)

//go:embed markdown.tpl
Expand All @@ -23,7 +23,7 @@ func genDoc(api *spec.ApiSpec, dir, filename string) error {
return nil
}

fp, _, err := util.MaybeCreateFile(dir, "", filename)
fp, _, err := apiutil.MaybeCreateFile(dir, "", filename)
if err != nil {
return err
}
Expand All @@ -36,12 +36,12 @@ func genDoc(api *spec.ApiSpec, dir, filename string) error {
routeComment = "N/A"
}

requestContent, err := buildDoc(route.RequestType, api)
requestContent, err := buildDoc(route.RequestType, api.Types)
if err != nil {
return err
}

responseContent, err := buildDoc(route.ResponseType, api)
responseContent, err := buildDoc(route.ResponseType, api.Types)
if err != nil {
return err
}
Expand All @@ -61,14 +61,15 @@ func genDoc(api *spec.ApiSpec, dir, filename string) error {
if err != nil {
return err
}

builder.Write(tmplBytes.Bytes())
}

_, err = fp.WriteString(strings.Replace(builder.String(), """, `"`, -1))
return err
}

func buildDoc(route spec.Type, api *spec.ApiSpec) (string, error) {
func buildDoc(route spec.Type, types []spec.Type) (string, error) {
if route == nil || len(route.Name()) == 0 {
return "", nil
}
Expand All @@ -78,15 +79,12 @@ func buildDoc(route spec.Type, api *spec.ApiSpec) (string, error) {
if definedType, ok := route.(spec.DefineStruct); ok {
associatedTypes(definedType, &tps)
}
value, err := gogen.BuildTypes(tps, api)
value, err := buildTypes(tps, types)
if err != nil {
return "", err
}
formatted, err := format.Source([]byte(value))
if err != nil {
return "", err
}
return fmt.Sprintf("\n\n```golang\n%s\n```\n", string(formatted)), nil

return fmt.Sprintf("\n\n```golang\n%s\n```\n", value), nil
}

func associatedTypes(tp spec.DefineStruct, tps *[]spec.Type) {
Expand All @@ -107,3 +105,82 @@ func associatedTypes(tp spec.DefineStruct, tps *[]spec.Type) {
}
}
}

// buildTypes gen types to string
func buildTypes(types []spec.Type, all []spec.Type) (string, error) {
var builder strings.Builder
first := true
for _, tp := range types {
if first {
first = false
} else {
builder.WriteString("\n\n")
}
if err := writeType(&builder, tp, all); err != nil {
return "", apiutil.WrapErr(err, "Type "+tp.Name()+" generate error")
}
}

return builder.String(), nil
}

func writeType(writer io.Writer, tp spec.Type, all []spec.Type) error {
fmt.Fprintf(writer, "type %s struct {\n", util.Title(tp.Name()))
if err := writerMembers(writer, tp, all); err != nil {
return err
}
fmt.Fprintf(writer, "}")
return nil
}

func writerMembers(writer io.Writer, tp spec.Type, all []spec.Type) error {
structType, ok := tp.(spec.DefineStruct)
if !ok {
return fmt.Errorf("unspport struct type: %s", tp.Name())
}

getTargetType := func(tp string) spec.Type {
for _, v := range all {
if v.Name() == tp {
return v
}
}
return nil
}
for _, member := range structType.Members {
if member.IsInline {
inlineType := getTargetType(member.Type.Name())
if inlineType == nil {
if _, err := fmt.Fprintf(writer, "%s\n", strings.Title(member.Type.Name())); err != nil {
return err
}
} else {
if err := writerMembers(writer, inlineType, all); err != nil {
return err
}
}

continue
}

if err := writeProperty(writer, member.Name, member.Tag, member.GetComment(), member.Type, 1); err != nil {
return err
}
}

return nil
}

func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
apiutil.WriteIndent(writer, indent)
var err error
if len(comment) > 0 {
comment = strings.TrimPrefix(comment, "//")
comment = "//" + comment
_, err = fmt.Fprintf(writer, "%s %s %s %s\n", strings.Title(name), tp.Name(), tag, comment)
} else {
_, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp.Name(), tag)
}

return err
}
18 changes: 13 additions & 5 deletions tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const typesFile = "types"
var typesTemplate string

// BuildTypes gen types to string
func BuildTypes(types []spec.Type, api *spec.ApiSpec) (string, error) {
func BuildTypes(types []spec.Type) (string, error) {
var builder strings.Builder
first := true
for _, tp := range types {
Expand All @@ -30,7 +30,7 @@ func BuildTypes(types []spec.Type, api *spec.ApiSpec) (string, error) {
} else {
builder.WriteString("\n\n")
}
if err := writeType(&builder, tp, api); err != nil {
if err := writeType(&builder, tp); err != nil {
return "", apiutil.WrapErr(err, "Type "+tp.Name()+" generate error")
}
}
Expand All @@ -39,7 +39,7 @@ func BuildTypes(types []spec.Type, api *spec.ApiSpec) (string, error) {
}

func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
val, err := BuildTypes(api.Types, api)
val, err := BuildTypes(api.Types)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
})
}

func writeType(writer io.Writer, tp spec.Type, api *spec.ApiSpec) error {
func writeType(writer io.Writer, tp spec.Type) error {
structType, ok := tp.(spec.DefineStruct)
if !ok {
return fmt.Errorf("unspport struct type: %s", tp.Name())
Expand All @@ -81,7 +81,15 @@ func writeType(writer io.Writer, tp spec.Type, api *spec.ApiSpec) error {

fmt.Fprintf(writer, "type %s struct {\n", util.Title(tp.Name()))
for _, member := range structType.Members {
if err := writeProperty(writer, member.Name, member.Tag, member.GetComment(), member.Type, 1, api); err != nil {
if member.IsInline {
if _, err := fmt.Fprintf(writer, "%s\n", strings.Title(member.Type.Name())); err != nil {
return err
}

continue
}

if err := writeProperty(writer, member.Name, member.Tag, member.GetComment(), member.Type, member.Docs, 1); err != nil {
return err
}
}
Expand Down
3 changes: 0 additions & 3 deletions tools/goctl/api/gogen/testdata/api_common.api

This file was deleted.

48 changes: 0 additions & 48 deletions tools/goctl/api/gogen/testdata/api_has_nested_type.api

This file was deleted.

16 changes: 2 additions & 14 deletions tools/goctl/api/gogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func genFile(c fileGenConfig) error {
return err
}

func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int, api *spec.ApiSpec) error {
func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, doc spec.Doc, indent int) error {
// write doc for swagger
for _, v := range tp.Documents() {
for _, v := range doc {
fmt.Fprintf(writer, "\t%s\n", v)
}

Expand Down Expand Up @@ -186,15 +186,3 @@ func golangExpr(ty spec.Type, pkg ...string) string {

return ""
}

func isCustomType(t string) bool {
var builtinType = []string{"string", "bool", "int", "uint", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float32", "float64", "uintptr", "complex64", "complex128"}
var is bool = true
for _, v := range builtinType {
if t == v {
is = false
break
}
}
return is
}
2 changes: 2 additions & 0 deletions tools/goctl/kube/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
varIntRevisions int
varIntPort int
varIntNodePort int
varIntTargetPort int
varIntMinReplicas int
varIntMaxReplicas int
varStringHome string
Expand Down Expand Up @@ -51,6 +52,7 @@ func init() {
deployCmd.Flags().IntVar(&varIntRevisions, "revisions", 5, "The number of revision history to limit")
deployCmd.Flags().IntVar(&varIntPort, "port", 0, "The port of the deployment to listen on pod (required)")
deployCmd.Flags().IntVar(&varIntNodePort, "nodePort", 0, "The nodePort of the deployment to expose")
deployCmd.Flags().IntVar(&varIntTargetPort, "targetPort", 0, "The targetPort of the deployment, default to port")
deployCmd.Flags().IntVar(&varIntMinReplicas, "minReplicas", 3, "The min replicas to deploy")
deployCmd.Flags().IntVar(&varIntMaxReplicas, "maxReplicas", 10, "The max replicas to deploy")
deployCmd.Flags().StringVar(&varStringImagePullPolicy, "imagePullPolicy", "", "Image pull policy. One of Always, Never, IfNotPresent")
Expand Down
Loading

0 comments on commit d1ab664

Please sign in to comment.