codegen

package
v0.0.1-alpha.0...-700cb3a Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrObjectDocumentationNotFound ...
	ErrObjectDocumentationNotFound = errors.New("documentation not found")
	// ErrEmptyDocumentation ...
	ErrEmptyDocumentation = errors.New("empty documentation table: likely a chromium rendering issue")
	// ErrInternalProperty ...
	ErrInternalProperty = errors.New("internal salesforce property do not use")
	// ErrPossiblyNotEnabled ...
	ErrPossiblyNotEnabled = errors.New("property is disabled by default")
	// ErrPropertyNameNotFound ...
	ErrPropertyNameNotFound = errors.New("property name not found")
	// ErrTableStructureUnknown ...
	ErrTableStructureUnknown = errors.New("property row table structure unknown")
	// ErrEmptyRow ...
	ErrEmptyRow = errors.New("property row is empty")
)
View Source
var (
	// ErrInvalidPropertyName ...
	ErrInvalidPropertyName = errors.New("property name contains invalid characters")
	// ErrPropertyKeyBlank ...
	ErrPropertyKeyBlank = errors.New("property name must not be empty string")
)
View Source
var DefaultFuncMap = template.FuncMap{
	"ToLower":          strings.ToLower,
	"ToPlural":         pluralizer.Plural,
	"ToCamelCase":      toCamelCase,
	"ToLowerCamelCase": toLowerCamelCase,
	"ToFieldName":      toFieldName,
	"Type":             convertType,
	"Nullable":         isNillable,
}

DefaultFuncMap ...

Functions

func Generate

func Generate(seeds ...*Seed) error

Generate ...

func RelationshipName

func RelationshipName(name string) (string, error)

RelationshipName ...

Types

type Definer

type Definer interface {
	Options() ([]Option, error)
}

Definer defines options for a seed, for example to query additional data from a rest API before rendering a seed.

type Option

type Option func(*Seed)

Option is a functional option used for dynamically configuring an instance of Seed

func WithData

func WithData(data interface{}) Option

WithData ...

func WithDefinition

func WithDefinition(definer Definer) Option

WithDefinition ...

func WithDistinctData

func WithDistinctData(distinctData map[string]interface{}) Option

WithDistinctData ...

func WithDistinctSubpackages

func WithDistinctSubpackages(distinctSubpackages []string) Option

WithDistinctSubpackages ...

func WithFormatting

func WithFormatting(opts *imports.Options) Option

WithFormatting ...

func WithFuncMap

func WithFuncMap(funcMap template.FuncMap) Option

WithFuncMap ...

func WithOutputDirectory

func WithOutputDirectory(outputDirectory string) Option

WithOutputDirectory ...

func WithPackageImports

func WithPackageImports(packageImports []string) Option

WithPackageImports ...

func WithPackageName

func WithPackageName(packageName string) Option

WithPackageName ...

func WithTemplateMap

func WithTemplateMap(templateMap map[string]*template.Template) Option

WithTemplateMap ...

type Properties

type Properties []*Property

Properties ...

func (Properties) ConvertNillable

func (props Properties) ConvertNillable() (results Properties)

ConvertNillable ...

func (Properties) Merge

func (props Properties) Merge(p2 Properties, fn func(old, new Property) Property, includeDistinct bool) (final Properties)

Merge ...

type Property

type Property struct {
	// ParentName is a salesforce specific annotation used to indicate
	// the parent SObject in a parent:child relationship
	ParentName string
	// Name ...
	Name string
	// Documentation ...
	Documentation string
	// Type ...
	Type string
	// Tag ...
	Tag Tag
	// IsEmbedded indicated an embedded property such as
	//
	// type Foo struct {
	//     Bar
	// }
	//
	// This is used to provide response types for polymorphic foreign keys
	IsEmbedded bool
	// IsNillable ...
	IsNillable bool
}

Property represents a Field of a struct

func MergeOverrideNulls

func MergeOverrideNulls(old, new Property) Property

MergeOverrideNulls ...

func MergeProperty

func MergeProperty(old, new Property) Property

MergeProperty returns a new Property which overrides fields of oldProperty if conditions are met:

  1. newProperty.{{Field}} is not a zero value
  2. newProperty.{{Field}} != oldProperty.{{Field}}
  3. the case of merging a property.Tag is harder as a user may pass in a variety of values with an unknown intent for how they should be merged, for example a k,v of "": []string{} or a Tag{}. In these cases we will not override existing values with a zero value "" or []string{}
  4. the documentation property is concatenated if the old.Documentation != ""

func MergePropertyDocumentation

func MergePropertyDocumentation(old, new Property) Property

MergePropertyDocumentation merges only the Documentation field of two Propeprty objects returning the remaining properties of the initial Property argument unchanged

func NewChildProperty

func NewChildProperty(parentName string, relationshipName string) (Property, error)

NewChildProperty ...

func NewParentProperty

func NewParentProperty(parentName string, relationshipName string) Property

NewParentProperty ...

func NewProperty

func NewProperty(parent string, name string, dataType string, documentation string, tagName string, tagValues ...string) (Property, error)

NewProperty ...

func (*Property) IsRelation

func (p *Property) IsRelation() bool

IsRelation returns true if this property represents a parent or child relationship

type Seed

type Seed struct {
	// OutputDirectory ...
	OutputDirectory string
	// PackageName ...
	PackageName string
	// SubPackageName string
	SubPackageName string
	// PackageImports ...
	PackageImports []string
	// Definition ...
	Definition Definer
	// DistinctSubpackages indicates each Objects should produce
	// a subpackage in a directory that matches the pluralized lowercase
	// name of the distinct package
	DistinctSubpackages []string
	// Options ...
	Options []Option
	// Templates is map linking the absolute file path of a go template with an output filename
	// for its generated result
	// k:outputFileName: template
	TemplateMap map[string]*template.Template
	// Data is the data passed to a go template, configured via options
	Data interface{}
	// Distinct data is data but for DistinctSubpackages
	DistinctData map[string]interface{}
	//
	FormattingOptions *imports.Options
	//
	FuncMap template.FuncMap
}

Seed contains the structures needed to generate go source code

func From

func From(definer Definer) (*Seed, error)

From ...

type Source

type Source map[string][]byte

Source is the output of a generated Seed k:absoluteFilePath v:fileContents

func Render

func Render(seed *Seed) (Source, error)

Render executes all templates in seed.TemplateMap.

If DistinctObjects is not nil or empty then each template will be executed for each distinctObject and use DistinctData[distinctObject] for the template execution instead of seed.Data. This allows us to generate subpackages for each distinct object.

type Struct

type Struct struct {
	// DocumentationURL ...
	DocumentationURL string
	// ParentName ...
	ParentName string
	// Name ...
	Name string
	// Documentation ...
	Documentation string
	// Properties respresent a struct field
	Properties Properties
	// Dependencies are salesforce objects required by child or parent relationships of this struct
	Dependencies []string
	// IsPlolymorphicModel represents a model with two or more embeded structs representing a polymorphic relationship
	IsPolymorphicModel bool
}

Struct contains the components to generate a golang struct definition

func FromHTML

func FromHTML(doc *goquery.Document) (*Struct, error)

FromHTML converts the an HTML table representing a Salesforce Object to a codegen.Struct

func MergeAll

func MergeAll(old, new Struct) Struct

MergeAll ...

func MergeDocumentation

func MergeDocumentation(old, new Struct) Struct

MergeDocumentation merges only the .Documentation property of two structs

func (*Struct) RemoveRelations

func (s *Struct) RemoveRelations() *Struct

RemoveRelations returns a new copy of Struct with properties representing parent or child relations removed

func (*Struct) String

func (s *Struct) String() (string, error)

func (*Struct) Valid

func (s *Struct) Valid() bool

Valid ...

type Structs

type Structs []*Struct

Structs allows us to define utility methods on a group of Struct objects

func FromDescribe

func FromDescribe(desc *metadata.Describe, ignoreRelations bool) (structs Structs, err error)

FromDescribe converts the result of a /sobjects/{objectName}/describe request to a list of objects representing golang struct types annotated with their parent and child dependencies

func FromJSON

func FromJSON(structName string, structDocumentation string, JSON []byte) (results Structs, err error)

FromJSON converts a JSON payload to its requisite Struct components. Nested objects will be separated into discrete Struct objects rather then nesting them like Matt Holt's JSON2go website. StructDocumentation refers to Struct.Documentation and will append comments above the Struct.

func (Structs) ConvertNillable

func (s Structs) ConvertNillable() Structs

ConvertNillable ...

func (Structs) Dedupe

func (s Structs) Dedupe(overrideNulls bool) (results Structs)

Dedupe ...

func (Structs) DocSize

func (s Structs) DocSize() int

DocSize ...

func (Structs) Merge

func (s Structs) Merge(s2 Structs, fn func(old, new Struct) Struct, includeDistinct bool) Structs

Merge merges two []*Struct objects via the function fn.

This allows for merge behavior to change based on the source of the given []*Struct.

For example code derived from the reference documentation may only wish to contribute its struct.Documentation property where as code derived from the tooling/query api should contriubte its struct.Type .

func (Structs) RemoveRelations

func (s Structs) RemoveRelations() Structs

RemoveRelations returns a new set of structs with all polymorphic structs and all properties representing relationships removed

func (Structs) Sort

func (s Structs) Sort() Structs

Sort ...

func (Structs) Valid

func (s Structs) Valid() bool

Valid ...

type Tag

type Tag map[string][]string

Tag represents a set of struct property tags i.e. `json:"foo,omitempty" bson:"bar"`

func (Tag) Add

func (t Tag) Add(key string, value string)

Add extends the key-value pairing for a golang JSON struct tag

func (Tag) String

func (t Tag) String() string

String builds a Tag object into a string i.e. `json:"foo,omitempty" envconfig:"FOO"` note that WriteString() always returns a nil error so there is no point catching it

Jump to

Keyboard shortcuts

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