Skip to content

Commit df11b76

Browse files
committed
Add none formatting option
1 parent 66e7247 commit df11b76

File tree

4 files changed

+27
-60
lines changed

4 files changed

+27
-60
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The struct tag schema schema used by fixedwidth is: `fixed:"{startPos},{endPos},
1212

1313
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.
1414

15-
The `alignment` argument controls the alignment of the value within it's interval. The valid options are `default`<sup id="a2">[2](#f2)</sup>, `right`, and `left`. The `alignment` is optional and can be omitted.
15+
The `alignment` argument controls the alignment of the value within it's interval. The valid options are `default`<sup id="a2">[2](#f2)</sup>, `right`, `left`, and `none`. The `alignment` is optional and can be omitted.
1616

1717
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.
1818

@@ -120,6 +120,7 @@ encoder.SetUseCodepointIndices(true)
120120
| `default` | Field is left aligned | The padding character is trimmed from both right and left of value |
121121
| `left` | Field is left aligned | The padding character is trimmed from right of value |
122122
| `right` | Field is right aligned | The padding character is trimmed from left of value |
123+
| `none` | Field is left aligned | The padding character is not trimmed from value. Useful for nested structs. |
123124

124125
## Notes
125126
1. <span id="f1">`{}` indicates an argument. `[]` indicates and optional segment [^](#a1)</span>

decode.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ func rawValueFromLine(value rawValue, startPos, endPos int, format format) rawVa
207207
trimFunc = func(r rawValue) rawValue {
208208
return r.trimLeft(string(format.padChar))
209209
}
210+
case alignmentNone:
211+
trimFunc = func(r rawValue) rawValue { return r }
210212
default:
211213
trimFunc = func(r rawValue) rawValue {
212214
return r.trim(string(format.padChar))

decode_test.go

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,17 @@ func TestDecodeSetUseCodepointIndices(t *testing.T) {
386386

387387
}
388388

389-
func TestDecodeSetUseCodepointIndices_Nested(t *testing.T) {
389+
func TestDecode_Nested(t *testing.T) {
390390
type Nested struct {
391391
First string `fixed:"1,3"`
392392
Second string `fixed:"4,6"`
393393
}
394394

395395
type Test struct {
396396
First string `fixed:"1,3"`
397-
Second Nested `fixed:"4,9"`
397+
Second Nested `fixed:"4,9,none"`
398398
Third string `fixed:"10,12"`
399-
Fourth Nested `fixed:"13,18"`
399+
Fourth Nested `fixed:"13,18,none"`
400400
Fifth string `fixed:"19,21"`
401401
}
402402

@@ -416,6 +416,17 @@ func TestDecodeSetUseCodepointIndices_Nested(t *testing.T) {
416416
Fifth: "012",
417417
},
418418
},
419+
{
420+
name: "All ASCII characters with padding",
421+
raw: []byte(" 2 B 5 E 8 H 1 \n"),
422+
expected: Test{
423+
First: "2",
424+
Second: Nested{First: "B", Second: "5"},
425+
Third: "E",
426+
Fourth: Nested{First: "8", Second: "H"},
427+
Fifth: "1",
428+
},
429+
},
419430
{
420431
name: "Multi-byte characters",
421432
raw: []byte("123x☃x456x☃x789x☃x012\n"),
@@ -427,63 +438,15 @@ func TestDecodeSetUseCodepointIndices_Nested(t *testing.T) {
427438
Fifth: "012",
428439
},
429440
},
430-
} {
431-
t.Run(tt.name, func(t *testing.T) {
432-
d := NewDecoder(bytes.NewReader(tt.raw))
433-
d.SetUseCodepointIndices(true)
434-
var s Test
435-
err := d.Decode(&s)
436-
if err != nil {
437-
t.Errorf("Unexpected err: %v", err)
438-
}
439-
if !reflect.DeepEqual(tt.expected, s) {
440-
t.Errorf("Decode(%v) want %v, have %v", tt.raw, tt.expected, s)
441-
}
442-
})
443-
}
444-
}
445-
446-
func TestDecodeSetUseCodepointIndices_PaddingTrimmed(t *testing.T) {
447-
type Nested struct {
448-
First int64 `fixed:"1,2,right,0"`
449-
Second string `fixed:"3,4"`
450-
Third string `fixed:"5,6"`
451-
Fourth string `fixed:"7,8"`
452-
}
453-
type Test struct {
454-
First Nested `fixed:"1,8"`
455-
Second string `fixed:"9,10"`
456-
}
457-
458-
for _, tt := range []struct {
459-
name string
460-
raw []byte
461-
expected Test
462-
}{
463-
{
464-
name: "All ASCII characters",
465-
raw: []byte("00 11"),
466-
expected: Test{
467-
First: Nested{
468-
First: 0,
469-
Second: "",
470-
Third: "",
471-
Fourth: "",
472-
},
473-
Second: "11",
474-
},
475-
},
476441
{
477-
name: "Multi-byte characters",
478-
raw: []byte("00 ☃☃"),
442+
name: "Multi-byte characters with padding",
443+
raw: []byte(" ☃ Ñ ☃ Ñ ☃ Ñ ☃ \n"),
479444
expected: Test{
480-
First: Nested{
481-
First: 0,
482-
Second: "",
483-
Third: "",
484-
Fourth: "",
485-
},
486-
Second: "☃☃",
445+
First: "☃",
446+
Second: Nested{First: "Ñ", Second: "☃"},
447+
Third: "Ñ",
448+
Fourth: Nested{First: "☃", Second: "Ñ"},
449+
Fifth: "☃",
487450
},
488451
},
489452
} {

format.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fixedwidth
22

33
const (
44
defaultAlignment alignment = "default"
5+
alignmentNone alignment = "none"
56
right alignment = "right"
67
left alignment = "left"
78
)
@@ -24,7 +25,7 @@ type alignment string
2425

2526
func (a alignment) Valid() bool {
2627
switch a {
27-
case defaultAlignment, right, left:
28+
case defaultAlignment, right, left, alignmentNone:
2829
return true
2930
default:
3031
return false

0 commit comments

Comments
 (0)