generated from adrienaury/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: configure default encoding via flag #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
410b40e
chore: fix fixed-width file editor config
adrienaury 8bd4c64
test: fix test data schema definition
adrienaury c19474e
fix: do not trim control characters
adrienaury 74b8231
fix: json string encoding
adrienaury 935351a
refactor: config is on the appli side, not infra
adrienaury fdf0e0c
fix: flag config should not override yaml config
adrienaury d56aec4
fix: flag config should not override yaml config
adrienaury 56df9f2
feat: configure default encoding via flag
adrienaury File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| RECORD-TYPE-1 001JOHN DOE 1234 ELM STREET SPRINGFIELD, IL 62704 1234560100123456 XXXXX0200123456 XXXXX0300123456 XXXXX0400123456 XXXXX0500123456 XXXXX | ||
| RECORD-TYPE-1 002JANE SMITH 56 MAPLE AVENUE RIVERSIDE, CA 92501 9876540100123456 XXXXX0200123456 XXXXX0300123456 XXXXX0400123456 XXXXX0500123456 XXXXX | ||
| RECORD-TYPE-2 01202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 02202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 03202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 04202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 11202504302025043020250430XXXXXXXYYYYYYYY | ||
| RECORD-TYPE-2 12202504302025043020250430XXXXXXXYYYYYYYY | ||
| RECORD-TYPE-2 13202504302025043020250430XXXXXXXYYYYYYYY | ||
| RECORD-TYPE-2 01202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 02202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 03202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 04202504302025043020250430AAAAABBBBBCCCCC | ||
| RECORD-TYPE-2 11202504302025043020250430XXXXXXXYYYYYYYY | ||
| RECORD-TYPE-2 12202504302025043020250430XXXXXXXYYYYYYYY | ||
| RECORD-TYPE-2 13202504302025043020250430XXXXXXXYYYYYYYY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,5 @@ schema: | |
| length: 11 | ||
| - name: FILLER | ||
| length: 5 | ||
| - name: NL | ||
| length: 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,7 @@ schema: | |
| length: 7 | ||
| - name: DATA-2-2 | ||
| length: 8 | ||
| - name: FILLER | ||
| length: 163 | ||
| - name: NL | ||
| length: 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/cgi-fr/posimap/pkg/posimap/core/codec" | ||
| "github.com/cgi-fr/posimap/pkg/posimap/core/predicate" | ||
| "github.com/cgi-fr/posimap/pkg/posimap/core/schema" | ||
| "golang.org/x/text/encoding/charmap" | ||
| ) | ||
|
|
||
| var ErrUnsupportedCharset = errors.New("unsupported charset") | ||
|
|
||
| type Config struct { | ||
| Schema Schema `yaml:"schema"` | ||
| } | ||
|
|
||
| type Schema []Field | ||
|
|
||
| type Field struct { | ||
| Name string `yaml:"name"` | ||
| Occurs int `yaml:"occurs,omitempty"` | ||
| Redefine string `yaml:"redefine,omitempty"` | ||
| When string `yaml:"when,omitempty"` | ||
|
|
||
| Length int `yaml:"length"` | ||
| Trim *bool `yaml:"trim,omitempty"` // Trim can be nil if not set in the configuration file | ||
| Charset *string `yaml:"charset,omitempty"` // Charset can be nil if not set in the configuration file | ||
|
|
||
| Schema Either[string, Schema] `yaml:"schema"` // Schema is either a filename (external schema) or an embedded schema | ||
| } | ||
|
|
||
| func (f Field) IsRecord() bool { | ||
| return f.Schema.T2 != nil | ||
| } | ||
|
|
||
| func (f Field) CompileOptions() []schema.Option { | ||
| options := make([]schema.Option, 0) | ||
|
|
||
| if f.Occurs > 0 { | ||
| options = append(options, schema.Occurs(f.Occurs)) | ||
| } | ||
|
|
||
| if f.Redefine != "" { | ||
| options = append(options, schema.Redefines(f.Redefine)) | ||
| } | ||
|
|
||
| if f.When != "" { | ||
| options = append(options, schema.Condition(predicate.When(f.When))) | ||
| } | ||
|
|
||
| return options | ||
| } | ||
|
|
||
| func (f Field) CompileCharset() (*charmap.Charmap, error) { | ||
| for _, encoding := range charmap.All { | ||
| if charmap, ok := encoding.(*charmap.Charmap); ok && charmap.String() == *f.Charset { | ||
| return charmap, nil | ||
| } | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("%w: %s", ErrUnsupportedCharset, *f.Charset) | ||
| } | ||
|
|
||
| func (f Field) Compile(record schema.Record, defaults ...Default) (schema.Record, error) { | ||
| if f.IsRecord() { | ||
| schema, err := f.Schema.T2.Compile(defaults...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| record = record.WithRecord(f.Name, schema, f.CompileOptions()...) | ||
| } else { | ||
| charset, err := f.CompileCharset() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| record = record.WithField(f.Name, codec.NewString(charset, f.Length, *f.Trim), f.CompileOptions()...) | ||
| } | ||
|
|
||
| return record, nil | ||
| } | ||
|
|
||
| func (s Schema) Compile(defaults ...Default) (schema.Record, error) { | ||
| var err error | ||
|
|
||
| record := make(schema.Record, 0, len(s)) | ||
|
|
||
| for _, field := range s { | ||
| for _, defaultFunc := range defaults { | ||
| field = defaultFunc(field) | ||
| } | ||
|
|
||
| record, err = field.Compile(record, defaults...) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to compile field %s: %w", field.Name, err) | ||
| } | ||
| } | ||
|
|
||
| return record, nil | ||
| } | ||
|
|
||
| func (c Config) Compile(defaults ...Default) (schema.Record, error) { | ||
| return c.Schema.Compile(defaults...) | ||
| } | ||
|
|
||
| type Default func(field Field) Field | ||
|
|
||
| func Trim(enable bool) Default { | ||
| return func(field Field) Field { | ||
| if field.Trim == nil { | ||
| field.Trim = &enable | ||
| } | ||
|
|
||
| return field | ||
| } | ||
| } | ||
|
|
||
| func Charset(name string) Default { | ||
| return func(field Field) Field { | ||
| if field.Charset == nil { | ||
| field.Charset = &name | ||
| } | ||
|
|
||
| return field | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package config_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/cgi-fr/posimap/internal/appli/config" | ||
| "github.com/cgi-fr/posimap/pkg/posimap/core/codec" | ||
| "github.com/cgi-fr/posimap/pkg/posimap/core/schema" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "golang.org/x/text/encoding/charmap" | ||
| "gotest.tools/assert" | ||
| ) | ||
|
|
||
| func TestCompile(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| //nolint:exhaustruct | ||
| tests := []struct { | ||
| name string | ||
| schema config.Schema | ||
| expected schema.Record | ||
| }{ | ||
| { | ||
| name: "Simple schema", | ||
| schema: config.Schema{ | ||
| config.Field{ | ||
| Name: "field1", | ||
| Length: 10, | ||
| }, | ||
| }, | ||
| expected: schema.NewSchema().WithField("field1", codec.NewString(charmap.ISO8859_1, 10, true)), | ||
| }, | ||
| { | ||
| name: "Nested schema", | ||
| schema: config.Schema{ | ||
| config.Field{ | ||
| Name: "lvel1", | ||
| Schema: config.Either[string, config.Schema]{ | ||
| T2: &config.Schema{ | ||
| { | ||
| Name: "lvel2", | ||
| Length: 10, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expected: schema.NewSchema().WithRecord("lvel1", | ||
| schema.NewSchema().WithField("lvel2", codec.NewString(charmap.ISO8859_1, 10, true)), | ||
| ), | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| result, err := test.schema.Compile(config.Trim(true), config.Charset(charmap.ISO8859_1.String())) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got %v", err) | ||
| } | ||
|
|
||
| // Compare the loaded schema with the expected schema | ||
| assert.DeepEqual(t, result, test.expected, | ||
| cmp.AllowUnexported(schema.Field{}, schema.Definition{}, codec.String{}, charmap.Charmap{}), | ||
| cmpopts.IgnoreFields(charmap.Charmap{}, "decode"), | ||
| ) | ||
| }) | ||
| } | ||
| } |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.