Skip to content

Commit

Permalink
Merge pull request #8 from underbek/feature/config
Browse files Browse the repository at this point in the history
Yaml config
  • Loading branch information
underbek committed Feb 6, 2023
2 parents 492b58a + 4d993cd commit 1ad47df
Show file tree
Hide file tree
Showing 13 changed files with 616 additions and 183 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ local-test: build

$(GOENV) go generate ./_test_data/mapper/domain

./bin/datamapper -c _test_data/config/config.yaml

.PHONY: help
help: build
./bin/datamapper --help
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
### Install

```shell
go install github.com/underbek/datamapper@v0.0.7
go install github.com/underbek/datamapper@v0.0.8
```

### Usage

```text
Usage:
datamapper [OPTIONS]
Application Options:
-c, --config= Yaml config path
-d, --destination= Destination file path
--cf= User conversion functions sources/packages. Can add package alias like {package_path}:{alias)
--from= Model from name
Expand All @@ -27,6 +27,54 @@ Help Options:
-h, --help Show this help message
```

### Config:
datamapper can read configuration file by `config` flag.

Config example:
```yaml
# array of conversion functions
conversion-functions:
## source path or full package name
- source: github.com/underbek/datamapper/_test_data/mapper/convertors
## optional package alias
alias: cf
- source: github.com/underbek/datamapper/_test_data/mapper/other_convertors

# array of conversion mapping
options:
## From model
- from:
## name of model (can use with pointer)
name: "*User"
## mapping tag (optional|default = map)
tag : map
## source path or full package name
source: github.com/underbek/datamapper/_test_data/mapper/domain
## optional package alias
alias: domain
## From model like a from model
to:
name: "User"
source: github.com/underbek/datamapper/_test_data/mapper/transport
## Destination file path
destination: _test_data/local_test/domain_to_dto_user_converter.go
## If you need to crate inverse conversions
inverse: true

- from:
name: "User"
source: github.com/underbek/datamapper/_test_data/mapper/broken
alias: bk
tag: map
to:
name: "*User"
source: github.com/underbek/datamapper/_test_data/mapper/domain
alias: dm
tag: map
destination: _test_data/local_test/broken_to_domain_user_converter.go
inverse: true
```

### go generate

```go
Expand Down
41 changes: 41 additions & 0 deletions _test_data/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# array of conversion functions
conversion-functions:
## source path or full package name
- source: github.com/underbek/datamapper/_test_data/mapper/convertors
## optional package alias
alias: cf
- source: github.com/underbek/datamapper/_test_data/mapper/other_convertors

# array of conversion mapping
options:
## From model
- from:
## name of model (can use with pointer)
name: "*User"
## mapping tag (optional|default = map)
tag : map
## source path or full package name
source: github.com/underbek/datamapper/_test_data/mapper/domain
## optional package alias
alias: domain
## From model like a from model
to:
name: "User"
source: github.com/underbek/datamapper/_test_data/mapper/transport
## Destination file path
destination: _test_data/local_test/domain_to_dto_user_converter.go
## If you need to crate inverse conversions
inverse: true

- from:
name: "User"
source: github.com/underbek/datamapper/_test_data/mapper/broken
alias: bk
tag: map
to:
name: "*User"
source: github.com/underbek/datamapper/_test_data/mapper/domain
alias: dm
tag: map
destination: _test_data/local_test/broken_to_domain_user_converter.go
inverse: true
4 changes: 4 additions & 0 deletions _test_data/mapper/domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ type User struct {
ChildCount *int `map:"children"`
Empty string
}

type Order struct {
User *User `map:"user"`
}
24 changes: 24 additions & 0 deletions _test_data/mapper/expected/with_generated/expected.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions _test_data/mapper/expected/with_generated/user_converter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions _test_data/mapper/transport/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ type User struct {
Age string `map:"age"`
ChildCount *string `map:"children"`
}

type Order struct {
User *User `map:"user"`
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module github.com/underbek/datamapper
go 1.18

require (
github.com/creasty/defaults v1.6.0
github.com/google/uuid v1.3.0
github.com/jessevdk/go-flags v1.5.0
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.8.0
golang.org/x/exp v0.0.0-20220706164943-b4a6d9510983
golang.org/x/text v0.3.7
golang.org/x/tools v0.1.10
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -18,5 +20,4 @@ require (
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/creasty/defaults v1.6.0 h1:ltuE9cfphUtlrBeomuu8PEyISTXnxqkBIoQfXgv7BSc=
github.com/creasty/defaults v1.6.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
5 changes: 5 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const depth = 2

type Logger interface {
Info(v ...any)
Infof(format string, v ...any)
Warn(v ...any)
Error(v ...any)
Fatal(v ...any)
Expand All @@ -33,6 +34,10 @@ func (l *logger) Info(v ...any) {
_ = l.info.Output(depth, fmt.Sprint(v...))
}

func (l *logger) Infof(format string, v ...any) {
_ = l.info.Output(depth, fmt.Sprintf(format, v...))
}

func (l *logger) Warn(v ...any) {
_ = l.warn.Output(depth, fmt.Sprint(v...))
}
Expand Down
Loading

0 comments on commit 1ad47df

Please sign in to comment.