From ca7118223a884ecd2b7f13903d76ff6cc42a0c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Wed, 12 Jun 2024 17:48:54 +0200 Subject: [PATCH] Follow cog's API changes --- codegen/jennies/gotypes.go | 39 +++++++++++++++++++++++------------ codegen/jennies/typescript.go | 10 ++++----- go.mod | 4 ++-- go.sum | 8 +++---- go.work.sum | 6 ++++++ 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/codegen/jennies/gotypes.go b/codegen/jennies/gotypes.go index b450e22c..8e006ae7 100644 --- a/codegen/jennies/gotypes.go +++ b/codegen/jennies/gotypes.go @@ -84,16 +84,15 @@ func (g *GoTypes) generateFiles(version *codegen.KindVersion, name string, machi return g.generateFilesAtDepth(version.Schema, version, 0, machineName, packageName, pathPrefix, namePrefix) } - codegenOpts := []cog.SingleSchemaOption{ - cog.CUEValue(packageName, version.Schema, cog.ForceEnvelope(name)), - cog.GoTypes(), - } + codegenPipeline := cog.TypesFromSchema(). + CUEValue(packageName, version.Schema, cog.ForceEnvelope(name)). + Golang() if g.AddKubernetesCodegen { - codegenOpts = append(codegenOpts, cog.SchemaTransformations(cog.AppendCommentToObjects("+k8s:openapi-gen=true"))) + codegenPipeline = codegenPipeline.SchemaTransformations(cog.AppendCommentToObjects("+k8s:openapi-gen=true")) } - goBytes, err := cog.TypesFromSchema(context.Background(), codegenOpts...) + goBytes, err := codegenPipeline.Run(context.Background()) if err != nil { return nil, err } @@ -155,19 +154,33 @@ type CUEGoConfig struct { NamePrefix string } -// TODO(kgz): what to do with `maxNamingDepth`? func GoTypesFromCUE(v cue.Value, cfg CUEGoConfig, maxNamingDepth int) ([]byte, error) { - codegenOpts := []cog.SingleSchemaOption{ - cog.CUEValue(cfg.PackageName, v, cog.ForceEnvelope(cfg.Name)), - cog.GoTypes(), - cog.SchemaTransformations(cog.PrefixObjectsNames(cfg.NamePrefix)), + nameFunc := func(value cue.Value, path cue.Path) string { + i := 0 + for ; i < len(path.Selectors()) && i < len(v.Path().Selectors()); i++ { + if maxNamingDepth > 0 && i >= maxNamingDepth { + break + } + if !SelEq(path.Selectors()[i], v.Path().Selectors()[i]) { + break + } + } + if i > 0 { + path = cue.MakePath(path.Selectors()[i:]...) + } + return strings.Trim(path.String(), "?#") } + codegenPipeline := cog.TypesFromSchema(). + CUEValue(cfg.PackageName, v, cog.ForceEnvelope(cfg.Name), cog.NameFunc(nameFunc)). + SchemaTransformations(cog.PrefixObjectsNames(cfg.NamePrefix)). + Golang() + if cfg.AddKubernetesOpenAPIGenComment { - codegenOpts = append(codegenOpts, cog.SchemaTransformations(cog.AppendCommentToObjects("+k8s:openapi-gen=true"))) + codegenPipeline = codegenPipeline.SchemaTransformations(cog.AppendCommentToObjects("+k8s:openapi-gen=true")) } - return cog.TypesFromSchema(context.Background(), codegenOpts...) + return codegenPipeline.Run(context.Background()) } // SanitizeLabelString strips characters from a string that are not allowed for diff --git a/codegen/jennies/typescript.go b/codegen/jennies/typescript.go index 7e96fc8c..02902a19 100644 --- a/codegen/jennies/typescript.go +++ b/codegen/jennies/typescript.go @@ -201,10 +201,8 @@ func (j TypeScriptTypes) generateFilesAtDepth(v cue.Value, kv *codegen.KindVersi } func generateTypescriptBytes(v cue.Value, name string) ([]byte, error) { - codegenOpts := []cog.SingleSchemaOption{ - cog.CUEValue("", v, cog.ForceEnvelope(name)), - cog.TypescriptTypes(), - } - - return cog.TypesFromSchema(context.Background(), codegenOpts...) + return cog.TypesFromSchema(). + CUEValue("", v, cog.ForceEnvelope(name)). + Typescript(). + Run(context.Background()) } diff --git a/go.mod b/go.mod index 15140161..9940780c 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( cuelang.org/go v0.8.2 github.com/getkin/kin-openapi v0.123.0 github.com/grafana/codejen v0.0.4-0.20230321061741-77f656893a3d - github.com/grafana/cog v0.0.0-20240603130906-42de25f609a6 + github.com/grafana/cog v0.0.0-20240612153742-9da3256be0c3 github.com/grafana/thema v0.0.0-20230511182720-3146087fcc26 github.com/hashicorp/go-multierror v1.1.1 github.com/matryer/moq v0.3.4 @@ -64,7 +64,7 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect - github.com/huandu/xstrings v1.4.0 // indirect + github.com/huandu/xstrings v1.5.0 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/invopop/yaml v0.3.1 // indirect diff --git a/go.sum b/go.sum index 8aa6fcf6..20abe7bc 100644 --- a/go.sum +++ b/go.sum @@ -137,8 +137,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grafana/codejen v0.0.4-0.20230321061741-77f656893a3d h1:hrXbGJ5jgp6yNITzs5o+zXq0V5yT3siNJ+uM8LGwWKk= github.com/grafana/codejen v0.0.4-0.20230321061741-77f656893a3d/go.mod h1:zmwwM/DRyQB7pfuBjTWII3CWtxcXh8LTwAYGfDfpR6s= -github.com/grafana/cog v0.0.0-20240603130906-42de25f609a6 h1:WWQFmxHrAyYepeRYLmzGb1SsknCT7ZUbhgZ2/4JSzPQ= -github.com/grafana/cog v0.0.0-20240603130906-42de25f609a6/go.mod h1:A+ggqoURW2MB0Y5bxfi7DZOtxKYqwLuugRfMI+sT/0w= +github.com/grafana/cog v0.0.0-20240612153742-9da3256be0c3 h1:+4Mdp5Tu5OwQiX6bJOaYsjYOgk72zWCddZh53kUDmRQ= +github.com/grafana/cog v0.0.0-20240612153742-9da3256be0c3/go.mod h1:ldzzH0ZOWXvgKm4huFoTpXOzWelfIYL3DA5W1PlmACg= github.com/grafana/thema v0.0.0-20230511182720-3146087fcc26 h1:HX927q4X1n451pnGb8U0wq74i8PCzuxVjzv7TyD10kc= github.com/grafana/thema v0.0.0-20230511182720-3146087fcc26/go.mod h1:Pn9nfzCk7nV0mvNgwusgCjCROZP6nm4GpwTnmEhLT24= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= @@ -151,8 +151,8 @@ github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9 github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU= -github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI= +github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/hydrogen18/memlistener v0.0.0-20200120041712-dcc25e7acd91/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= diff --git a/go.work.sum b/go.work.sum index 3472ec2c..8d4d6968 100644 --- a/go.work.sum +++ b/go.work.sum @@ -212,6 +212,7 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grafana/codejen v0.0.3/go.mod h1:zmwwM/DRyQB7pfuBjTWII3CWtxcXh8LTwAYGfDfpR6s= github.com/grafana/cog v0.0.0-20240529130920-083c489f684f/go.mod h1:A+ggqoURW2MB0Y5bxfi7DZOtxKYqwLuugRfMI+sT/0w= +github.com/grafana/cog v0.0.0-20240603130906-42de25f609a6/go.mod h1:A+ggqoURW2MB0Y5bxfi7DZOtxKYqwLuugRfMI+sT/0w= github.com/grafana/cuetsy v0.1.10/go.mod h1:Ix97+CPD8ws9oSSxR3/Lf4ahU1I4Np83kjJmDVnLZvc= github.com/grafana/grafana-plugin-sdk-go v0.222.0/go.mod h1:0lYONo3rJrjIoJNOQP0h2NXmOaF5rFIuH/rwSVSeIrQ= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= @@ -231,6 +232,7 @@ github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/jszwedko/go-datemath v0.1.1-0.20230526204004-640a500621d6/go.mod h1:WrYiIuiXUMIvTDAQw97C+9l0CnBmCcvosPjN3XDqS/o= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE= @@ -349,6 +351,7 @@ go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -356,6 +359,7 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= @@ -391,6 +395,7 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= +golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= @@ -403,6 +408,7 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= google.golang.org/api v0.164.0/go.mod h1:2OatzO7ZDQsoS7IFf3rvsE17/TldiU3F/zxFHeqUB5o=