Skip to content

Commit 406525f

Browse files
committed
reverse headers + ci
1 parent bd22e11 commit 406525f

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

.github/workflows/go.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches:
8+
- *
9+
10+
jobs:
11+
lint:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v5
16+
with:
17+
fetch-depth: 0 # needed because of the new-from-rev in golangci
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v6
21+
with:
22+
go-version: "1.25.x"
23+
24+
- name: golangci-lint
25+
uses: golangci/golangci-lint-action@v8
26+
with:
27+
args: --timeout 5m0s
28+
29+
- id: govulncheck
30+
uses: golang/govulncheck-action@v1
31+
32+
test:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout repo
36+
uses: actions/checkout@v5
37+
38+
- name: Set up Go
39+
uses: actions/setup-go@v6
40+
with:
41+
go-version: "1.25.x"
42+
43+
- name: Running Unit Tests
44+
run: go test ./...

errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package csvheaders
2+
3+
// ReplacerError is an error that can be returned when using the Replacer.
4+
type ReplacerError string
5+
6+
// Error implements the error interface.
7+
func (e ReplacerError) Error() string {
8+
return string(e)
9+
}
10+
11+
// ErrDuplicateHeader is returned when the header map registers the same header twice.
12+
const ErrDuplicateHeader = ReplacerError("duplicate header")

replacer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ func NewReplacer(reader *csv.Reader, inToOut map[string]string) *Replacer {
2121
return &Replacer{Reader: reader, inToOut: inToOut}
2222
}
2323

24+
// NewReverseReplacer returns a replacer using mappings from struct definition to file contents.
25+
// If there is a duplicate value in the headers mapping, this returns an error.
26+
func NewReverseReplacer(reader *csv.Reader, outToIn map[string]string) (*Replacer, error) {
27+
r := &Replacer{Reader: reader, inToOut: make(map[string]string)}
28+
29+
for out, in := range outToIn {
30+
if h, alreadyRegistered := r.inToOut[in]; alreadyRegistered {
31+
return nil, fmt.Errorf("struct tag %s registered with CSV headers %s and %s: %w", in, out, h, ErrDuplicateHeader)
32+
}
33+
34+
r.inToOut[in] = out
35+
}
36+
37+
return r, nil
38+
}
39+
2440
// Read a record from the reader.
2541
func (r *Replacer) Read() ([]string, error) {
2642
record, err := r.Reader.Read()

0 commit comments

Comments
 (0)