-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from grafana/generate-imports
gen: Introduce generic import mapping
- Loading branch information
Showing
9 changed files
with
226 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
module github.com/grafana/cuetsy | ||
|
||
go 1.16 | ||
go 1.19 | ||
|
||
require ( | ||
cuelang.org/go v0.5.0 | ||
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.9.0 | ||
github.com/stretchr/testify v1.7.1 // indirect | ||
github.com/urfave/cli/v2 v2.24.4 | ||
github.com/urfave/cli/v2 v2.25.0 | ||
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/cpuguy83/go-md2man/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/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/stretchr/testify v1.7.1 // indirect | ||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // 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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.