transform

package
v5.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: Apache-2.0 Imports: 16 Imported by: 182

Documentation

Overview

Package transform defines functions that modify plugin.Column values.

For a given column, data comes from a hydrate call either implicitly (from List or Get), or explicitly if you declared a hydrate function. In either case the result is a hydrate item that may need to be transformed in order to populate the column.

The transform functions in this package handle common needs. You can also use transform.From to call your own custom transform function.

You can form chains of transform functions, but the transform chain must begin with a transform.FromXXX function:

{
	Name: "created_date",
	Type: proto.ColumnType_TIMESTAMP,
	Transform: transform.FromField("Domain.CreatedDate").Transform(whoisDateToTimestamp).NullIfZero(),
	Description: "Date when the domain was first registered."
},

DefaultTransform

To transform all columns of a plugin in the same way:

func Plugin(ctx context.Context) *plugin.Plugin {
	p := &plugin.Plugin{
		Name: "steampipe-plugin-hackernews",
		DefaultTransform: transform.FromJSONTag().NullIfZero(),
	...
}

Examples:

FromConstant

To populate a column with a constant value:

// standard gcp columns
{
	Name:        "location",
	Description: ColumnDescriptionLocation,
	Type:        proto.ColumnType_STRING,
	Transform:   transform.FromConstant("global"),
},

Examples:

FromField

To extract a field from a property path in a Go struct:

{
	Name: "id",
	Type: proto.ColumnType_STRING,
	Transform: transform.FromField("Comment.ID"), Description: "ID of the comment."},
}

Examples:

FromValue

To use the raw value of the hydrate item (e.g., a string), when it is the value intended for the column.

{
	Name:        "cloud_environment",
	Type:        proto.ColumnType_STRING,
	Hydrate:     getCloudEnvironment,
	Description: ColumnDescriptionCloudEnvironment,
	Transform:   transform.FromValue(),
},

Examples:

FromCamel

To convert a camel-cased Go field name to a snake-cased column name:

{
	Name:        "account",
	Type:        proto.ColumnType_STRING,
	Hydrate:     plugin.HydrateFunc(getCommonColumns).WithCache(),
	Description: "The Snowflake account ID.",
	Transform:   transform.FromCamel(),
},

Examples:

FromGo

To convert a camel-cased Go field name to a snake-cased column name, while preserving common acronyms such as ASCII or HTTP:

{
	Name:        "user_arn",
	Description: "The Amazon Resource Name (ARN) of the user.",
	Type:        proto.ColumnType_STRING,
	Transform:   transform.FromGo(),
},

Examples:

FromTag

To generate a value from a field of a Go struct with a tag 'tagName:value'

Examples:

  • none

FromJSONTag

To generate a value from a field of a Go struct with a tag 'json:value'

{
	Name: "adversary_ids",
	Type: proto.ColumnType_JSON,
	Transform: transform.FromJSONTag()
},

Examples:

From

To generate a value by calling 'transformFunc'

Examples:

  • none

FromP

To generate a value by calling 'transformFunc', passing param.

{
	Name: "client_delete_prohibited",
	Type: proto.ColumnType_BOOL,
	Transform: transform.FromP(statusToBool, "clientdeleteprohibited"),
	Description: "This status code tells your domain's registry to reject requests to delete the domain."
},

Examples:

Chained function: Transform

To apply an arbitrary transform to the data (specified by 'transformFunc').

{
	Name: "created_date",
	Type: proto.ColumnType_TIMESTAMP,
	Transform: transform.FromField("Domain.CreatedDate").Transform(whoisDateToTimestamp).NullIfZero(),
	Description: "Date when the domain was first registered."
},

Examples:

Chained function: NullIfEqual

To return nil if the input value equals the transform param

{
	Name:        "access_key_1_last_rotated",
	Description: "The date and time when the user's access key was created or last changed.",
	Type:        proto.ColumnType_TIMESTAMP,
	Transform:   transform.FromGo().NullIfEqual("N/A"),
},

Examples:

Chained function: NullIfZero

To return nil of the input value equals the zero value of its type.

{
	Name: "domain_id",
	Type: proto.ColumnType_STRING,
	Transform: transform.FromField("Domain.ID").NullIfZero(),
	Description: "Unique identifier for the domain."
},

Examples:

Transform package provides the ability to transform data from APIs. It contains transform functions which can be chained together to get the desired values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstantValue

func ConstantValue(_ context.Context, d *TransformData) (interface{}, error)

ConstantValue is intended for the start of a transform chain This returns the value passed as d.Param

func EnsureStringArray

func EnsureStringArray(_ context.Context, d *TransformData) (interface{}, error)

EnsureStringArray convert the input value from transform data into a string array

func FieldValue

func FieldValue(_ context.Context, d *TransformData) (interface{}, error)

FieldValue function is intended for the start of a transform chain. This returns a field value of either the hydrate call result (if present) or the root item if not the 'Param' is a list of field names - Each field is tried in turn. If the property does not exist or is nil, the next field is tried

func FieldValueCamelCase

func FieldValueCamelCase(ctx context.Context, d *TransformData) (interface{}, error)

FieldValueCamelCase is intended for the start of a transform chain This converts the column name to camel case and call FieldValue

func FieldValueGo

func FieldValueGo(ctx context.Context, d *TransformData) (interface{}, error)

FieldValueGo is intended for the start of a transform chain This converts the column name to camel case, with common initialisms upper case, and call FieldValue

func FieldValueTag

func FieldValueTag(ctx context.Context, d *TransformData) (interface{}, error)

FieldValueTag is intended for the start of a transform chain This finds the data value with the tag matching the column name

func MatrixItemValue

func MatrixItemValue(ctx context.Context, d *TransformData) (interface{}, error)

MatrixItemValue is intended for the start of a transform chain This retrieves a value from the matrix item, using the param from transform data as a key

func MethodValue

func MethodValue(_ context.Context, d *TransformData) (interface{}, error)

MethodValue function takes the transform data and invokes specified method on the hydrate item

func NullIfEmptySliceValue added in v5.1.0

func NullIfEmptySliceValue(_ context.Context, d *TransformData) (interface{}, error)

NullIfEmptySliceValue returns nil if the input Value is an empty slice/array

func NullIfEqualParam

func NullIfEqualParam(_ context.Context, d *TransformData) (interface{}, error)

NullIfEqualParam returns nil if the input Value equals the transform param

func NullIfZeroValue

func NullIfZeroValue(_ context.Context, d *TransformData) (interface{}, error)

NullIfZeroValue takes the transform data and returns nil if the input value equals the zero value of its type

func QualValue

func QualValue(ctx context.Context, d *TransformData) (interface{}, error)

QualValue takes the column name from the transform data param and retrieves any quals for it If the quals is a single equals quals it returns it If there are any other quals and error is returned

func RawValue

func RawValue(_ context.Context, d *TransformData) (interface{}, error)

RawValue is intended for the start of a transform chain This returns the whole hydrate item as it is

func StringArrayToMap

func StringArrayToMap(_ context.Context, d *TransformData) (interface{}, error)

StringArrayToMap converts a string array to a map where the keys are the array elements

func ToBool

func ToBool(_ context.Context, d *TransformData) (interface{}, error)

ToBool converts the (string) value to a bool This returns nil if value is not a string

func ToDouble

func ToDouble(_ context.Context, d *TransformData) (interface{}, error)

ToDouble convert the value from transform data to float64

func ToInt

func ToInt(_ context.Context, d *TransformData) (interface{}, error)

ToInt convert the value from transform data to an int64

func ToLower

func ToLower(_ context.Context, d *TransformData) (interface{}, error)

ToLower converts the (string or *string) value to lower case returns unaltered value if value is not a string

func ToString

func ToString(_ context.Context, d *TransformData) (interface{}, error)

ToString convert the value from transform data to a string

func ToUpper

func ToUpper(_ context.Context, d *TransformData) (interface{}, error)

ToUpper converts the (string or *string) value to upper case, returns unaltered value if value from the transform data is not a string

func UnixMsToTimestamp

func UnixMsToTimestamp(_ context.Context, d *TransformData) (interface{}, error)

UnixMsToTimestamp convert unix time in milliseconds to go time object (which will later be converted to RFC3339 format by the FDW)

func UnixToTimestamp

func UnixToTimestamp(_ context.Context, d *TransformData) (interface{}, error)

UnixToTimestamp convert unix time format to go time object (which will later be converted to RFC3339 format by the FDW)

func UnmarshalYAML

func UnmarshalYAML(_ context.Context, d *TransformData) (interface{}, error)

UnmarshalYAML parse the yaml-encoded data and return the result

Types

type ColumnTransforms

type ColumnTransforms struct {
	// a list of transforms to apply to the data
	Transforms []*TransformCall
}

ColumnTransforms struct defines the data transforms required to map from a JSON value to a column value

func From

func From(transformFunc TransformFunc) *ColumnTransforms

From generate a value by calling 'transformFunc'

func FromCamel

func FromCamel() *ColumnTransforms

FromCamel generates a value by converting the given field name to camel case and retrieving from the source item

func FromConstant

func FromConstant(value interface{}) *ColumnTransforms

FromConstant returns a constant value (specified by 'param')

func FromField

func FromField(fieldNames ...string) *ColumnTransforms

FromField generates a value by retrieving a field or a set of fields from the source item

func FromGo

func FromGo() *ColumnTransforms

FromGo generates a value by converting the given field name to camel case and retrieving from the source item

func FromJSONTag

func FromJSONTag() *ColumnTransforms

FromJSONTag generates a value by finding a struct property with the json tag matching the column name

func FromMatrixItem

func FromMatrixItem(key string) *ColumnTransforms

FromMatrixItem takes key from transform data and generates the value from Matrix Items

func FromMethod

func FromMethod(methodName string) *ColumnTransforms

FromMethod invokes a function on the hydrate item coming from transform data

func FromP

func FromP(transformFunc TransformFunc, param interface{}) *ColumnTransforms

FromP generates a value by calling 'transformFunc' passing param

func FromQual

func FromQual(qual string) *ColumnTransforms

FromQual takes the specific column and generates it's values from key column quals

func FromTag

func FromTag(tagName string) *ColumnTransforms

FromTag generates a value by finding a struct property with the tag 'tagName' matching the column name

func FromValue

func FromValue() *ColumnTransforms

FromValue generates a value by returning the raw hydrate item

func (*ColumnTransforms) Execute

func (t *ColumnTransforms) Execute(ctx context.Context, transformData *TransformData) (interface{}, error)

func (*ColumnTransforms) NullIfEmptySlice added in v5.1.0

func (t *ColumnTransforms) NullIfEmptySlice() *ColumnTransforms

NullIfEmptySlice returns nil if the input value is an empty slice/array

func (*ColumnTransforms) NullIfEqual

func (t *ColumnTransforms) NullIfEqual(nullValue interface{}) *ColumnTransforms

NullIfEqual returns nil if the input Value equals the transform param

func (*ColumnTransforms) NullIfZero

func (t *ColumnTransforms) NullIfZero() *ColumnTransforms

NullIfZero returns nil if the input value equals the zero value of its type

func (*ColumnTransforms) Transform

func (t *ColumnTransforms) Transform(transformFunc TransformFunc) *ColumnTransforms

Transform function applies an arbitrary transform to the data (specified by 'transformFunc')

func (*ColumnTransforms) TransformP

func (t *ColumnTransforms) TransformP(transformFunc TransformFunc, param interface{}) *ColumnTransforms

TransformP function applies an arbitrary transform to the data, passing a parameter

type ForceImport

type ForceImport string

ForceImport is a mechanism to ensure godoc can reference all required packages

type GetSourceFieldFunc

type GetSourceFieldFunc func(interface{}) string

type TransformCall

type TransformCall struct {
	Transform TransformFunc
	Param     interface{}
}

TransformCall is a transform function and parameter to invoke it with

func (*TransformCall) Execute

func (tr *TransformCall) Execute(ctx context.Context, value interface{}, transformData *TransformData) (transformedValue interface{}, err error)

Execute function executes a transform call

type TransformData

type TransformData struct {
	// an optional parameter
	Param interface{}
	// the value to be transformed
	Value interface{}
	// a data object containing the source data for this column
	HydrateItem interface{}
	// all hydrate results
	HydrateResults map[string]interface{}
	// the column this transform is generating
	ColumnName string
	// the 'matrix item' associated with this row
	MatrixItem map[string]interface{}
	// KeyColumnQuals will be populated with the quals as a map of column name to an array of quals for that column
	KeyColumnQuals map[string]quals.QualSlice
}

TransformData is the input to a transform function.

type TransformFunc

type TransformFunc func(context.Context, *TransformData) (interface{}, error)

TransformFunc is a function to transform a data value from the api value to a column value parameters are: value, parent json object, param returns the transformed HydrateItem

Jump to

Keyboard shortcuts

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