Documentation
¶
Overview ¶
Package parse provides functions to parse blueprints from YAML or JSON data. It also provides functions to convert between YAML and JSON formats. The package uses the sigs.k8s.io/yaml package for YAML parsing and encoding.
Additionally, functions for validating schema against the OpenAPI specification are provided.
Index ¶
- Variables
- func ConvertJSONtoYAML(data []byte) ([]byte, error)
- func ConvertYAMLtoJSON(data []byte) ([]byte, error)
- func MarshalJSON(b *ubp.Blueprint, indent bool) ([]byte, error)
- func MarshalYAML(b *ubp.Blueprint) ([]byte, error)
- func ReadJSON(reader io.Reader) (*ubp.Blueprint, error)
- func ReadYAML(reader io.Reader) (*ubp.Blueprint, error)
- func UnmarshalAny(buf []byte, details *AnyDetails) (*ubp.Blueprint, error)
- func UnmarshalJSON(data []byte) (*ubp.Blueprint, error)
- func UnmarshalStrictJSON(data []byte) (*ubp.Blueprint, error)
- func UnmarshalStrictYAML(buf []byte) (*ubp.Blueprint, error)
- func UnmarshalYAML(buf []byte) (*ubp.Blueprint, error)
- func WriteJSON(b *ubp.Blueprint, writer io.Writer, indent bool) error
- func WriteYAML(b *ubp.Blueprint, writer io.Writer) error
- type AnyDetails
- type AnyFormat
- type Schema
- func (s *Schema) ApplyExtensions(ctx context.Context) error
- func (s *Schema) Bundle(ctx context.Context) error
- func (s *Schema) Document() *openapi3.T
- func (s *Schema) MarshalJSON() ([]byte, error)
- func (s *Schema) MarshalYAML() ([]byte, error)
- func (s *Schema) ReadAndValidateYAML(ctx context.Context, reader io.Reader) error
- func (s *Schema) ValidateAny(ctx context.Context, data []byte) error
- func (s *Schema) ValidateJSON(ctx context.Context, data []byte) error
- func (s *Schema) ValidateSchema(ctx context.Context) error
- func (s *Schema) ValidateYAML(ctx context.Context, data []byte) error
Constants ¶
This section is empty.
Variables ¶
var ErrCannotCompileSchema = errors.New("cannot compile schema")
ErrCannotCompileSchema is returned when the schema cannot be compiled.
var ErrParsingDetection = errors.New("parsing and detecting blueprint failed")
var ErrUnmarshal = errors.New("cannot unmarshal JSON/YAML")
ErrUnmarshal is returned when a buffer/reader cannot be unmarshaled.
var ErrValidateFailed = errors.New("validation failed")
ErrValidateFailed is returned when the validation fails.
Functions ¶
func ConvertJSONtoYAML ¶
ConvertJSONtoYAML converts JSON to YAML.
func ConvertYAMLtoJSON ¶
ConvertYAMLtoJSON converts YAML to JSON. Output is not indented.
func MarshalJSON ¶
MarshalJSON uses JSON encoder to marshal the object into JSON. Output can be optionaly indented.
Do not use this function for user-facing data.
func MarshalYAML ¶
MarshalYAML uses JSON encoder to marshal the object into JSON and then converts JSON to YAML. No YAML Go struct tags are necessary as JSON tags are used.
Uses sigs.k8s.io/yaml package for YAML encoding, for the API guarantees and compatibility read https://pkg.go.dev/sigs.k8s.io/yaml#Unmarshal.
func ReadJSON ¶
ReadJSON calls UnmarshalJSON after reading into a buffer.
Do not use this function for user-facing data.
func ReadYAML ¶
ReadYAML reads into a buffer and calls UnmarshalYAML. Read UnmarshalYAML for more details.
func UnmarshalAny ¶
func UnmarshalAny(buf []byte, details *AnyDetails) (*ubp.Blueprint, error)
UnmarshalAny attempts to unmarshal a blueprint from a byte slice in any format. It performs heuristic detection of UBP YAML, UBP JSON, BP TOML, and BP JSON formats via strict unmarshalling. If parsing fails, it returns a list of wrapped errors indicating the failure of each format attempt.
When BP format is detected, it is converted to UBP automatically and default values are populated.
To get some insights about the parsing process, you can pass an `AnyDetails` pointer as an argument. This allows finding out which format was detected, whether there were any warnings during the conversion, and what the intermediate blueprint was if conversion was necessary.
func UnmarshalJSON ¶
UnmarshalJSON uses JSON decoder to unmarshal into an object.
Do not use this function for user-facing data.
func UnmarshalStrictJSON ¶
UnmarshalStrictJSON uses JSON decoder to unmarshal into an object, but it does not allow unknown fields. See UnmarshalJSON for more details.
func UnmarshalStrictYAML ¶
UnmarshalStrictYAML loads a blueprint from YAML data, but it does not allow unknown fields. Read UnmarshalYAML for more details.
func UnmarshalYAML ¶
UnmarshalYAML loads a blueprint from YAML data. It converts YAML into JSON first, and then unmarshals it into a Blueprint object. This is done to ensure that the YAML representation is consistent with the JSON representation.
Uses sigs.k8s.io/yaml package for YAML parsing, for the API guarantees and compatibility read https://pkg.go.dev/sigs.k8s.io/yaml#Unmarshal.
Types ¶
type AnyDetails ¶
type AnyDetails struct {
Format AnyFormat // Detected format of the blueprint
Warnings error // Warnings encountered during conversion
Intermediate *bp.Blueprint // Intermediate blueprint if conversion was necessary
}
AnyDetails contains details about the parsing process of a blueprint in any format. It includes the format detected, warnings encountered, whether the blueprint was converted, and the intermediate blueprint if conversion was necessary.
type AnyFormat ¶
type AnyFormat int
AnyFormat represents the format of a blueprint that can be parsed.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
func CompileBundledSchema ¶
CompileBundledSchema compiles the JSON schema. Uses the bundled schema with extensions. Use this schema for validation.
func CompileSourceSchema ¶
CompileSourceSchema compiles the JSON schema. Uses the embedded schema from the oas/ directory. Returns the compiled schema or an error if the schema cannot be compiled.
Do not use this schema for validation, use bundled schema instead.
func (*Schema) ApplyExtensions ¶
ApplyExtensions applies extensions to the schema. It reads all files from the oas/extensions directory and applies them to the schema.
func (*Schema) Bundle ¶
Bundle resolves all references in the schema. It modifies the schema in place. It is not necessary to call this function if the schema is already bundled.
func (*Schema) MarshalJSON ¶
MarshalJSON marshals the schema to JSON.
func (*Schema) MarshalYAML ¶
MarshalYAML marshals the schema to YAML.
func (*Schema) ReadAndValidateYAML ¶
ReadAndValidateYAML reads YAML from the reader and calls ValidateMap. TODO: implement this function for JSON as well
func (*Schema) ValidateAny ¶
ValidateAny reads data and performs validation based on the detected file type. It supports both JSON and YAML formats. If the format is not supported, it returns an error.
func (*Schema) ValidateJSON ¶
ValidateJSON reads JSON and performs validation.
func (*Schema) ValidateSchema ¶
ValidateSchema validates the schema. It checks if the schema is valid and if all references are valid.