From 67e1910e462ff6496db7384fbf152cd81fc77c66 Mon Sep 17 00:00:00 2001 From: sam boyer Date: Mon, 13 Mar 2023 23:53:36 -0400 Subject: [PATCH 1/9] gen: Introduce generic import mapping --- analyze.go | 31 +++++++++++++++++ generate_test.go | 3 ++ generator.go | 88 +++++++++++++++++++++++++++++++++++++++------- go.mod | 9 +++-- go.sum | 90 ++++++++++++++++-------------------------------- 5 files changed, 143 insertions(+), 78 deletions(-) diff --git a/analyze.go b/analyze.go index a69800f..01c621d 100644 --- a/analyze.go +++ b/analyze.go @@ -5,6 +5,7 @@ import ( "fmt" "cuelang.org/go/cue" + "cuelang.org/go/cue/build" "cuelang.org/go/cue/format" ) @@ -12,7 +13,37 @@ func tpv(v cue.Value) { fmt.Printf("%s:\n%s\n", v.Path(), exprTree(v)) } +func findInstance(raw cue.Value) *build.Instance { + // Search, recursively, for cue.Value.BuildInstance() on: + // - the provided raw node + // - the first direct conjunct of the raw node, if any + // - the ReferencePath() result, if different, of either of the above + + bi := raw.BuildInstance() + if bi != nil { + return bi + } + + if ref, path := raw.ReferencePath(); len(path.Selectors()) != 0 && ref != raw { + if bi = findInstance(ref); bi != nil { + return bi + } + } + + // No instance on anything reachable from the value itself. Try again with any + // immediate conjuncts. + if op, dvals := raw.Expr(); op == cue.AndOp { + // Only try the first value, which will represent the package. Additional values + // will be constraints specified on the whole package instance. + bi = findInstance(dvals[0]) + } + + return bi +} + func isReference(v cue.Value) bool { + // return cue.Dereference(v) == v + // FIXME the below impl is just wrong, but current behavior somehow relies on it. Above is correct, it's a pointer comparison _, path := v.ReferencePath() return len(path.Selectors()) > 0 } diff --git a/generate_test.go b/generate_test.go index 16a3737..f723566 100644 --- a/generate_test.go +++ b/generate_test.go @@ -62,6 +62,9 @@ func TestGenerateWithImports(t *testing.T) { b, err := cuetsy.Generate(v, cuetsy.Config{ Export: true, + ImportMapper: func(string) (string, error) { + return "", nil + }, }) if err != nil { errors.Print(t, err, nil) diff --git a/generator.go b/generator.go index dab754a..cb2b1b0 100644 --- a/generator.go +++ b/generator.go @@ -51,24 +51,35 @@ var allKinds = [...]TSType{ TypeEnum, } -// An ImportMapper takes an ImportDecl and returns a string indicating the -// import statement that should be used in the corresponding typescript, or -// an error if no mapping can be made. -type ImportMapper func(*ast.ImportDecl) (string, error) - -// NoImportMappingErr returns a standard error indicating that no mapping can be -// made for the provided import statement. -func NoImportMappingErr(d *ast.ImportDecl) error { - return errors.Newf(d.Pos(), "a corresponding typescript import is not available for %q", d.Import.String()) +// An ImportMapper takes a string containing a CUE import path (e.g. +// "github.com/grafana/cuetsy") and returns a string indicating the import path +// that should be used in the corresponding generated typescript, or an error if +// no mapping can be made. +// +// An empty string return indicates no TS import statements +// should be generated for that CUE import path. +type ImportMapper func(string) (string, error) + +func nilImportMapper(path string) (string, error) { + return "", fmt.Errorf("a corresponding typescript import is not available for %q", path) } -func nilImportMapper(d *ast.ImportDecl) (string, error) { return "", NoImportMappingErr(d) } +// IgnoreImportMapper ignores all import paths cuetsy encounters, resulting in no +// import statements in generated TS output. +func IgnoreImportMapper(path string) (string, error) { + return "", nil +} // Config governs certain variable behaviors when converting CUE to Typescript. type Config struct { - // ImportMapper determines how CUE imports are mapped to Typescript imports. - // If nil, any non-stdlib import in the CUE source will result in a fatal + // ImportMapper determines how CUE imports are mapped to Typescript imports. If + // nil, any non-stdlib import in the input CUE source will result in a fatal // error. + // + // Import conversions are only run if the input [cue.Value] or its top-level + // conjuncts if [cue.Value.Source] returns an [*ast.File]. This eliminates + // computed values, and values representing nodes other than a root file + // node. ImportMapper // Export determines whether generated TypeScript symbols are exported. @@ -101,6 +112,14 @@ func GenerateAST(val cue.Value, c Config) (*ts.File, error) { val: &val, } + var file ts.File + var err error + + file.Imports, err = mapImports(val, c.ImportMapper) + if err != nil { + return nil, err + } + iter, err := val.Fields( cue.Definitions(true), cue.Optional(true), @@ -109,7 +128,6 @@ func GenerateAST(val cue.Value, c Config) (*ts.File, error) { return nil, err } - var file ts.File for iter.Next() { n := g.decl(iter.Selector().String(), iter.Value()) file.Nodes = append(file.Nodes, n...) @@ -136,6 +154,50 @@ func GenerateSingleAST(name string, v cue.Value, t TSType) (*DeclPair, error) { } } +// mapImports converts CUE import statements, represented in their AST form, +// to the corresponding TS import, if the CUE import is allowed. +// +// Some CUE imports are allowed but have no corresponding TS import - the CUE +// types from that package are expected to be inlined. +func mapImports(raw cue.Value, fn ImportMapper) ([]tsast.ImportSpec, error) { + bi := findInstance(raw) + if bi == nil { + return nil, nil + } + + var ims []*ast.ImportSpec + for _, src := range bi.Files { + ims = append(ims, src.Imports...) + } + + var specs []tsast.ImportSpec + for _, im := range ims { + pkg, err := fn(strings.Trim(im.Path.Value, "\"")) + if err != nil || pkg == "" { + // Empty string mapping means skip it + return nil, err + } + + tsim := tsast.ImportSpec{ + From: tsast.Str{Value: pkg}, + } + + if im.Name != nil && im.Name.String() != "" { + tsim.AsName = im.Name.String() + } else { + sl := strings.Split(strings.Trim(im.Path.Value, "\""), "/") + final := sl[len(sl)-1] + if idx := strings.Index(final, ":"); idx != -1 { + tsim.AsName = final[idx:] + } else { + tsim.AsName = final + } + } + specs = append(specs, tsim) + } + return specs, nil +} + // DeclPair represents a generated type declaration, with its corresponding default declaration. type DeclPair struct { // The generated type declaration. diff --git a/go.mod b/go.mod index 2423f17..4929463 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,13 @@ module github.com/grafana/cuetsy go 1.16 require ( - cuelang.org/go v0.4.3 - github.com/google/go-cmp v0.5.6 + cuelang.org/go v0.5.0-beta.5 + github.com/google/go-cmp v0.5.8 github.com/kr/text v0.2.0 github.com/matryer/is v1.4.0 - github.com/rogpeppe/go-internal v1.8.1 + github.com/rogpeppe/go-internal v1.9.0 github.com/stretchr/testify v1.7.1 // indirect github.com/xlab/treeprint v1.1.0 - golang.org/x/tools v0.1.10 - golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect + golang.org/x/tools v0.1.12 gotest.tools v2.2.0+incompatible ) diff --git a/go.sum b/go.sum index c12e388..c0fd57c 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,19 @@ -cuelang.org/go v0.4.3 h1:W3oBBjDTm7+IZfCKZAmC8uDG0eYfJL4Pp/xbbCMKaVo= -cuelang.org/go v0.4.3/go.mod h1:7805vR9H+VoBNdWFdI7jyDR3QLUPp4+naHfbcgp55HI= -dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= -github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= -github.com/cockroachdb/apd/v2 v2.0.1 h1:y1Rh3tEU89D+7Tgbw+lp52T6p/GJLpDmNvr10UWqLTE= -github.com/cockroachdb/apd/v2 v2.0.1/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= +cuelang.org/go v0.5.0-beta.5 h1:TAV4ZjXw2M6xf6jI8XyAAXCqWJ82Y0oxhlf9w3l544A= +cuelang.org/go v0.5.0-beta.5/go.mod h1:okjJBHFQFer+a41sAe2SaGm1glWS8oEb6CmJvn5Zdws= +github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E= +github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/emicklei/proto v1.6.15 h1:XbpwxmuOPrdES97FrSfpyy67SSCV/wBIKXqgJzh6hNw= -github.com/emicklei/proto v1.6.15/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw= +github.com/emicklei/proto v1.10.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -35,6 +30,8 @@ github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto= github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -43,10 +40,10 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0= -github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc/go.mod h1:KbKfKPy2I6ecOIGA9apfheFv14+P3RSmmQvshofQyMY= -github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= -github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= +github.com/protocolbuffers/txtpbfmt v0.0.0-20220428173112-74888fd59c2b h1:zd/2RNzIRkoGGMjE+YIsZ85CnDIz672JK2F3Zl4vux4= +github.com/protocolbuffers/txtpbfmt v0.0.0-20220428173112-74888fd59c2b/go.mod h1:KjY0wibdYKc4DYkerHSbguaf3JeIPGhNJBp2BNiFH78= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= @@ -57,68 +54,41 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk= github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= -golang.org/x/exp v0.0.0-20210126221216-84987778548c/go.mod h1:I6l2HNBLBZEcrOoCpyKLdY2lHoRZ8lI4x60KMCQDft4= -golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= -golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= -golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.6.0-dev.0.20220818022119-ed83ed61efb9/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200612220849-54c614fe050c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20= -golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= -golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= From e214aa2d4c2f30801a4f0153ad090997501f68f6 Mon Sep 17 00:00:00 2001 From: sam boyer Date: Tue, 14 Mar 2023 06:53:03 -0400 Subject: [PATCH 2/9] Bump go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 4929463..a79025f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/grafana/cuetsy -go 1.16 +go 1.19 require ( cuelang.org/go v0.5.0-beta.5 From 722eae84a74c9ed67a9074b0b320bee1ec7860e3 Mon Sep 17 00:00:00 2001 From: sam boyer Date: Tue, 14 Mar 2023 06:55:33 -0400 Subject: [PATCH 3/9] Split import mapping funcs into new file --- generator.go | 63 ----------------------------------------- import_mapper.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 import_mapper.go diff --git a/generator.go b/generator.go index cb2b1b0..92a54e2 100644 --- a/generator.go +++ b/generator.go @@ -51,25 +51,6 @@ var allKinds = [...]TSType{ TypeEnum, } -// An ImportMapper takes a string containing a CUE import path (e.g. -// "github.com/grafana/cuetsy") and returns a string indicating the import path -// that should be used in the corresponding generated typescript, or an error if -// no mapping can be made. -// -// An empty string return indicates no TS import statements -// should be generated for that CUE import path. -type ImportMapper func(string) (string, error) - -func nilImportMapper(path string) (string, error) { - return "", fmt.Errorf("a corresponding typescript import is not available for %q", path) -} - -// IgnoreImportMapper ignores all import paths cuetsy encounters, resulting in no -// import statements in generated TS output. -func IgnoreImportMapper(path string) (string, error) { - return "", nil -} - // Config governs certain variable behaviors when converting CUE to Typescript. type Config struct { // ImportMapper determines how CUE imports are mapped to Typescript imports. If @@ -154,50 +135,6 @@ func GenerateSingleAST(name string, v cue.Value, t TSType) (*DeclPair, error) { } } -// mapImports converts CUE import statements, represented in their AST form, -// to the corresponding TS import, if the CUE import is allowed. -// -// Some CUE imports are allowed but have no corresponding TS import - the CUE -// types from that package are expected to be inlined. -func mapImports(raw cue.Value, fn ImportMapper) ([]tsast.ImportSpec, error) { - bi := findInstance(raw) - if bi == nil { - return nil, nil - } - - var ims []*ast.ImportSpec - for _, src := range bi.Files { - ims = append(ims, src.Imports...) - } - - var specs []tsast.ImportSpec - for _, im := range ims { - pkg, err := fn(strings.Trim(im.Path.Value, "\"")) - if err != nil || pkg == "" { - // Empty string mapping means skip it - return nil, err - } - - tsim := tsast.ImportSpec{ - From: tsast.Str{Value: pkg}, - } - - if im.Name != nil && im.Name.String() != "" { - tsim.AsName = im.Name.String() - } else { - sl := strings.Split(strings.Trim(im.Path.Value, "\""), "/") - final := sl[len(sl)-1] - if idx := strings.Index(final, ":"); idx != -1 { - tsim.AsName = final[idx:] - } else { - tsim.AsName = final - } - } - specs = append(specs, tsim) - } - return specs, nil -} - // DeclPair represents a generated type declaration, with its corresponding default declaration. type DeclPair struct { // The generated type declaration. diff --git a/import_mapper.go b/import_mapper.go new file mode 100644 index 0000000..5bf668f --- /dev/null +++ b/import_mapper.go @@ -0,0 +1,73 @@ +package cuetsy + +import ( + "fmt" + "strings" + + "cuelang.org/go/cue" + "cuelang.org/go/cue/ast" + tsast "github.com/grafana/cuetsy/ts/ast" +) + +// An ImportMapper takes a string containing a CUE import path (e.g. +// "github.com/grafana/cuetsy") and returns a string indicating the import path +// that should be used in the corresponding generated typescript, or an error if +// no mapping can be made. +// +// An empty string return indicates no TS import statements +// should be generated for that CUE import path. +type ImportMapper func(string) (string, error) + +func nilImportMapper(path string) (string, error) { + return "", fmt.Errorf("a corresponding typescript import is not available for %q", path) +} + +// IgnoreImportMapper ignores all import paths cuetsy encounters, resulting in no +// import statements in generated TS output. +func IgnoreImportMapper(path string) (string, error) { + return "", nil +} + +// mapImports converts CUE import statements, represented in their AST form, +// to the corresponding TS import, if the CUE import is allowed. +// +// Some CUE imports are allowed but have no corresponding TS import - the CUE +// types from that package are expected to be inlined. +func mapImports(raw cue.Value, fn ImportMapper) ([]tsast.ImportSpec, error) { + bi := findInstance(raw) + if bi == nil { + return nil, nil + } + + var ims []*ast.ImportSpec + for _, src := range bi.Files { + ims = append(ims, src.Imports...) + } + + var specs []tsast.ImportSpec + for _, im := range ims { + pkg, err := fn(strings.Trim(im.Path.Value, "\"")) + if err != nil || pkg == "" { + // Empty string mapping means skip it + return nil, err + } + + tsim := tsast.ImportSpec{ + From: tsast.Str{Value: pkg}, + } + + if im.Name != nil && im.Name.String() != "" { + tsim.AsName = im.Name.String() + } else { + sl := strings.Split(strings.Trim(im.Path.Value, "\""), "/") + final := sl[len(sl)-1] + if idx := strings.Index(final, ":"); idx != -1 { + tsim.AsName = final[idx:] + } else { + tsim.AsName = final + } + } + specs = append(specs, tsim) + } + return specs, nil +} From f3dde4c12ae11392ac3f945ab56d2433b5b4f730 Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 14 Mar 2023 12:05:04 +0100 Subject: [PATCH 4/9] Update go.mod --- go.mod | 16 +++++++++++++++- go.sum | 37 ------------------------------------- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index a79025f..b8a9fdb 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,22 @@ require ( github.com/kr/text v0.2.0 github.com/matryer/is v1.4.0 github.com/rogpeppe/go-internal v1.9.0 - github.com/stretchr/testify v1.7.1 // indirect github.com/xlab/treeprint v1.1.0 golang.org/x/tools v0.1.12 gotest.tools v2.2.0+incompatible ) + +require ( + github.com/cockroachdb/apd/v2 v2.0.2 // indirect + github.com/emicklei/proto v1.10.0 // indirect + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect + github.com/google/uuid v1.2.0 // indirect + github.com/mitchellh/go-wordwrap v1.0.1 // indirect + github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect + github.com/pkg/errors v0.8.1 // indirect + github.com/protocolbuffers/txtpbfmt v0.0.0-20220428173112-74888fd59c2b // indirect + github.com/stretchr/testify v1.7.1 // indirect + golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/text v0.3.8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index c0fd57c..504cc1d 100644 --- a/go.sum +++ b/go.sum @@ -2,11 +2,9 @@ cuelang.org/go v0.5.0-beta.5 h1:TAV4ZjXw2M6xf6jI8XyAAXCqWJ82Y0oxhlf9w3l544A= cuelang.org/go v0.5.0-beta.5/go.mod h1:okjJBHFQFer+a41sAe2SaGm1glWS8oEb6CmJvn5Zdws= github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E= github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= -github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw= github.com/emicklei/proto v1.10.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= @@ -14,27 +12,20 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -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/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= -github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto= github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -44,49 +35,21 @@ github.com/protocolbuffers/txtpbfmt v0.0.0-20220428173112-74888fd59c2b h1:zd/2RN github.com/protocolbuffers/txtpbfmt v0.0.0-20220428173112-74888fd59c2b/go.mod h1:KjY0wibdYKc4DYkerHSbguaf3JeIPGhNJBp2BNiFH78= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk= github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.6.0-dev.0.20220818022119-ed83ed61efb9/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 319788721b5d63aaf6bce76f66f84388256404f3 Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 14 Mar 2023 12:19:51 +0100 Subject: [PATCH 5/9] Update drone to 1.19 --- .drone/drone.cue | 2 +- .drone/drone.yml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone/drone.cue b/.drone/drone.cue index 28a4330..b092947 100644 --- a/.drone/drone.cue +++ b/.drone/drone.cue @@ -4,7 +4,7 @@ name: "default" #Step: { name: string, commands: [string], - image: string | *"golang:1.16", + image: string | *"golang:1.19", volumes: [{name: "gopath", path: "/go"}], ... } diff --git a/.drone/drone.yml b/.drone/drone.yml index eff7148..3cd16aa 100644 --- a/.drone/drone.yml +++ b/.drone/drone.yml @@ -8,7 +8,7 @@ platform: steps: - name: download - image: golang:1.16 + image: golang:1.19 commands: - go mod download volumes: @@ -16,7 +16,7 @@ steps: path: /go - name: lint - image: golang:1.16 + image: golang:1.19 commands: - make lint volumes: @@ -26,7 +26,7 @@ steps: - download - name: test - image: golang:1.16 + image: golang:1.19 commands: - make test volumes: From 406f8ef1a393131dd92f059db010d03053fa64ef Mon Sep 17 00:00:00 2001 From: spinillos Date: Tue, 14 Mar 2023 12:52:46 +0100 Subject: [PATCH 6/9] Update signature --- .drone/drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone/drone.yml b/.drone/drone.yml index 3cd16aa..4d9b1ff 100644 --- a/.drone/drone.yml +++ b/.drone/drone.yml @@ -37,6 +37,6 @@ steps: --- kind: signature -hmac: 27e9124f010ea02719fed325246799697d827ceb9dcf7d742fb30fb45372b728 +hmac: 3da164ae6427461c772a7df77a7be50f79b255bfe61821440492c01aa1333670 ... From 6f2ce596acf9b9e32f740d887f7d0c8e9a469601 Mon Sep 17 00:00:00 2001 From: spinillos Date: Thu, 20 Apr 2023 11:42:11 +0200 Subject: [PATCH 7/9] Modify txtar tests to add configurable import mappers --- generate_test.go | 14 +++++++++---- internal/cuetxtar/cuetxtar.go | 12 +++++++++++ testdata/imports/imports.txtar | 37 ++++++++++++++++++++++++++++++++++ ts/ast/ast.go | 6 +++++- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 testdata/imports/imports.txtar diff --git a/generate_test.go b/generate_test.go index de9f1d3..abb1887 100644 --- a/generate_test.go +++ b/generate_test.go @@ -46,6 +46,14 @@ func TestGenerateWithImports(t *testing.T) { ToDo: map[string]string{ "imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case", }, + ImportMappers: map[string]func(string) (string, error){ + "imports/imports": func(s string) (string, error) { + if s == "example.com/dep" { + return "new", nil + } + return s, nil + }, + }, } ctx := cuecontext.New() @@ -57,10 +65,8 @@ func TestGenerateWithImports(t *testing.T) { } b, err := cuetsy.Generate(v, cuetsy.Config{ - Export: true, - ImportMapper: func(string) (string, error) { - return "", nil - }, + Export: true, + ImportMapper: t.ImportMapperFn, }) if err != nil { errors.Print(t, err, nil) diff --git a/internal/cuetxtar/cuetxtar.go b/internal/cuetxtar/cuetxtar.go index 0325bc9..1a2abeb 100644 --- a/internal/cuetxtar/cuetxtar.go +++ b/internal/cuetxtar/cuetxtar.go @@ -59,6 +59,8 @@ type TxTarTest struct { // ToDo is a map of tests that should be skipped now, but should be fixed. ToDo map[string]string + + ImportMappers map[string]func(string) (string, error) } // A Test represents a single test based on a .txtar file. @@ -83,6 +85,8 @@ type Test struct { Dir string hasGold bool + + ImportMapperFn func(s string) (string, error) } func (t *Test) Write(b []byte) (n int, err error) { @@ -288,6 +292,14 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) { t.Skip(msg) } + if im, ok := x.ImportMappers[testName]; ok { + tc.ImportMapperFn = im + } else { + tc.ImportMapperFn = func(s string) (string, error) { + return "", nil + } + } + f(tc) update := false diff --git a/testdata/imports/imports.txtar b/testdata/imports/imports.txtar new file mode 100644 index 0000000..db0efa6 --- /dev/null +++ b/testdata/imports/imports.txtar @@ -0,0 +1,37 @@ +-- cue.mod/module.cue -- +module: "example.com" + +-- in.cue -- +package test + +import "example.com/dep" +import "example.com/other" + +#Foo: { + x: dep.#Test + y: other.#Test +} @cuetsy(kind="interface") + +-- dep/file.cue -- +package dep + +#Test: { + a: string +} @cuetsy(kind="interface") + +-- other/file.cue -- +package other + +#Test: { + a: string +} @cuetsy(kind="interface") + +-- out/gen -- + +import * as dep from 'new'; +import * as other from 'example.com/other'; + +export interface Foo { + x: dep.Test; + y: other.Test; +} diff --git a/ts/ast/ast.go b/ts/ast/ast.go index 619971f..d0f61d7 100644 --- a/ts/ast/ast.go +++ b/ts/ast/ast.go @@ -49,7 +49,11 @@ func (f File) String() string { for _, i := range f.Imports { b.WriteString(formatInner(EOLNone, 0, i)) - b.WriteString("\n\n") + b.WriteString("\n") + } + + if len(f.Imports) > 0 { + b.WriteString("\n") } for i, n := range f.Nodes { From ea7b68c830d37e1591762c52fa80b83d64e688c0 Mon Sep 17 00:00:00 2001 From: spinillos Date: Thu, 20 Apr 2023 13:22:37 +0200 Subject: [PATCH 8/9] Add alias test and import from different file and same package --- generate_test.go | 2 +- testdata/imports/imports.txtar | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/generate_test.go b/generate_test.go index abb1887..2185108 100644 --- a/generate_test.go +++ b/generate_test.go @@ -49,7 +49,7 @@ func TestGenerateWithImports(t *testing.T) { ImportMappers: map[string]func(string) (string, error){ "imports/imports": func(s string) (string, error) { if s == "example.com/dep" { - return "new", nil + return "@example/deps", nil } return s, nil }, diff --git a/testdata/imports/imports.txtar b/testdata/imports/imports.txtar index db0efa6..50c7e11 100644 --- a/testdata/imports/imports.txtar +++ b/testdata/imports/imports.txtar @@ -4,12 +4,17 @@ module: "example.com" -- in.cue -- package test -import "example.com/dep" -import "example.com/other" +import ( + "example.com/dep" + "example.com/other" + ui "example.com/alias" +) #Foo: { - x: dep.#Test + w: dep.#Test + x: dep.#Test2 y: other.#Test + z: ui.#Test } @cuetsy(kind="interface") -- dep/file.cue -- @@ -19,6 +24,13 @@ package dep a: string } @cuetsy(kind="interface") +-- dep/file2.cue -- +package dep + +#Test2: { + a: string +} @cuetsy(kind="interface") + -- other/file.cue -- package other @@ -26,12 +38,22 @@ package other a: string } @cuetsy(kind="interface") +-- alias/file.cue -- +package alias + +#Test: { + a: string +} @cuetsy(kind="interface") + -- out/gen -- -import * as dep from 'new'; +import * as dep from '@example/deps'; import * as other from 'example.com/other'; +import * as ui from 'example.com/alias'; export interface Foo { - x: dep.Test; + w: dep.Test; + x: dep.Test2; y: other.Test; + z: ui.Test; } From 26527fd44e8cdb21753b17cfb668c7c4466ab13e Mon Sep 17 00:00:00 2001 From: spinillos Date: Thu, 20 Apr 2023 13:36:06 +0200 Subject: [PATCH 9/9] Move import mapper test into main test --- generate_test.go | 24 ++++++++++++++++-------- internal/cuetxtar/cuetxtar.go | 13 ++----------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/generate_test.go b/generate_test.go index 2185108..9129ee6 100644 --- a/generate_test.go +++ b/generate_test.go @@ -46,13 +46,14 @@ func TestGenerateWithImports(t *testing.T) { ToDo: map[string]string{ "imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case", }, - ImportMappers: map[string]func(string) (string, error){ - "imports/imports": func(s string) (string, error) { - if s == "example.com/dep" { - return "@example/deps", nil - } - return s, nil - }, + } + + importMappers := map[string]func(s string) (string, error){ + "imports/imports": func(s string) (string, error) { + if s == "example.com/dep" { + return "@example/deps", nil + } + return s, nil }, } @@ -64,9 +65,16 @@ func TestGenerateWithImports(t *testing.T) { t.Fatal(v.Err()) } + im := func(s string) (string, error) { + return "", nil + } + if i, ok := importMappers[t.Name]; ok { + im = i + } + b, err := cuetsy.Generate(v, cuetsy.Config{ Export: true, - ImportMapper: t.ImportMapperFn, + ImportMapper: im, }) if err != nil { errors.Print(t, err, nil) diff --git a/internal/cuetxtar/cuetxtar.go b/internal/cuetxtar/cuetxtar.go index 1a2abeb..b126b97 100644 --- a/internal/cuetxtar/cuetxtar.go +++ b/internal/cuetxtar/cuetxtar.go @@ -59,8 +59,6 @@ type TxTarTest struct { // ToDo is a map of tests that should be skipped now, but should be fixed. ToDo map[string]string - - ImportMappers map[string]func(string) (string, error) } // A Test represents a single test based on a .txtar file. @@ -86,7 +84,7 @@ type Test struct { hasGold bool - ImportMapperFn func(s string) (string, error) + Name string } func (t *Test) Write(b []byte) (n int, err error) { @@ -270,6 +268,7 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) { T: t, Archive: a, Dir: filepath.Dir(filepath.Join(dir, fullpath)), + Name: testName, prefix: path.Join("out", x.Name), } @@ -292,14 +291,6 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) { t.Skip(msg) } - if im, ok := x.ImportMappers[testName]; ok { - tc.ImportMapperFn = im - } else { - tc.ImportMapperFn = func(s string) (string, error) { - return "", nil - } - } - f(tc) update := false