parse

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var ErrCannotCompileSchema = errors.New("cannot compile schema")

ErrCannotCompileSchema is returned when the schema cannot be compiled.

View Source
var ErrParsingDetection = errors.New("parsing and detecting blueprint failed")
View Source
var ErrUnmarshal = errors.New("cannot unmarshal JSON/YAML")

ErrUnmarshal is returned when a buffer/reader cannot be unmarshaled.

View Source
var ErrValidateFailed = errors.New("validation failed")

ErrValidateFailed is returned when the validation fails.

Functions

func ConvertJSONtoYAML

func ConvertJSONtoYAML(data []byte) ([]byte, error)

ConvertJSONtoYAML converts JSON to YAML.

func ConvertYAMLtoJSON

func ConvertYAMLtoJSON(data []byte) ([]byte, error)

ConvertYAMLtoJSON converts YAML to JSON. Output is not indented.

func MarshalJSON

func MarshalJSON(b *ubp.Blueprint, indent bool) ([]byte, error)

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

func MarshalYAML(b *ubp.Blueprint) ([]byte, error)

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

func ReadJSON(reader io.Reader) (*ubp.Blueprint, error)

ReadJSON calls UnmarshalJSON after reading into a buffer.

Do not use this function for user-facing data.

func ReadYAML

func ReadYAML(reader io.Reader) (*ubp.Blueprint, error)

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

func UnmarshalJSON(data []byte) (*ubp.Blueprint, error)

UnmarshalJSON uses JSON decoder to unmarshal into an object.

Do not use this function for user-facing data.

func UnmarshalStrictJSON

func UnmarshalStrictJSON(data []byte) (*ubp.Blueprint, error)

UnmarshalStrictJSON uses JSON decoder to unmarshal into an object, but it does not allow unknown fields. See UnmarshalJSON for more details.

func UnmarshalStrictYAML

func UnmarshalStrictYAML(buf []byte) (*ubp.Blueprint, error)

UnmarshalStrictYAML loads a blueprint from YAML data, but it does not allow unknown fields. Read UnmarshalYAML for more details.

func UnmarshalYAML

func UnmarshalYAML(buf []byte) (*ubp.Blueprint, error)

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.

func WriteJSON

func WriteJSON(b *ubp.Blueprint, writer io.Writer, indent bool) error

WriteJSON calls MarshalJSON and writes the result to the writer. Output can be optionaly indented.

Do not use this function for user-facing data.

func WriteYAML

func WriteYAML(b *ubp.Blueprint, writer io.Writer) error

WriteYAML calls MarshalYAML and writes the result to the writer.

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.

const (
	AnyFormatUnknown AnyFormat = iota // Unknown format
	AnyFormatUBPYAML                  // UBP YAML format
	AnyFormatUBPJSON                  // UBP JSON format
	AnyFormatBPTOML                   // BP TOML format
	AnyFormatBPJSON                   // BP JSON format
)

func (AnyFormat) String

func (f AnyFormat) String() string

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

func CompileBundledSchema

func CompileBundledSchema() (*Schema, error)

CompileBundledSchema compiles the JSON schema. Uses the bundled schema with extensions. Use this schema for validation.

func CompileSourceSchema

func CompileSourceSchema() (*Schema, error)

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

func (s *Schema) ApplyExtensions(ctx context.Context) error

ApplyExtensions applies extensions to the schema. It reads all files from the oas/extensions directory and applies them to the schema.

func (*Schema) Bundle

func (s *Schema) Bundle(ctx context.Context) error

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) Document

func (s *Schema) Document() *openapi3.T

func (*Schema) MarshalJSON

func (s *Schema) MarshalJSON() ([]byte, error)

MarshalJSON marshals the schema to JSON.

func (*Schema) MarshalYAML

func (s *Schema) MarshalYAML() ([]byte, error)

MarshalYAML marshals the schema to YAML.

func (*Schema) ReadAndValidateYAML

func (s *Schema) ReadAndValidateYAML(ctx context.Context, reader io.Reader) error

ReadAndValidateYAML reads YAML from the reader and calls ValidateMap. TODO: implement this function for JSON as well

func (*Schema) ValidateAny

func (s *Schema) ValidateAny(ctx context.Context, data []byte) error

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

func (s *Schema) ValidateJSON(ctx context.Context, data []byte) error

ValidateJSON reads JSON and performs validation.

func (*Schema) ValidateSchema

func (s *Schema) ValidateSchema(ctx context.Context) error

ValidateSchema validates the schema. It checks if the schema is valid and if all references are valid.

func (*Schema) ValidateYAML

func (s *Schema) ValidateYAML(ctx context.Context, data []byte) error

ValidateYAML reads YAML and performs validation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL