inpututil

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldSelectors = "selectors"
	FieldUpdates   = "updates"
	FieldCount     = "count"
	FieldValue     = "value"
	FieldPredicate = "predicate"
)

Variables

This section is empty.

Functions

func GenericCreateAPIFields

func GenericCreateAPIFields[T any](
	wrapperName string, nested input.APIFields,
) input.APIFields

GenericCreateAPIFields creates input.APIFields for a create request.

Parameters:

  • wrapperName: The name of the wrapper field.
  • nested: The nested input.APIFields.

Returns:

  • input.APIFields: The input.APIFields.

func GenericDeleteAPIFields

func GenericDeleteAPIFields(
	selectableAPIFields input.APIFields,
	predicates map[string]apidb.Predicates,
) input.APIFields

GenericDeleteAPIFields creates input.APIFields for a delete request.

func GenericGetAPIFields

func GenericGetAPIFields(
	apiFields input.APIFields,
	predicates map[string]apidb.Predicates,
	orderable []string,
) input.APIFields

GenericGetAPIFields creates input.APIFields for a get request.

func GenericUpdateAPIFields

func GenericUpdateAPIFields(
	selectableAPIFields input.APIFields,
	predicates map[string]apidb.Predicates,
	updatableAPIFields input.APIFields,
) input.APIFields

GenericUpdateAPIFields creates input.APIFields for an update request.

func GetAPIToDBMap

func GetAPIToDBMap(
	apiFields []input.APIField,
	tableName string,
) map[string]apidb.APIToDBField

GetAPIToDBMap builds a mapping from API field names to DB fields.

Example:

apiFields := []input.APIField{
    {
        APIName:  "name",
        DBColumn: "name",
    },
}
tableName := "users"

Output:

mapping := map[string]DBField{
    "name": {
        Table:  "users",
        Column: "name",
    },
}

func InputHandlerFromAPIFields

func InputHandlerFromAPIFields[Input any](
	inputFactoryFn func() *Input,
	apiFields input.APIFields,
	conversionRules map[string]func(any) any,
	customRules map[string]func(any) error,
) endpoint.InputHandler[Input]

func MapEntitiesToOutput

func MapEntitiesToOutput(
	entities []any, outputFactoryFn func() any, apiToDBFields APIToDBFields,
) ([]any, error)

MapEntitiesToOutput maps fields from entities to an output struct using the provided APIToDBFields. It expects the entities to be structs (or pointer to struct) and the output to be a pointer to a struct. The mapping is done by matching the entity’s "db" tag with the colum in the APIToDBFields value, and the output’s JSON tag with the APIToDBFields key.

Parameters:

  • entities: The entities to map.
  • outputFactoryFn: A function that creates the output struct.
  • apiToDBFields: The mapping between API fields and database columns.

Returns:

  • []any: The mapped outputs.
  • error: An error if the mapping fails.

func MapInputToEntity

func MapInputToEntity[Entity any](
	input any, entity *Entity, apiToDBFields APIToDBFields,
) (*Entity, error)

MapInputToEntity is a generic helper function that maps fields from an input struct to an entity struct using the provided APIToDBFields. It expects the input to be a struct (or pointer to struct) and the entity to be a pointer to a struct. It uses the JSON tag of the input and the "db" tag of the entity.

Parameters:

  • input: The input struct to map.
  • entity: The entity struct to map to.
  • apiToDBFields: The mapping between API fields and database columns.

Returns:

  • *Entity: The mapped entity.
  • error: An error if the mapping fails.

func MatchStructAPIFIelds

func MatchStructAPIFIelds(
	t reflect.Type, allFields input.APIFields, depth int,
) input.APIFields

MatchStructAPIFIelds processes the given type recursively.

func MustMapEntitiesToOutput

func MustMapEntitiesToOutput(
	entities []any, outputFactoryFn func() any, apiToDBFields APIToDBFields,
) []any

MapEntityToOutput maps fields from entities to an output struct using the provided APIToDBFields. It panics if the mapping fails.

Parameters:

  • entities: The entities to map.
  • outputFactoryFn: A function that creates the output struct.
  • apiToDBFields: The mapping between API fields and database columns.

Returns:

  • []any: The mapped output struct.

func MustMapInputToEntity

func MustMapInputToEntity[Entity any](
	input any, entity *Entity, apiToDBFields APIToDBFields,
) *Entity

MustMapInputToEntity is a generic helper function that maps fields from an input struct to an entity struct using the provided APIToDBFields. It panics if the mapping fails.

Parameters:

  • input: The input struct to map.
  • entity: The entity struct to map to.
  • apiToDBFields: The mapping between API fields and database columns.

Returns:

  • *Entity: The mapped entity.

func MustMatchStructAPIFields

func MustMatchStructAPIFields[Struct any](
	allFields input.APIFields,
) input.APIFields

MustMatchStructAPIFields is a generic function that returns a slice of input.APIFields for any struct type. For each field, if an input.APIField with a matching JSON name exists in allFields, that input.APIField is used. If the struct field hasa "validate" tag, its rules override any validations from the input.APIField. Additionally, if a field is tagged with `required:"true"`, then it must be found in allFields. It will try to find the "required" and "type" tags and these will also override input.APIField settings.

Parameters:
	- Struct: The struct type.
	- allFields: The input.APIFields to match against.

Returns:
	- input.APIFields: The matched input.APIFields.

Types

type APIToDBFields

type APIToDBFields map[string]apidb.APIToDBField

APIToDBFields maps API fields to database fields.

Jump to

Keyboard shortcuts

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