-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[codegen] Source Codegen from CUE App Manifest (#483)
## What this PR Does This PR changes the format of CUE codegen, moving from reading all top-level CUE selectors as kinds to reading a single `manifest` top-level field (or a different name if supplied). Other top-level fields are ignored, allowing kinds to still be defined as root-level keys, but they will only be parsed for codegen if listed in the `kinds` field in the manifest. This change is required to properly generate a manifest that * enforces that all kinds for an app have the same group (`group` is now a manifest-level attribute, and if it is specified in the kind, it must match the full computed API group for the manifest) * allows for specifying permissions required for the app operator * Ensures that all kinds for an app are now API resources (the `model` type has been fully removed) There are some slight changes around writing a kind, namely that the fields previous in `apiResource` are either moved into the main body of the kind, or into the manifest itself, as all kinds are now always API resources. * `apiResource.scope` is now just `scope` * `apiResource.validateion` is now just `validation` * `apiResource.mutation` is now just `mutation` * `apiResource.conversion` is now just `conversion` * `apiResource.groupOverride` is now in the manifest, as `manifest.groupOverride`, as it will apply to all kinds for the manifest The `grafana-app-sdk project init` command now generates a `kinds/manifest.cue` with a basic manifest, and `grafana-app-sdk project kind add` still creates a kind CUE file, but now also adds the kind to the list of kinds in `manifest.cue`, found under `manifest.kinds`. In the `codegen` package, jennies may use `codegen.Kind` or `codegen.AppManifest` as their input, to allow for not requiring too much change there. The `cuekind.Parser` now doesn't implement `codegen.Parser[codegen.Kind]` itself, and instead has methods to get a parser that implements either `codegen.Parser[codegen.Kind]` or `codegen.Parser[codegen.AppManifest]`. This is likely an intermediary step as we continue to improve the codegen pipeline's structure. This PR aims mainly to change the input type of `--format=cue` to use the manifest, and define that manifest properly, with a low-impact changes as possible to the rest of the project. --------- Co-authored-by: Igor Suleymanov <[email protected]>
- Loading branch information
1 parent
afa5519
commit a9d2b24
Showing
82 changed files
with
1,004 additions
and
866 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
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func addKindToManifestBytesCUE(manifestBytes []byte, kindFieldName string) ([]byte, error) { | ||
// Rather than attempt to load and modify in-CUE (as this is complex and will also change the CUE the user has written) | ||
// We will just modify the file at <kindpath>/manifest.cue and stick kindFieldName at the beginning of the `kinds` array | ||
// This is slightly brittle, but it keeps decent compatibility with the current `kind add` functionality. | ||
contents := string(manifestBytes) | ||
expr := regexp.MustCompile(`(?m)^(\s*kinds\s*:)(.*)$`) | ||
matches := expr.FindStringSubmatch(contents) | ||
if len(matches) < 3 { | ||
return nil, fmt.Errorf("could not find kinds field in manifest.cue") | ||
} | ||
kindsStr := matches[2] | ||
if regexp.MustCompile(`^\s*\[`).MatchString(kindsStr) { | ||
// Direct array, we can prepend our field | ||
// Check if there's anything in the array | ||
if regexp.MustCompile(`^\s\[\s*]`).MatchString(kindsStr) { | ||
// Empty, just replace with our field | ||
contents = expr.ReplaceAllString(contents, matches[1]+" ["+kindFieldName+"]") | ||
} else { | ||
kindsStr = regexp.MustCompile(`^\s*\[`).ReplaceAllString(kindsStr, " ["+kindFieldName+", ") | ||
contents = expr.ReplaceAllString(contents, matches[1]+kindsStr) | ||
} | ||
} else { | ||
// Not a simple list, prepend `[<fieldname>] + ` | ||
contents = expr.ReplaceAllString(contents, matches[1]+" ["+kindFieldName+"] + "+matches[2]) | ||
} | ||
return []byte(contents), 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
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.