code

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package holds higher-level abstractions on top of OpenAPI that are used to generate code via text/template for Databricks SDK in different languages.

Index

Constants

This section is empty.

Variables

View Source
var ErrSkipThisFile = errors.New("skip generating this file")
View Source
var HelperFuncs = template.FuncMap{
	"notLast": func(idx int, a interface{}) bool {
		return idx+1 != reflect.ValueOf(a).Len()
	},
	"lower": strings.ToLower,
	"lowerFirst": func(s string) string {
		return strings.ToLower(s[0:1]) + s[1:]
	},
	"trimPrefix": func(right, left string) string {
		return strings.TrimPrefix(left, right)
	},
	"trimSuffix": func(right, left string) string {
		return strings.TrimSuffix(left, right)
	},
	"replaceAll": func(from, to, str string) string {
		return strings.ReplaceAll(str, from, to)
	},
	"without": func(left, right string) string {
		return strings.ReplaceAll(right, left, "")
	},
	"skipThisFile": func() error {

		panic(ErrSkipThisFile)
	},
	"alphanumOnly": func(in []*Field) (out []*Field) {
		for _, v := range in {
			if !alphanumRE.MatchString(v.Name) {
				continue
			}
			out = append(out, v)
		}
		return out
	},
	"list": func(l ...any) []any {
		return l
	},
	"in": func(haystack []any, needle string) bool {
		for _, v := range haystack {
			if needle == fmt.Sprint(v) {
				return true
			}
		}
		return false
	},
	"dict": func(args ...any) map[string]any {
		if len(args)%2 != 0 {
			panic("number of arguments to dict is not even")
		}
		result := map[string]any{}
		for i := 0; i < len(args); i += 2 {
			k := fmt.Sprint(args[i])
			v := args[i+1]
			result[k] = v
		}
		return result
	},
	"getOrDefault": func(dict map[string]any, key string, def any) any {
		v, ok := dict[key]
		if ok {
			return v
		}
		return def
	},
	"fmt": fmt.Sprintf,
	"concat": func(v ...string) string {
		return strings.Join(v, "")
	},
}

Functions

This section is empty.

Types

type Batch

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

func NewFromFile

func NewFromFile(name string) (*Batch, error)

NewFromFile loads OpenAPI specification from file

func NewFromSpec

func NewFromSpec(spec *openapi.Specification) (*Batch, error)

NewFromSpec converts OpenAPI spec to intermediate representation

func (*Batch) FullName

func (b *Batch) FullName() string

func (*Batch) Packages

func (b *Batch) Packages() (pkgs []*Package)

Packages returns sorted slice of packages

func (*Batch) Services

func (b *Batch) Services() (services []*Service)

Pkgs returns sorted slice of packages

func (*Batch) Types

func (b *Batch) Types() (types []*Entity)

Pkgs returns sorted slice of packages

type Binding

type Binding struct {
	// Polling method request field
	PollField *Field

	// Wrapped method either response or request body field
	Bind *Field

	// Is wrapped method response used?
	IsResponseBind bool
}

Binding connects fields in generated code across multiple requests

type Entity

type Entity struct {
	Named
	Package *Package

	ArrayValue *Entity
	MapValue   *Entity
	IsInt      bool
	IsInt64    bool
	IsFloat64  bool
	IsBool     bool
	IsString   bool
	IsEmpty    bool

	// this field does not have a concrete type
	IsAny bool

	// this field is computed on the platform side
	IsComputed bool

	// if entity has required fields, this is the order of them
	RequiredOrder []string

	// Schema references the OpenAPI schema this entity was created from.
	Schema *openapi.Schema
	// contains filtered or unexported fields
}

Entity represents a Type

func (*Entity) CamelName

func (e *Entity) CamelName() string

CamelName overrides parent implementation by appending List suffix for unnamed list types

func (*Entity) Enum

func (e *Entity) Enum() (enum []EnumEntry)

Enum returns all entries for enum entities

func (*Entity) Field

func (e *Entity) Field(name string) *Field

Field gets field representation by name or nil

func (*Entity) Fields

func (e *Entity) Fields() (fields []*Field)

Fields returns sorted slice of field representations

func (*Entity) FullName

func (e *Entity) FullName() string

FullName includes package name and untransformed name of the entity

func (*Entity) GetUnderlyingFields

func (e *Entity) GetUnderlyingFields(path []string) ([]*Field, error)

Given a list of field names, return the list of *Field objects which result from following the path of fields in the entity.

func (*Entity) HasJsonField

func (e *Entity) HasJsonField() bool

HasJsonField returns true if any of the fields is in the body

func (*Entity) HasQueryField

func (e *Entity) HasQueryField() bool

HasQueryField returns true if any of the fields is from query

func (*Entity) HasRequiredNonBodyField

func (e *Entity) HasRequiredNonBodyField() bool

func (*Entity) IsAllRequiredFieldsPrimitive

func (e *Entity) IsAllRequiredFieldsPrimitive() bool

func (*Entity) IsExternal

func (e *Entity) IsExternal() bool

IsExternal returns true if entity is declared in external package and has to be imported from it

func (*Entity) IsNumber

func (e *Entity) IsNumber() bool

IsNumber returns true if field is numeric

func (*Entity) IsObject

func (e *Entity) IsObject() bool

IsObject returns true if entity is not a Mpa and has more than zero fields

func (*Entity) IsOnlyPrimitiveFields

func (e *Entity) IsOnlyPrimitiveFields() bool

func (*Entity) IsPrimitive

func (e *Entity) IsPrimitive() bool

func (*Entity) IsPrivatePreview

func (e *Entity) IsPrivatePreview() bool

IsPrivatePreview flags object being in private preview.

func (*Entity) IsPublicPreview

func (e *Entity) IsPublicPreview() bool

IsPublicPreview flags object being in public preview.

func (*Entity) IsReferred

func (e *Entity) IsReferred() bool

func (*Entity) IsRequest

func (e *Entity) IsRequest() bool

func (*Entity) IsResponse

func (e *Entity) IsResponse() bool

func (*Entity) NonRequiredFields

func (e *Entity) NonRequiredFields() (fields []*Field)

func (*Entity) PascalName

func (e *Entity) PascalName() string

PascalName overrides parent implementation by appending List suffix for unnamed list types

func (*Entity) RequiredFields

func (e *Entity) RequiredFields() (fields []*Field)

type EnumEntry

type EnumEntry struct {
	Named
	Entity *Entity
	// SCIM API has schema specifiers
	Content string
}

type Field

type Field struct {
	Named
	Required bool
	Entity   *Entity
	Of       *Entity
	IsJson   bool
	IsPath   bool
	IsQuery  bool
	Schema   *openapi.Schema
}

Field of a Type (Entity)

func (*Field) IsOptionalObject

func (f *Field) IsOptionalObject() bool

func (*Field) IsPrivatePreview

func (f *Field) IsPrivatePreview() bool

IsPrivatePreview flags object being in private preview.

func (*Field) IsPublicPreview

func (f *Field) IsPublicPreview() bool

IsPublicPreview flags object being in public preview.

type Method

type Method struct {
	Named
	Service *Service
	// HTTP method name
	Verb string
	// Full API Path, including /api/2.x prefix
	Path string
	// Slice of path params, e.g. permissions/{type}/{id}
	PathParts []PathPart
	// Request type representation
	Request *Entity
	// Response type representation
	Response          *Entity
	EmptyResponseName Named

	// For list APIs, the path of fields in the response entity to follow to get
	// the resource ID.
	IdFieldPath []*Field

	// For list APIs, the path of fields in the response entity to follow to get
	// the user-friendly name of the resource.
	NameFieldPath []*Field
	// contains filtered or unexported fields
}

Method represents service RPC

func (*Method) CanHaveResponseBody

func (m *Method) CanHaveResponseBody() bool

func (*Method) GetByName

func (m *Method) GetByName() *Entity

GetByName returns entity from the same service with x-databricks-crud:read

func (*Method) HasIdentifierField

func (m *Method) HasIdentifierField() bool

func (*Method) HasNameField

func (m *Method) HasNameField() bool

func (*Method) IdentifierField

func (m *Method) IdentifierField() *Field

func (*Method) IsCrudCreate

func (m *Method) IsCrudCreate() bool

func (*Method) IsCrudRead

func (m *Method) IsCrudRead() bool

func (*Method) IsJsonOnly

func (m *Method) IsJsonOnly() bool

func (*Method) IsPrivatePreview

func (m *Method) IsPrivatePreview() bool

IsPrivatePreview flags object being in private preview.

func (*Method) IsPublicPreview

func (m *Method) IsPublicPreview() bool

IsPublicPreview flags object being in public preview.

func (*Method) NamedIdMap

func (m *Method) NamedIdMap() *NamedIdMap

NamedIdMap returns name-to-id mapping retrieval definition for all entities of a type

func (*Method) NeedsOffsetDedupe

func (m *Method) NeedsOffsetDedupe() bool

func (*Method) Pagination

func (m *Method) Pagination() *Pagination

Pagination returns definition for possibly multi-request result iterator

func (*Method) Shortcut

func (m *Method) Shortcut() *Shortcut

Shortcut creates definition from path params and single-field request entities

func (*Method) TitleVerb

func (m *Method) TitleVerb() string

func (*Method) Wait

func (m *Method) Wait() *Wait

Wait returns definition for long-running operation

type Named

type Named struct {
	Name        string
	Description string
}

Named holds common methods for identifying and describing things

func (*Named) AbbrName

func (n *Named) AbbrName() string

AbbrName returns `nlt` for `namesLikeThis`

func (*Named) CamelName

func (n *Named) CamelName() string

CamelName creates namesLikesThis

func (*Named) Comment

func (n *Named) Comment(prefix string, maxLen int) string

Comment formats description into language-specific comment multi-line strings

func (*Named) ConstantName

func (n *Named) ConstantName() string

ConstantName creates NAMES_LIKE_THIS

func (*Named) DescriptionWithoutSummary

func (n *Named) DescriptionWithoutSummary() string

func (*Named) HasComment

func (n *Named) HasComment() bool

func (*Named) IsNameReserved

func (n *Named) IsNameReserved() bool

func (*Named) KebabName

func (n *Named) KebabName() string

KebabName creates names-like-this

func (*Named) PascalName

func (n *Named) PascalName() string

PascalName creates NamesLikesThis

func (*Named) Singular

func (n *Named) Singular() *Named

func (*Named) SnakeName

func (n *Named) SnakeName() string

SnakeName creates names_like_this

func (*Named) Summary

func (n *Named) Summary() string

Summary gets the first sentence from the description. Always ends in a dot.

func (*Named) TitleName

func (n *Named) TitleName() string

TitleName creates Names Likes This

func (*Named) TrimPrefix

func (n *Named) TrimPrefix(prefix string) *Named

TrimPrefix returns *Named, but with a prefix trimmed from CamelName()

Example:

(&Named{Name: "AccountMetastoreAssigment"}).TrimPrefix("account").CamelName() == "metastoreAssignment"

type NamedIdMap

type NamedIdMap struct {
	Named
	IdPath   []*Field
	NamePath []*Field
	Entity   *Entity

	// if List method returns []Item directly
	// without generated iteration wrapper
	Direct bool
}

NamedIdMap depends on Pagination and is generated, when paginated item entity has Identifier and Name fields. End-users usually use this method for drop-downs or any other selectors.

func (*NamedIdMap) Id

func (n *NamedIdMap) Id() *Field

type Package

type Package struct {
	Named
	Components *openapi.Components
	// contains filtered or unexported fields
}

Package represents a service package, which contains entities and interfaces that are relevant to a single service

func (*Package) EmptyTypes

func (pkg *Package) EmptyTypes() (types []*Named)

EmptyTypes returns sorted list of types without fields

func (*Package) FullName

func (pkg *Package) FullName() string

FullName just returns pacakge name

func (*Package) HasPagination

func (pkg *Package) HasPagination() bool

HasPagination returns try if any service within this package has result iteration

func (*Package) HasPathParams

func (pkg *Package) HasPathParams() bool

HasPathParams returns true if any service has methods that rely on path params

func (*Package) HasWaits

func (pkg *Package) HasWaits() bool

HasWaits returns true if any service has methods with long-running operations

func (*Package) ImportedEntities

func (pkg *Package) ImportedEntities() (res []*Entity)

func (*Package) ImportedPackages

func (pkg *Package) ImportedPackages() (res []string)

func (*Package) Load

func (pkg *Package) Load(spec *openapi.Specification, tag openapi.Tag) error

Load takes OpenAPI specification and loads a service model

func (*Package) MainService

func (pkg *Package) MainService() *Service

MainService returns a Service that matches Package name

func (*Package) Services

func (pkg *Package) Services() (types []*Service)

Services returns sorted slice of services

func (*Package) Types

func (pkg *Package) Types() (types []*Entity)

Types returns sorted slice of types

type Pagination

type Pagination struct {
	Offset    *Field
	Limit     *Field
	Results   *Field
	Entity    *Entity
	Token     *Binding
	Increment int
}

Pagination holds definition of result iteration type per specific RPC. Databricks as of now has a couple different types of pagination:

  • next_token/next_page_token + repeated field
  • offset/limit with zero-based offsets + repeated field
  • page/limit with 1-based pages + repeated field
  • repeated inline field
  • repeated field

func (*Pagination) MultiRequest

func (p *Pagination) MultiRequest() bool

type PathPart

type PathPart struct {
	Prefix      string
	Field       *Field
	IsAccountId bool
}

PathPart represents required field, that is always part of the path

type Service

type Service struct {
	Named
	IsRpcStyle bool
	IsAccounts bool
	Package    *Package

	ByPathParamsMethods []*Shortcut
	// contains filtered or unexported fields
}

Service represents specific Databricks API

func (*Service) Create

func (svc *Service) Create() *Method

List returns a method annotated with x-databricks-crud:create

func (*Service) Delete

func (svc *Service) Delete() *Method

List returns a method annotated with x-databricks-crud:delete

func (*Service) FullName

func (svc *Service) FullName() string

FullName holds package name and service name

func (*Service) HasPagination

func (svc *Service) HasPagination() bool

HasPagination returns true if any method has result iteration

func (*Service) HasWaits

func (svc *Service) HasWaits() bool

func (*Service) IsPrivatePreview

func (svc *Service) IsPrivatePreview() bool

IsPrivatePreview flags object being in private preview.

func (*Service) IsPublicPreview

func (svc *Service) IsPublicPreview() bool

IsPublicPreview flags object being in public preview.

func (*Service) List

func (svc *Service) List() *Method

List returns a method annotated with x-databricks-crud:list

func (*Service) MatchesPackageName

func (svc *Service) MatchesPackageName() bool

MatchesPackageName if package name and service name are the same, e.g. `clusters` package & `Clusters` service

func (*Service) Methods

func (svc *Service) Methods() (methods []*Method)

Methods returns sorted slice of methods

func (*Service) Read

func (svc *Service) Read() *Method

List returns a method annotated with x-databricks-crud:read

func (*Service) Update

func (svc *Service) Update() *Method

List returns a method annotated with x-databricks-crud:update

func (*Service) Waits

func (svc *Service) Waits() (waits []*Wait)

type Shortcut

type Shortcut struct {
	Named
	Params []Field
	Method *Method
}

Shortcut holds definition of "shortcut" methods, that are generated for methods with request entities only with required fields.

type Wait

type Wait struct {
	Named
	// represents a method that triggers the start of the long-running operation
	Method *Method
}

Wait represents a long-running operation, that requires multiple RPC calls

func (*Wait) Binding

func (w *Wait) Binding() (binding []Binding)

Binding returns a slice of request and response connections

func (*Wait) ComplexMessagePath

func (w *Wait) ComplexMessagePath() bool

func (*Wait) Failure

func (w *Wait) Failure() (match []EnumEntry)

Failure holds the failed end-states of the operation

func (*Wait) ForceBindRequest

func (w *Wait) ForceBindRequest() bool

ForceBindRequest is a workaround for Jobs#RepairRun, that does not send run_id in response

func (*Wait) MessagePath

func (w *Wait) MessagePath() (path []*Field)

MessagePath holds the path to the field of polled entity, that can tell about current inner status of the long-running operation

func (*Wait) MessagePathHead

func (w *Wait) MessagePathHead() *Field

func (*Wait) Poll

func (w *Wait) Poll() *Method

Poll returns method definition for checking the state of the long running operation

func (*Wait) Status

func (w *Wait) Status() *Field

func (*Wait) StatusPath

func (w *Wait) StatusPath() (path []*Field)

StatusPath holds the path to the field of polled entity, that holds current state of the long-running operation

func (*Wait) Success

func (w *Wait) Success() (match []EnumEntry)

Success holds the successful end-states of the operation

func (*Wait) Timeout

func (w *Wait) Timeout() int

Timeout returns timeout in minutes, defaulting to 20

Jump to

Keyboard shortcuts

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