|
| 1 | +# ocrline |
| 2 | + |
| 3 | +[](https://github.com/karolusz/ocrline/actions/workflows/ci.yml) |
| 4 | +[](https://codecov.io/gh/karolusz/ocrline) |
| 5 | +[](https://pkg.go.dev/github.com/karolusz/ocrline) |
| 6 | +[](https://goreportcard.com/report/github.com/karolusz/ocrline) |
| 7 | + |
| 8 | +Marshal and unmarshal Go structs to and from fixed-width line formats using struct tags. |
| 9 | + |
| 10 | +Built for Scandinavian payment file formats (Nets AvtaleGiro, OCR Giro, Bankgirot AutoGiro) but works with any 80-character (or custom width) fixed-position record format. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +``` |
| 15 | +go get github.com/karolusz/ocrline |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Define structs with `ocr` tags specifying 0-based byte positions (Go slice convention): |
| 21 | + |
| 22 | +```go |
| 23 | +type TransmissionStart struct { |
| 24 | + FormatCode string `ocr:"0:2"` |
| 25 | + ServiceCode string `ocr:"2:4"` |
| 26 | + TransactionType string `ocr:"4:6"` |
| 27 | + RecordType int `ocr:"6:8"` |
| 28 | + DataTransmitter string `ocr:"8:16"` |
| 29 | + TransmissionNo string `ocr:"16:23"` |
| 30 | + DataRecipient string `ocr:"23:31"` |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Unmarshal |
| 35 | + |
| 36 | +```go |
| 37 | +line := "NY000010555555551000081000080800000000000000000000000000000000000000000000000000" |
| 38 | + |
| 39 | +var record TransmissionStart |
| 40 | +if err := ocrline.Unmarshal(line, &record); err != nil { |
| 41 | + log.Fatal(err) |
| 42 | +} |
| 43 | + |
| 44 | +fmt.Println(record.FormatCode) // "NY" |
| 45 | +fmt.Println(record.DataTransmitter) // "55555555" |
| 46 | +fmt.Println(record.RecordType) // 10 |
| 47 | +``` |
| 48 | + |
| 49 | +### Marshal |
| 50 | + |
| 51 | +```go |
| 52 | +record := TransmissionStart{ |
| 53 | + FormatCode: "NY", |
| 54 | + ServiceCode: "00", |
| 55 | + TransactionType: "00", |
| 56 | + RecordType: 10, |
| 57 | + DataTransmitter: "55555555", |
| 58 | + TransmissionNo: "1000081", |
| 59 | + DataRecipient: "00008080", |
| 60 | +} |
| 61 | + |
| 62 | +line, err := ocrline.Marshal(record) |
| 63 | +// "NY000010555555551000081000080800000000000000000000000000000000000000000000000000" |
| 64 | +``` |
| 65 | + |
| 66 | +## Tag Syntax |
| 67 | + |
| 68 | +``` |
| 69 | +ocr:"start:end" |
| 70 | +ocr:"start:end,option,option,..." |
| 71 | +``` |
| 72 | + |
| 73 | +Positions are **0-based, exclusive end** (like Go slices). `ocr:"0:2"` reads `line[0:2]`. |
| 74 | + |
| 75 | +### Options |
| 76 | + |
| 77 | +| Option | Description | |
| 78 | +|--------|-------------| |
| 79 | +| `align-left` | Left-align the value in the field | |
| 80 | +| `align-right` | Right-align the value in the field | |
| 81 | +| `pad-zero` | Pad with `'0'` characters | |
| 82 | +| `pad-space` | Pad with `' '` characters | |
| 83 | +| `omitempty` | If zero-valued, fill with padding instead of the value | |
| 84 | + |
| 85 | +### Type Defaults |
| 86 | + |
| 87 | +| Go Type | Default Alignment | Default Padding | |
| 88 | +|---------|-------------------|-----------------| |
| 89 | +| `string` | left | space | |
| 90 | +| `int`, `int8`..`int64` | right | zero | |
| 91 | +| `uint`, `uint8`..`uint64` | right | zero | |
| 92 | +| `bool` | right | zero | |
| 93 | +| `*T` | inherits from `T` | inherits from `T` | |
| 94 | + |
| 95 | +Named types follow their underlying type: `type Numeric string` gets string defaults. |
| 96 | + |
| 97 | +## Struct Composition |
| 98 | + |
| 99 | +Embedded structs are flattened, just like `encoding/json`: |
| 100 | + |
| 101 | +```go |
| 102 | +type RecordBase struct { |
| 103 | + FormatCode string `ocr:"0:2"` |
| 104 | + ServiceCode string `ocr:"2:4"` |
| 105 | + RecordType int `ocr:"6:8"` |
| 106 | +} |
| 107 | + |
| 108 | +type PaymentRecord struct { |
| 109 | + RecordBase |
| 110 | + PayerNumber string `ocr:"15:31"` |
| 111 | + Amount int `ocr:"31:43"` |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Embedded pointer structs (`*RecordBase`) are also supported. On unmarshal, nil pointers are auto-allocated. On marshal, nil embedded pointers are skipped (gaps filled with default). |
| 116 | + |
| 117 | +## Gap Filling |
| 118 | + |
| 119 | +Byte positions not covered by any field are filled with `'0'` by default. To fill specific gaps with a different character, implement the `Filler` interface: |
| 120 | + |
| 121 | +```go |
| 122 | +func (r PaymentRecord) OCRFill() []ocrline.Fill { |
| 123 | + return []ocrline.Fill{ |
| 124 | + {Start: 8, End: 15, Char: ' '}, // positions 8-14 filled with spaces |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +## Custom Types |
| 130 | + |
| 131 | +Implement `Marshaler` and `Unmarshaler` for full control over field serialization: |
| 132 | + |
| 133 | +```go |
| 134 | +type ServiceCode string |
| 135 | + |
| 136 | +func (s ServiceCode) MarshalOCR() (string, error) { |
| 137 | + return string(s), nil |
| 138 | +} |
| 139 | + |
| 140 | +func (s *ServiceCode) UnmarshalOCR(data string) error { |
| 141 | + *s = ServiceCode(strings.TrimSpace(data)) |
| 142 | + return nil |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Line Width |
| 147 | + |
| 148 | +`Marshal` outputs 80 characters by default. Use `MarshalWidth` for other widths: |
| 149 | + |
| 150 | +```go |
| 151 | +line, err := ocrline.MarshalWidth(record, 120) // 120-char line |
| 152 | +line, err := ocrline.MarshalWidth(record, 0) // no padding, exact width of rightmost field |
| 153 | +``` |
| 154 | + |
| 155 | +## Validation and Caching |
| 156 | + |
| 157 | +Struct metadata is parsed, validated, and cached per type on first use (same pattern as `encoding/json`). Subsequent calls for the same struct type have zero reflection overhead for tag parsing. |
| 158 | + |
| 159 | +Validated once per type: |
| 160 | + |
| 161 | +- **Tag syntax** - start and end must be integers, `start >= 0`, `end > start` |
| 162 | +- **Overlapping fields** - two fields covering the same positions are rejected with `*OverlapError` |
| 163 | +- **Invalid tags** - malformed `ocr` tags produce `*TagError` |
| 164 | + |
| 165 | +Validated per call: |
| 166 | + |
| 167 | +- **Out-of-range fields** - on unmarshal, fields exceeding the line length produce `*UnmarshalRangeError` |
| 168 | +- **Unexported fields** are silently skipped (same as `encoding/json`) |
| 169 | + |
| 170 | +## API |
| 171 | + |
| 172 | +```go |
| 173 | +func Marshal(v any) (string, error) |
| 174 | +func MarshalWidth(v any, width int) (string, error) |
| 175 | +func Unmarshal(line string, v any) error |
| 176 | +``` |
| 177 | + |
| 178 | +### Interfaces |
| 179 | + |
| 180 | +```go |
| 181 | +type Marshaler interface { |
| 182 | + MarshalOCR() (string, error) |
| 183 | +} |
| 184 | + |
| 185 | +type Unmarshaler interface { |
| 186 | + UnmarshalOCR(data string) error |
| 187 | +} |
| 188 | + |
| 189 | +type Filler interface { |
| 190 | + OCRFill() []Fill |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Supported Formats |
| 195 | + |
| 196 | +The library is format-agnostic. It has been designed with these formats in mind: |
| 197 | + |
| 198 | +| Format | Country | Provider | Line Width | |
| 199 | +|--------|---------|----------|------------| |
| 200 | +| AvtaleGiro | Norway | Nets | 80 | |
| 201 | +| OCR Giro | Norway | Nets | 80 | |
| 202 | +| AutoGiro | Sweden | Bankgirot | 80 | |
| 203 | + |
| 204 | +## License |
| 205 | + |
| 206 | +MIT |
0 commit comments