Kelmscott/docs

Reference

Version 2.4.1. Anything not listed here is not part of the public API.

Layout and Field

NameField name. Must be unique within a layout; used in lookups and errors only.
WidthField width in bytes. Must be greater than zero.
TypeOne of the field types below.
ScaleImplied decimal places. Decimal only.
FormatReference layout string. Timestamp and Date only.
PadSpaceRight (default), SpaceLeft, ZeroLeft.
OptionalAn all-pad field decodes to the zero value instead of an error.

Layout.RecordLength() returns the sum of the widths. Layout.Validate() reports a duplicate name, a zero width, a scale on a non-decimal field, or a missing format on a temporal field. Both readers and writers call Validate on construction, so an invalid layout fails before any I/O happens.

Field types

TextBytes decoded with the reader's encoding, padding trimmed according to Pad.
IntegerOptionally signed decimal digits. An overpunched sign in the final byte is accepted.
DecimalDigits with an implied decimal point at Scale. Exact; never a float.
PackedPacked decimal, two digits per byte, sign in the low nibble.
DateCalendar date, no time zone.
TimestampDate and time. UTC unless the format carries an offset.
RawBytes, untouched. Padding is not trimmed and no encoding is applied.

Reader

NewReader(io.Reader, Layout) *ReaderConstructs a reader. Panics only if the layout is nil.
Next() boolDecodes the next record. False at EOF and on the first error.
Record() RecordThe record decoded by the last Next. Invalid after the following call.
Err() errorThe first error encountered, or nil at a clean EOF.
Offset() int64Byte offset of the start of the current record.
EncodingASCII (default), Latin1, UTF8.
TerminatorNone (default), LF, CRLF, Auto.
BufferSizeRead buffer in bytes, default 262144, rounded up to whole records.
OnErrorfunc(*PositionError) Action. Return Skip or Stop.

Record

Text(name) (string, error)Decoded text. Error if the field is not Text.
Int(name) (int64, error)Integer value. Error on any non-digit byte in a required field.
Decimal(name) (Decimal, error)Exact decimal with the declared scale.
Time(name) (time.Time, error)Parsed with the field's format.
Bytes(name) ([]byte, error)The raw field bytes. Aliases the reader's buffer.
IsBlank(name) boolTrue if the field is entirely padding.
Clone() RecordAn independent copy, safe to retain.

Writer

NewWriter(io.Writer, Layout) *WriterConstructs a buffered writer.
Write(Values) errorEncodes one record. A missing required field is an error.
Flush() errorWrites buffered records to the underlying writer.
Close() errorFlushes and marks the writer used. Must be checked.
TerminatorMatches the reader's; default None.

Errors

ErrShortRecordThe stream ended part-way through a record.
ErrFieldSyntaxThe bytes of a field do not match its declared type.
ErrFieldOverflowA value written does not fit its declared width.
ErrLayoutThe layout is self-inconsistent; returned before any I/O.
ErrEncodingA byte sequence is not valid in the declared encoding.
PositionErrorWraps any of the above with Offset, Record, Field.

All sentinel errors are comparable with errors.Is through the wrapping PositionError. New sentinels may be added in a minor release; existing ones will not be removed before 3.0.