Documentation
¶
Index ¶
- func ConvertTimestamp(ts int64) string
- func FormatTime(t time.Time) string
- func FromNanos(ns int64) time.Time
- func IntToString(i int) string
- func NanosOrNil(ns int64) *time.Time
- func ParseTime(s string) (time.Time, error)
- func ParseTimestamp(s string) (int64, error)
- func Upper(s string) string
- type AdvancedMapper
- type Mapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertTimestamp ¶
ConvertTimestamp is a custom converter function for the using: directive It converts int64 (unix timestamp) to string
func FormatTime ¶
FormatTime is a custom converter for time.Time -> string
func NanosOrNil ¶
NanosOrNil is a custom converter for int64 (unix nanoseconds) -> *time.Time, nil for zero
func ParseTimestamp ¶
ParseTimestamp is a custom converter function for the using: directive It converts string (RFC3339) to int64 (unix timestamp)
Types ¶
type AdvancedMapper ¶
type AdvancedMapper interface {
// A bare name in using: refers to the package declaring the interface, and
// `alias.Name` to one of the file's imports. The full import path still works.
// field:CreatedAt using:"FormatTime"
MapUser(user *models.User) *protos.UserProto
// field:CreatedAt using:"github.com/ligustah/mapper/example.ParseTime"
MapUserReverse(user *protos.UserProto) (*models.User, error)
MapUserWithRoles(user *models.UserWithRoles) *protos.UserWithRolesProto
MapUserWithRolesReverse(user *protos.UserWithRolesProto) *models.UserWithRoles
MapArrayExample(src *models.ArrayExample) *protos.ArrayExampleProto
MapArrayExampleReverse(src *protos.ArrayExampleProto) *models.ArrayExample
// the elements are not assignable as they are, so every one of them is converted
MapArrayConvert(src *models.ArrayConvertExample) *protos.ArrayConvertExampleProto
// field:Timestamp using:"github.com/ligustah/mapper/example.ConvertTimestamp"
MapCustomConvert(src *models.CustomConvertExample) *protos.CustomConvertExampleProto
// field:Timestamp using:"github.com/ligustah/mapper/example.ParseTimestamp"
MapCustomConvertReverse(src *protos.CustomConvertExampleProto) (*models.CustomConvertExample, error)
MapNamedSlice(src *models.NamedSliceExample) *protos.NamedSliceExampleProto
MapNamedSliceReverse(src *protos.NamedSliceExampleProto) *models.NamedSliceExample
// Status needs no annotation, both sides are named string types declaring the
// same values, so the conversion is derived. Visibility is numbered on the proto
// side, so the values are listed explicitly. Either side of a pair can be spelled
// as the name of a constant or as its value.
// field:Visibility map:"VisibilityUnspecified=VisibilityProtoUnspecified,public=VisibilityProtoPublic,private=VisibilityProtoPrivate"
MapEnumExample(src *models.EnumExample) (*protos.EnumExampleProto, error)
// field:Visibility map:"VisibilityProtoUnspecified=VisibilityUnspecified,VisibilityProtoPublic=public,VisibilityProtoPrivate=private"
MapEnumExampleReverse(src *protos.EnumExampleProto) (*models.EnumExample, error)
// Phase needs no map: either. The two enums differ in their underlying types, so
// no conversion exists, but their constants pair up by name once the type name
// is stripped: PhaseRunning and TaskPhase_TASK_PHASE_RUNNING are both RUNNING.
// The generated switch keeps an error branch, so the method returns an error.
MapTask(src *models.Task) (*protos.TaskProto, error)
// In this direction TASK_PHASE_UNSPECIFIED has no counterpart. Generation warns
// about it, or fails under -strict-enums, and at runtime it reaches the error
// branch.
MapTaskReverse(src *protos.TaskProto) (*models.Task, error)
MapItem(item *models.Item) *protos.ItemProto
// Items is []*models.Item on one side and []*protos.ItemProto on the other, the
// shape a repeated message field takes, so the elements stay pointers
MapPointerSlice(src *models.PointerSliceExample) *protos.PointerSliceExampleProto
// Both converters take a named struct this package already names, from a package
// the generator loads on its own: one off a field, one off the whole input
// field:Summary from:"Snapshot" using:"github.com/ligustah/mapper/example/converters.SummarizeSnapshot"
// field:Digest from:"." using:"github.com/ligustah/mapper/example/converters.DigestExample"
MapSnapshotExample(src *models.SnapshotExample) *protos.SnapshotExampleProto
MapDetail(detail models.Detail) protos.DetailProto
// Detail is *models.Detail on one side and *protos.DetailProto on the other, but
// only a value method exists for the pair, so the generator wraps it in a nil
// check and a nil field stays nil. Item has a pointer method, that one is called
// as it is and guards nil itself.
MapOptionalExample(src *models.OptionalExample) *protos.OptionalExampleProto
// AtNs is read off a method of the source value. Owner and Level need no
// annotation: string -> []byte and conversions between integer types, named or
// not, are derived. Neither does OwnerId, a field name that has no exact match
// falls back to a case-insensitive one, so it finds OwnerID.
//
// The three optional fields are pointers on the model. A nil RetiredAt leaves
// RetiredAtNs zero rather than calling the method on nil, a nil Deadline stays
// nil and the timestamp converter's pointer is kept as it is, and a nil Note
// leaves the field empty without running the converter.
// field:AtNs from:"At" method:"UnixNano"
// field:RetiredAtNs from:"RetiredAt" method:"UnixNano"
// field:Note using:"Upper"
MapEvent(src *models.Event) *protos.EventProto
// A from: path through a pointer never dereferences nil. Because this method
// returns an error, the edge is read through its OrErr accessor and an edge that
// was not loaded is reported.
// field:CustomerName from:"Edges.Customer.Name"
MapOrder(src *models.Order) (*protos.OrderProto, error)
// Without an error return the pointer is nil-checked instead and the field keeps
// its zero value.
// field:CustomerName from:"Edges.Customer.Name"
MapOrderLoose(src *models.Order) *protos.OrderProto
// A method whose output type is also one of its inputs fills a builder: every
// `SetX(v)` setter returning the builder is a target field named X, and the
// caller sets whatever the source does not carry. Two ent conventions are
// folded in: SetNillableX takes over from SetX when the source or the converter
// hands back a pointer, and SetCustomer steps aside for SetCustomerID. Variadic
// adders such as AddTagIDs are not fields.
// field:At from:"AtNs" using:"FromNanos"
// field:RetiredAt from:"RetiredAtNs" using:"NanosOrNil"
// field:CustomerID from:"OwnerId"
// field:DeletedAt from:"-"
FillEvent(src *protos.EventProto, b *models.EventCreate) *models.EventCreate
// An optional setter, one with a SetNillableX twin, would still need a source
// or `from:"-"` like any other. mapper:skip-optional, on a method or on the
// whole interface, leaves those without a source field of their name unset.
// Here that is RetiredAt and DeletedAt, Note is still set from src.Note.
// mapper:skip-optional
// field:At from:"AtNs" using:"FromNanos"
// field:CustomerID from:"OwnerId"
FillEventSparse(src *protos.EventProto, b *models.EventCreate) *models.EventCreate
// A pointer source field of the setter's own name goes to SetNillableNote, so a
// nil Note leaves the column unset.
// mapper:skip-optional
// field:At from:"-"
// field:Owner from:"-"
// field:Level from:"-"
// field:CustomerID from:"-"
FillEventDraft(src *protos.EventDraftProto, b *models.EventCreate) *models.EventCreate
}
AdvancedMapper demonstrates all supported mapper features: - Basic field mapping with automatic type conversion (int -> int32) - Custom converters using the `using:` directive - Slice and map field mapping with automatic element conversion - Fixed-size array support ([4]byte -> [4]byte) - Named slice type support (StringSlice -> []string) - Error handling for converters that can fail mapper:generate
type Mapper ¶
type Mapper interface {
// field:Payload from:"."
// field:Transitions from:"-"
MapJob(job *models.Job) *protos.Job
// field:Time from:"CreatedAt"
MapTransition(transition *models.Transition) (*protos.Transition, error)
// field:ID from:"-"
// field:JobID from:"-"
// field:R from:"-"
// field:L from:"-"
// field:CreatedAt from:"Time"
TransitionProtoToModel(transition *protos.Transition) (*models.Transition, error)
// field:ContentType from:"DataType"
// field:Body from:"Data"
MapPayload(job *models.Job) *protos.Data
// field:DataType from:"Payload.ContentType"
// field:Data from:"Payload.Body"
// field:UpdatedAt from:"-"
// field:CronID from:"-"
// field:R from:"-"
// field:L from:"-"
MapJobReverse(job *protos.Job) *models.Job
}
mapper:generate
Directories
¶
| Path | Synopsis |
|---|---|
|
Package converters holds converters referenced from using: directives.
|
Package converters holds converters referenced from using: directives. |