-
Notifications
You must be signed in to change notification settings - Fork 600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add command to generate jsonschema for limayaml #2306
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
88c17d3
Add command to generate jsonschema for limayaml
afbjorklund b89e5b6
Improve the limayaml and default.yaml syntax
afbjorklund 395fb58
Don't mention null as the builtin default
afbjorklund b204276
Change mount mountPoint to string pointer
afbjorklund 475f4c6
Allow Disk to use either name or struct
afbjorklund cfb63d8
Add jsonschema enum for the property types
afbjorklund 55224aa
Allow using composed words, like Arch and Types
afbjorklund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
_output/ | ||
_artifacts/ | ||
lima.REJECTED.yaml | ||
schema-limayaml.json | ||
.config |
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,59 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/invopop/jsonschema" | ||
"github.com/lima-vm/lima/pkg/limayaml" | ||
"github.com/spf13/cobra" | ||
orderedmap "github.com/wk8/go-ordered-map/v2" | ||
) | ||
|
||
func newGenSchemaCommand() *cobra.Command { | ||
genschemaCommand := &cobra.Command{ | ||
Use: "generate-jsonschema", | ||
Short: "Generate json-schema document", | ||
Args: WrapArgsError(cobra.NoArgs), | ||
RunE: genschemaAction, | ||
Hidden: true, | ||
} | ||
return genschemaCommand | ||
} | ||
|
||
func toAny(args []string) []any { | ||
result := []any{nil} | ||
for _, arg := range args { | ||
result = append(result, arg) | ||
} | ||
return result | ||
} | ||
|
||
func getProp(props *orderedmap.OrderedMap[string, *jsonschema.Schema], key string) *jsonschema.Schema { | ||
value, ok := props.Get(key) | ||
if !ok { | ||
return nil | ||
} | ||
return value | ||
} | ||
|
||
func genschemaAction(cmd *cobra.Command, _ []string) error { | ||
schema := jsonschema.Reflect(&limayaml.LimaYAML{}) | ||
// allow Disk to be either string (name) or object (struct) | ||
schema.Definitions["Disk"].Type = "" // was: "object" | ||
schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{ | ||
{Type: "string"}, | ||
{Type: "object"}, | ||
} | ||
properties := schema.Definitions["LimaYAML"].Properties | ||
getProp(properties, "os").Enum = toAny(limayaml.OSTypes) | ||
getProp(properties, "arch").Enum = toAny(limayaml.ArchTypes) | ||
getProp(properties, "mountType").Enum = toAny(limayaml.MountTypes) | ||
getProp(properties, "vmType").Enum = toAny(limayaml.VMTypes) | ||
j, err := json.MarshalIndent(schema, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprintln(cmd.OutOrStdout(), string(j)) | ||
return err | ||
} | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a test? What happens when this is out of the sync with the YAML struct definition?