generated from MacroPower/go_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep the unmodified JSON schema for use in YAML files
- Loading branch information
1 parent
aa538aa
commit 6a17f6c
Showing
8 changed files
with
226 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v3" | ||
"kcl-lang.io/kcl-go/pkg/tools/gen" | ||
|
||
"github.com/MacroPower/kclipper/pkg/kclutil" | ||
helmschema "github.com/dadav/helm-schema/pkg/schema" | ||
) | ||
|
||
// ConvertToKCLSchema converts a JSON schema to a KCL schema. | ||
func ConvertToKCLSchema(jsonSchemaData []byte) ([]byte, error) { | ||
fixedJSONSchema, err := ConvertToKCLCompatibleJSONSchema(jsonSchemaData) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert to KCL compatible JSON schema: %w", err) | ||
} | ||
|
||
kclSchema := &bytes.Buffer{} | ||
if err := kclutil.Gen.GenKcl(kclSchema, "values", fixedJSONSchema, &gen.GenKclOptions{ | ||
Mode: gen.ModeJsonSchema, | ||
CastingOption: gen.OriginalName, | ||
UseIntegersForNumbers: true, | ||
}); err != nil { | ||
return nil, fmt.Errorf("failed to generate kcl schema: %w", err) | ||
} | ||
|
||
return kclSchema.Bytes(), nil | ||
} | ||
|
||
// ConvertToKCLCompatibleJSONSchema converts a JSON schema to a JSON schema that | ||
// is compatible with KCL schema generation (i.e. removing unsupported fields). | ||
func ConvertToKCLCompatibleJSONSchema(jsonSchemaData []byte) ([]byte, error) { | ||
// YAML is a superset of JSON, so this works and is simpler than re-writing | ||
// the Unmarshaler for JSON. | ||
var jsonNode yaml.Node | ||
if err := yaml.Unmarshal(jsonSchemaData, &jsonNode); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal JSON Schema: %w", err) | ||
} | ||
hs := &helmschema.Schema{} | ||
if err := hs.UnmarshalYAML(&jsonNode); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal JSON Schema: %w", err) | ||
} | ||
|
||
// Remove the ID to keep KCL schema naming consistent. | ||
hs.Id = "" | ||
|
||
// For now, merge into an empty schema as that will result in a schema that | ||
// is compatible with KCL schema generation. | ||
mhs := &helmschema.Schema{} | ||
mhs = mergeHelmSchemas(mhs, hs, true) | ||
|
||
fixedJSONSchema, err := mhs.ToJson() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert schema to JSON: %w", err) | ||
} | ||
|
||
return fixedJSONSchema, 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
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 |
---|---|---|
|
@@ -81,5 +81,6 @@ | |
}, | ||
"required": [] | ||
} | ||
} | ||
}, | ||
"required": [] | ||
} |
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,74 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"properties": { | ||
"configMaps": { | ||
"additionalProperties": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"annotations": { | ||
"additionalProperties": { | ||
"required": [], | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"required": [], | ||
"type": [ | ||
"object", | ||
"null" | ||
] | ||
}, | ||
"binaryData": { | ||
"additionalProperties": { | ||
"required": [], | ||
"type": "string" | ||
}, | ||
"required": [], | ||
"type": "object" | ||
}, | ||
"data": { | ||
"additionalProperties": { | ||
"required": [], | ||
"type": "string" | ||
}, | ||
"required": [], | ||
"type": "object" | ||
}, | ||
"enabled": { | ||
"default": true, | ||
"required": [], | ||
"type": "boolean" | ||
}, | ||
"includeInChecksum": { | ||
"default": true, | ||
"required": [], | ||
"type": "boolean" | ||
}, | ||
"labels": { | ||
"additionalProperties": { | ||
"required": [], | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"required": [], | ||
"type": [ | ||
"object", | ||
"null" | ||
] | ||
}, | ||
"nameOverride": { | ||
"required": [], | ||
"type": "string" | ||
} | ||
}, | ||
"required": [], | ||
"type": "object" | ||
}, | ||
"required": [] | ||
} | ||
}, | ||
"required": [] | ||
} |
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