cosm/field_map

Source   Edit  

Macros

macro mapEnumFieldInput[T: enum](t: typedesc[T]; s: string;
                                 mappings: static FieldMappingPairs;
                                 normalizer: typed;
                                 templToCall, elseBody: untyped)

calls templToCall with the mapped enum field from s, emits elseBody if no such enum field exists

if normalizer is not nil, calls it for both key and the generated input names

Source   Edit  
macro mapEnumFieldOutput[T: enum](t: typedesc[T]; v: T;
                                  mappings: static FieldMappingPairs;
                                  normalizer: typed; templToCall: untyped)

calls templToCall with the mapped field name of v

if normalizer is not nil, calls it for both key and the generated input names

Source   Edit  
macro mapFieldInput[T: FieldedType](v: T; key: string;
                                    fields: static FieldMappingPairs;
                                    normalizer: typed;
                                    defaultInputs: static seq[NamePattern];
                                    templToCall, elseBody: untyped): untyped

calls templToCall with the address of the mapped field of v

if normalizer is not nil, calls it for both key and the generated input names

warning: currently requires importing std/importutils and calling privateAccess on the object type to work with private fields

Source   Edit  
macro mapInputVariantFieldName[T: VariantType](obj: typedesc[T]; key: string;
    fields: static FieldMappingPairs; normalizer: typed;
    defaultInputs: static seq[NamePattern];
    innerFieldTempl, variantFieldTempl, elseBody: untyped)
finds the variant field name for key based on variants: if key maps to a variant discriminator, calls variantFieldTempl with an identifier of the original field if key maps to a field inside a variant branch, calls innerFieldTempl with:
  1. the original field identifier of key
  2. the original identifier of the variant field
  3. the first acceptable value of the variant field for the branch that the inner field is in

otherwise, emits elseBody

if normalizer is not nil, calls it for both key and the generated input names

Source   Edit  

Templates

template mapFieldOutput[T: FieldedType](v: T; fields: static FieldMappingPairs;
                                        normalizer: typed;
                                        defaultOutput: static NamePattern;
                                        templToCall: untyped): untyped

calls templToCall with: 1. the mapped field address from v, 2. the mapped field name

if normalizer is not nil, calls it for both key and the generated input names

Source   Edit  
template mapInputVariantField[T: VariantType](obj: T; key: string;
    fields: static FieldMappingPairs; normalizer: typed;
    defaultInputs: static seq[NamePattern];
    innerFieldTempl, variantFieldTempl, elseBody: untyped)
finds the variant for key in obj: if key is a variant discriminator, calls variantFieldTempl with the address of key in obj if key is a field inside a variant branch, calls innerFieldTempl with:
  1. the address of key in obj
  2. the address of the variant field
  3. the first acceptable value of the variant field for the branch that the inner field is in

otherwise, emits elseBody

if normalizer is not nil, calls it for both key and the generated input names

Source   Edit