| 
									
										
										
										
											2024-03-21 18:11:29 +08:00
										 |  |  | package codegen | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"sort" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"cuelang.org/go/cue/ast" | 
					
						
							|  |  |  | 	tsast "github.com/grafana/cuetsy/ts/ast" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CUE import paths, mapped to corresponding TS import paths. An empty value
 | 
					
						
							|  |  |  | // indicates the import path should be dropped in the conversion to TS. Imports
 | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | // not present in the list are not allowed, and code generation will fail.
 | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | var importMap = map[string]string{ | 
					
						
							| 
									
										
										
										
											2025-01-10 20:33:51 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/codegen/pfs":            "", | 
					
						
							| 
									
										
										
										
											2023-01-19 01:40:22 +08:00
										 |  |  | 	"github.com/grafana/grafana/packages/grafana-schema/src/common": "@grafana/schema", | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | 	allow := PermittedCUEImports() | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | 	strsl := make([]string, 0, len(importMap)) | 
					
						
							|  |  |  | 	for p := range importMap { | 
					
						
							|  |  |  | 		strsl = append(strsl, p) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sort.Strings(strsl) | 
					
						
							|  |  |  | 	sort.Strings(allow) | 
					
						
							|  |  |  | 	if strings.Join(strsl, "") != strings.Join(allow, "") { | 
					
						
							|  |  |  | 		panic("CUE import map is not the same as permitted CUE import list - these files must be kept in sync!") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | // PermittedCUEImports returns the list of import paths that may be imported in
 | 
					
						
							|  |  |  | // Grafana kind definitions.
 | 
					
						
							|  |  |  | func PermittedCUEImports() []string { | 
					
						
							|  |  |  | 	return []string{ | 
					
						
							| 
									
										
										
										
											2025-01-10 20:33:51 +08:00
										 |  |  | 		"github.com/grafana/grafana/pkg/plugins/codegen/pfs", | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | 		"github.com/grafana/grafana/packages/grafana-schema/src/common", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // MapCUEImportToTS maps the provided CUE import path to the corresponding
 | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | // TypeScript import path in generated code.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Providing an import path that is not allowed results in an error. If a nil
 | 
					
						
							|  |  |  | // error and empty string are returned, the import path should be dropped in
 | 
					
						
							|  |  |  | // generated code.
 | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | func MapCUEImportToTS(path string) (string, error) { | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | 	i, has := importMap[path] | 
					
						
							|  |  |  | 	if !has { | 
					
						
							|  |  |  | 		return "", fmt.Errorf("import %q in models.cue is not allowed", path) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return i, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | // ConvertImport converts a CUE import statement, represented in its 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 ConvertImport(im *ast.ImportSpec) (tsast.ImportSpec, error) { | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | 	tsim := tsast.ImportSpec{} | 
					
						
							| 
									
										
										
										
											2023-05-30 20:56:18 +08:00
										 |  |  | 	pkg, err := MapCUEImportToTS(strings.Trim(im.Path.Value, "\"")) | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | 	if err != nil || pkg == "" { | 
					
						
							|  |  |  | 		// err should be unreachable if paths has been verified already
 | 
					
						
							|  |  |  | 		// Empty string mapping means skip it
 | 
					
						
							|  |  |  | 		return tsim, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tsim.From = tsast.Str{Value: pkg} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if im.Name != nil && im.Name.String() != "" { | 
					
						
							|  |  |  | 		tsim.AsName = im.Name.String() | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-01-21 00:04:44 +08:00
										 |  |  | 		sl := strings.Split(strings.Trim(im.Path.Value, "\""), "/") | 
					
						
							| 
									
										
										
										
											2022-12-02 15:22:28 +08:00
										 |  |  | 		final := sl[len(sl)-1] | 
					
						
							|  |  |  | 		if idx := strings.Index(final, ":"); idx != -1 { | 
					
						
							|  |  |  | 			tsim.AsName = final[idx:] | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			tsim.AsName = final | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return tsim, nil | 
					
						
							|  |  |  | } |