jsonast

package
v0.0.0-...-b4ea117 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SwaggerGroupRegex = regexp.MustCompile(`[Mm]icrosoft\.[^/\\]+`)

SwaggerGroupRegex matches a “group” (Swagger ‘namespace’) based on: https://github.com/Azure/autorest/blob/85de19623bdce3ccc5000bae5afbf22a49bc4665/core/lib/pipeline/metadata-generation.ts#L25

Functions

func GetPrimitiveType

func GetPrimitiveType(name SchemaType) (*astmodel.PrimitiveType, error)

GetPrimitiveType returns the primtive type for this schema type

Types

type CachingFileLoader

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

CachingFileLoader is a cache of schema that have been loaded, identified by file path

func NewCachingFileLoader

func NewCachingFileLoader(specs map[string]PackageAndSwagger) CachingFileLoader

NewCachingFileLoader creates an OpenAPISchemaCache with the initial file path → spec mapping

type GoJSONSchema

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

GoJSONSchema implements the Schema abstraction for gojsonschema

func (GoJSONSchema) Id

func (schema GoJSONSchema) Id() string

type OpenAPIFileLoader

type OpenAPIFileLoader interface {
	// contains filtered or unexported methods
}

type OpenAPISchema

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

OpenAPISchema implements the Schema abstraction for go-openapi

func (*OpenAPISchema) Id

func (schema *OpenAPISchema) Id() string

type PackageAndSwagger

type PackageAndSwagger struct {
	Package *astmodel.LocalPackageReference
	Swagger spec.Swagger
}

type ResourceDefinition

type ResourceDefinition struct {
	SpecType            astmodel.Type
	StatusType          astmodel.Type
	SourceFile          string
	ARMType             string // e.g. Microsoft.XYZ/resourceThings
	ARMURI              string
	SupportedOperations set.Set[astmodel.ResourceOperation]
	Scope               astmodel.ResourceScope
}

type Schema

type Schema interface {
	Id() string // Unique ID of this Schema within the document (if any)
	// contains filtered or unexported methods
}

Schema abstracts over the exact implementation of JSON schema we are using as we need to be able to process inputs provided by both 'gojsonschema' and 'go-openapi'. It is possible we could switch to 'go-openapi' for everything because it could handle both JSON Schema and Swagger; but this is not yet done.

func MakeGoJSONSchema

func MakeGoJSONSchema(
	schema *gojsonschema.SubSchema,
	makeLocalPackageReference func(groupName, version string) astmodel.LocalPackageReference,
	idFactory astmodel.IdentifierFactory,
) Schema

MakeGoJSONSchema wrapes a gojsonschema.SubSchema to conform to the Schema abstraction

func MakeOpenAPISchema

func MakeOpenAPISchema(
	name string,
	schema spec.Schema,
	fileName string,
	outputPackage astmodel.LocalPackageReference,
	idFactory astmodel.IdentifierFactory,
	cache OpenAPIFileLoader,
	log logr.Logger,
) Schema

MakeOpenAPISchema wraps a spec.Swagger to conform to the Schema abstraction

type SchemaScanner

type SchemaScanner struct {
	TypeHandlers map[SchemaType]TypeHandler
	// contains filtered or unexported fields
}

A SchemaScanner is used to scan a JSON Schema extracting and collecting type definitions

func NewSchemaScanner

func NewSchemaScanner(
	idFactory astmodel.IdentifierFactory,
	configuration *config.Configuration,
	log logr.Logger,
) *SchemaScanner

NewSchemaScanner constructs a new scanner, ready for use

func (*SchemaScanner) AddTypeHandler

func (scanner *SchemaScanner) AddTypeHandler(schemaType SchemaType, handler TypeHandler)

AddTypeHandler will override a default type handler for a given SchemaType. This allows for a consumer to customize AST generation.

func (*SchemaScanner) Definitions

func (scanner *SchemaScanner) Definitions() astmodel.TypeDefinitionSet

Definitions produces a set of all the types defined so far

func (*SchemaScanner) GenerateAllDefinitions

func (scanner *SchemaScanner) GenerateAllDefinitions(ctx context.Context, schema Schema) (astmodel.TypeDefinitionSet, error)

func (*SchemaScanner) RunHandler

func (scanner *SchemaScanner) RunHandler(ctx context.Context, schemaType SchemaType, schema Schema) (astmodel.Type, error)

RunHandler triggers the appropriate handler for the specified schemaType

func (*SchemaScanner) RunHandlerForSchema

func (scanner *SchemaScanner) RunHandlerForSchema(ctx context.Context, schema Schema) (astmodel.Type, error)

RunHandlerForSchema inspects the passed schema to identify what kind it is, then runs the appropriate handler

func (*SchemaScanner) RunHandlersForSchemas

func (scanner *SchemaScanner) RunHandlersForSchemas(ctx context.Context, schemas []Schema) ([]astmodel.Type, error)

RunHandlersForSchemas inspects each passed schema and runs the appropriate handler

type SchemaType

type SchemaType string

SchemaType defines the type of JSON schema node we are currently processing

const (
	AnyOf   SchemaType = "anyOf"
	AllOf   SchemaType = "allOf"
	OneOf   SchemaType = "oneOf"
	Ref     SchemaType = "ref"
	Array   SchemaType = "array"
	Bool    SchemaType = "boolean"
	Int     SchemaType = "integer"
	Number  SchemaType = "number"
	Object  SchemaType = "object"
	String  SchemaType = "string"
	Enum    SchemaType = "enum"
	Unknown SchemaType = "unknown"
)

Definitions for different kinds of JSON schema

type SwaggerTypeExtractor

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

func NewSwaggerTypeExtractor

func NewSwaggerTypeExtractor(
	config *config.Configuration,
	idFactory astmodel.IdentifierFactory,
	swagger spec.Swagger,
	swaggerPath string,
	outputPackage astmodel.LocalPackageReference,
	cache CachingFileLoader,
	log logr.Logger,
) SwaggerTypeExtractor

NewSwaggerTypeExtractor creates a new SwaggerTypeExtractor

func (*SwaggerTypeExtractor) ExtractOneOfTypes

func (extractor *SwaggerTypeExtractor) ExtractOneOfTypes(
	ctx context.Context,
	scanner *SchemaScanner,
	result SwaggerTypes,
) error

ExtractOneOfTypes ensures we haven't missed any of the required OneOf type definitions. The depth-first search of the Swagger spec done by ExtractResourcetypes() won't have found any "loose" one of options, so we need this extra step.

func (*SwaggerTypeExtractor) ExtractResourceTypes

func (extractor *SwaggerTypeExtractor) ExtractResourceTypes(ctx context.Context, scanner *SchemaScanner, result SwaggerTypes) error

func (*SwaggerTypeExtractor) ExtractTypes

func (extractor *SwaggerTypeExtractor) ExtractTypes(ctx context.Context) (SwaggerTypes, error)

ExtractTypes finds all operations in the Swagger spec that have a PUT verb and a path like "Microsoft.GroupName/…/resourceName/{resourceId}", and extracts the types for those operations, into the 'resourceTypes' result. Any additional types required by the resource types are placed into the 'otherTypes' result.

type SwaggerTypes

type SwaggerTypes struct {
	ResourceDefinitions ResourceDefinitionSet
	OtherDefinitions    astmodel.TypeDefinitionSet
}

type TypeHandler

type TypeHandler func(ctx context.Context, scanner *SchemaScanner, schema Schema, log logr.Logger) (astmodel.Type, error)

TypeHandler is a standard delegate used for walking the schema tree. Note that it is permissible for a TypeHandler to return `nil, nil`, which indicates that there is no type to be included in the output.

type UnknownSchemaError

type UnknownSchemaError struct {
	Schema  Schema
	Filters []string
}

UnknownSchemaError is used when we find a JSON schema node that we don't know how to handle

func (*UnknownSchemaError) Error

func (use *UnknownSchemaError) Error() string

Jump to

Keyboard shortcuts

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