gen

package
v0.93.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: MIT Imports: 10 Imported by: 0

README

show output schemas generation

These schemas are necessary to include SHOW output in every resource and datasource. The work is repetitive, so it's easier to just generate all the needed schemas and mappers.

Description

File generate.go invokes the generation logic from main.go. By default, all SDK show output struct are used (listed in sdk_show_result_structs.go). After successful generation all SDK objects will have:

  • show output schema that can be used in the resource/datasource (e.g. warehouse_gen)
  • mapper from the SDK object to the generated schema (e.g. warehouse_gen)

How it works

Invoking the generation

To generate all show outputs (with a cleanup first) run:

make clean-show-output-schemas generate-show-output-schemas
Supported types

The following types are supported currently in the generator (schema and mappings):

  • basic types (string, int, float64, bool)
  • pointers to basic types (the same as above)
  • time.Time (pointer too)
  • enums based on string and int like sdk.WarehouseType or sdk.ResourceMonitorLevel (pointers too)
  • identifiers (pointers too):
    • sdk.AccountIdentifier
    • sdk.ExternalObjectIdentifier
    • sdk.AccountObjectIdentifier
    • sdk.DatabaseObjectIdentifier
    • sdk.SchemaObjectIdentifier
    • sdk.TableColumnIdentifier
  • sdk.ObjectIdentifier interface
To schema mappings

Given SDK struct field can be mapped to the generated schema depending on its type:

  • no mapping (Identity) - used for string and other basic types
  • string value mapping (ToString) - used e.g. for time.Time
  • fully qualified name mapping (FullyQualifiedName) - used for all identifiers and sdk.ObjectIdentifier interface
  • casting (CastToString and CastToInt) - used for enums with underlying type string or int
Changing the SDK object's show output

If you change the show output struct in the SDK:

  1. Check if you don't introduce a type that is unsupported (check supported types and known limitations).
  2. Run generation according to instructions.
Adding a new object to the SDK
  1. Add the new show output struct to sdk_show_result_structs.go.
  2. Check if you don't introduce a type that is unsupported (check supported types and known limitations).
  3. Run generation according to instructions.

Next steps

Known limitations
  • The following types (already existing in the SDK show output structs) are not yet supported (for all of them the schema will be generated with schema.TypeInvalid:
    • other basic types (e.g. int8, etc.)
    • slices of basic types ([]int, []string)
    • slices of identifiers ([]sdk.AccountIdentifier, []sdk.SchemaObjectIdentifier)
    • slices of enums ([]sdk.IntegrationType, []sdk.PluralObjectType)
    • structs (sdk.FileFormatTypeOptions)
Improvements

Functional improvements:

  • handle the missing types (TODOs in schema_field_mapper.go and struct_details_extractor_test.go)
  • parametrize the generation, e.g.:
    • generate only given object(s) - now all are always generated
    • manage the output - currently, the output consists of all structs displayed with fields, unique types grouped, and schemas generated
    • (optional) parametrize the output directory - currently, it's always written to schemas package
  • discover a change and generate as part of a make pre-push

Implementation improvements:

  • add acceptance test for a testStruct (the one from struct_details_extractor_test.go) for the whole generation flow
  • extract common generator functions inside the project (TODO in main.go; e.g. writeCodeToFile function)
  • test the generator part and improve error handling (TODOs in generator.go)
  • extract common template functions (TODO in templates.go))
  • (optional) consider different implementations of Mapper (e.g. TODO in schema_field_mapper_test.go: ugly comparison of functions with the current implementation of mapper and not ideal implementation in the to_schema_mapper.tmpl: runMapper .Mapper $nameLowerCase "." .OriginalName)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Identity           = func(field string) string { return field }
	ToString           = func(field string) string { return fmt.Sprintf("%s.String()", field) }
	FullyQualifiedName = func(field string) string { return fmt.Sprintf("%s.FullyQualifiedName()", field) }
	Name               = func(field string) string { return fmt.Sprintf("%s.Name()", field) }
	CastToString       = func(field string) string { return fmt.Sprintf("string(%s)", field) }
	CastToInt          = func(field string) string { return fmt.Sprintf("int(%s)", field) }
)
View Source
var (
	PreambleTemplate, _ = template.New("preambleTemplate").Parse(preambleTemplateContent)

	SchemaTemplate, _ = template.New("schemaTemplate").Funcs(template.FuncMap{
		"firstLetterLowercase": func(in string) string { return strings.ToLower(in[:1]) + in[1:] },
	}).Parse(schemaTemplateContent)

	ToSchemaMapperTemplate, _ = template.New("toSchemaMapperTemplate").Funcs(template.FuncMap{
		"firstLetterLowercase": func(in string) string { return strings.ToLower(in[:1]) + in[1:] },
		"runMapper":            func(mapper Mapper, in ...string) string { return mapper(strings.Join(in, "")) },
	}).Parse(toSchemaMapperTemplateContent)
)

TODO [SNOW-1501905]: extract common funcs

View Source
var AdditionalStructs = []any{
	sdk.SecurityIntegrationProperty{},
}

TODO [SNOW-1501905]: currently all this structs have the "Show" added to the schema, while these are not show outputs TODO [SNOW-1501905]: temporary struct, may be refactored with addition to generation of describe results; for now used to some structs needing a schema representation

Functions

func ColumnOutput

func ColumnOutput(columnWidth int, columns ...string) string

ColumnOutput is a helper to align a tabular output with the given columnWidth, e.g. (for 20 spaces column width): Name string string State sdk.WarehouseState string

func Generate

func Generate(model ShowResultSchemaModel, writer io.Writer)

TODO [SNOW-1501905]: handle panics better TODO [SNOW-1501905]: test and describe

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase allows converting a CamelCase text to camel_case one (needed for schema attribute names). Examples: - CamelCase -> camel_case - ACamelCase -> a_camel_case - URLParser -> url_parser - Camel1Case -> camel1_case - camelCase -> camel_case - camelURL -> camel_url - camelURLSomething -> camel_url_something

Types

type Field

type Field struct {
	Name           string
	ConcreteType   string
	UnderlyingType string
}

func (*Field) IsPointer

func (f *Field) IsPointer() bool

func (*Field) IsSlice

func (f *Field) IsSlice() bool

type Mapper

type Mapper func(string) string

type SchemaField

type SchemaField struct {
	Name                  string
	SchemaType            schema.ValueType
	OriginalName          string
	IsOriginalTypePointer bool
	Mapper                Mapper
}

func MapToSchemaField

func MapToSchemaField(field Field) SchemaField

TODO [SNOW-1501905]: handle other basic type variants TODO [SNOW-1501905]: handle any other interface (error) TODO [SNOW-1501905]: handle slices TODO [SNOW-1501905]: handle structs (chosen one or all)

type ShowResultSchemaModel

type ShowResultSchemaModel struct {
	Name         string
	SdkType      string
	SchemaFields []SchemaField
}

func ModelFromStructDetails

func ModelFromStructDetails(sdkStruct Struct) ShowResultSchemaModel

type Struct

type Struct struct {
	Name   string
	Fields []Field
}

func ExtractStructDetails

func ExtractStructDetails(s any) Struct

Jump to

Keyboard shortcuts

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