Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ianlopshire/go-fixedwidth
Encoding and decoding for fixed-width formatted data
https://github.com/ianlopshire/go-fixedwidth
decoding encoding fixed-width
Last synced: about 2 months ago
JSON representation
Encoding and decoding for fixed-width formatted data
- Host: GitHub
- URL: https://github.com/ianlopshire/go-fixedwidth
- Owner: ianlopshire
- License: mit
- Created: 2017-11-15T21:05:44.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-08T23:36:46.000Z (11 months ago)
- Last Synced: 2024-07-31T20:53:11.076Z (5 months ago)
- Topics: decoding, encoding, fixed-width
- Language: Go
- Homepage: http://godoc.org/github.com/ianlopshire/go-fixedwidth
- Size: 89.8 KB
- Stars: 80
- Watchers: 4
- Forks: 32
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - go-fixedwidth - Fixed-width text formatting (encoder/decoder with reflection). (Text Processing / Formatters)
- awesome-go - go-fixedwidth - Encoding and decoding for fixed-width formatted data - ★ 14 (Text Processing)
- awesome-go-extra - go-fixedwidth - width formatted data|64|26|4|2017-11-15T21:05:44Z|2022-07-29T18:07:42Z| (Bot Building / Formatters)
README
# fixedwidth [![GoDoc](https://godoc.org/github.com/ianlopshire/go-fixedwidth?status.svg)](http://godoc.org/github.com/ianlopshire/go-fixedwidth) [![Report card](https://goreportcard.com/badge/github.com/ianlopshire/go-fixedwidth)](https://goreportcard.com/report/github.com/ianlopshire/go-fixedwidth) [![Go Cover](http://gocover.io/_badge/github.com/ianlopshire/go-fixedwidth)](http://gocover.io/github.com/ianlopshire/go-fixedwidth)
Package fixedwidth provides encoding and decoding for fixed-width formatted Data.
`go get github.com/ianlopshire/go-fixedwidth`
## Usage
### Struct Tags
The struct tag schema schema used by fixedwidth is: `fixed:"{startPos},{endPos},[{alignment},[{padChar}]]"`[1](#f1).
The `startPos` and `endPos` arguments control the position within a line. `startPos` and `endPos` must both be positive integers greater than 0. Positions start at 1. The interval is inclusive.
The `alignment` argument controls the alignment of the value within it's interval. The valid options are `default`[2](#f2), `right`, `left`, and `none`. The `alignment` is optional and can be omitted.
The `padChar` argument controls the character that will be used to pad any empty characters in the interval after writing the value. The default padding character is a space. The `padChar` is optional and can be omitted.
Fields without tags are ignored.
### Encode
```go
// define some data to encode
people := []struct {
ID int `fixed:"1,5"`
FirstName string `fixed:"6,15"`
LastName string `fixed:"16,25"`
Grade float64 `fixed:"26,30"`
Age uint `fixed:"31,33"`
Alive bool `fixed:"34,39"`
}{
{1, "Ian", "Lopshire", 99.5, 20, true},
}data, err := Marshal(people)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", data)
// Output:
// 1 Ian Lopshire 99.5020 true
```### Decode
```go
// define the format
var people []struct {
ID int `fixed:"1,5"`
FirstName string `fixed:"6,15"`
LastName string `fixed:"16,25"`
Grade float64 `fixed:"26,30"`
Age uint `fixed:"31,33"`
Alive bool `fixed:"34,39"`
Github bool `fixed:"40,41"`
}// define some fixed-with data to parse
data := []byte("" +
"1 Ian Lopshire 99.50 20 false f" + "\n" +
"2 John Doe 89.50 21 true t" + "\n" +
"3 Jane Doe 79.50 22 false F" + "\n" +
"4 Ann Carraway 79.59 23 false T" + "\n")err := Unmarshal(data, &people)
if err != nil {
log.Fatal(err)
}fmt.Printf("%+v\n", people[0])
fmt.Printf("%+v\n", people[1])
fmt.Printf("%+v\n", people[2])
fmt.Printf("%+v\n", people[3])
// Output:
//{ID:1 FirstName:Ian LastName:Lopshire Grade:99.5 Age:20 Alive:false Github:false}
//{ID:2 FirstName:John LastName:Doe Grade:89.5 Age:21 Alive:true Github:true}
//{ID:3 FirstName:Jane LastName:Doe Grade:79.5 Age:22 Alive:false Github:false}
//{ID:4 FirstName:Ann LastName:Carraway Grade:79.59 Age:23 Alive:false Github:true}
```It is also possible to read data incrementally
```go
decoder := fixedwidth.NewDecoder(bytes.NewReader(data))
for {
var element myStruct
err := decoder.Decode(&element)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
handle(element)
}
```### UTF-8, Codepoints, and Multibyte Characters
fixedwidth supports encoding and decoding fixed-width data where indices are expressed in
unicode codepoints and not raw bytes. The data must be UTF-8 encoded.```go
decoder := fixedwidth.NewDecoder(strings.NewReader(data))
decoder.SetUseCodepointIndices(true)
// Decode as usual now
``````go
buff := new(bytes.Buffer)
encoder := fixedwidth.NewEncoder(buff)
encoder.SetUseCodepointIndices(true)
// Encode as usual now
```### Alignment Behavior
| Alignment | Encoding | Decoding |
| --------- | -------- | -------- |
| `default` | Field is left aligned | The padding character is trimmed from both right and left of value |
| `left` | Field is left aligned | The padding character is trimmed from right of value |
| `right` | Field is right aligned | The padding character is trimmed from left of value |
| `none` | Field is left aligned | The padding character is not trimmed from value. Useful for nested structs. |## Notes
1. `{}` indicates an argument. `[]` indicates and optional segment [^](#a1)
2. The `default` alignment is similar to `left` but has slightly different behavior required to maintain backwards compatibility [^](#a2)## Licence
MIT