Skip to content

Commit

Permalink
Merge pull request #27 from strvcom/feat/id-scan
Browse files Browse the repository at this point in the history
feat: custom Scan method for uuid IDs
  • Loading branch information
TomasKocman committed Nov 9, 2022
2 parents 296e9c7 + f4e5004 commit 795e0d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ How to release a new version:

## [Unreleased]

## [0.3.0] - 2022-10-30
### Added
- Scan method for uuid IDs.

### Fixed
- IDs generated file format.

## [0.2.2] - 2022-10-26
### Fixed
- Openapi works with updated dependencies.
Expand All @@ -25,7 +32,8 @@ How to release a new version:
### Added
- Added Changelog.

[Unreleased]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.2.2...HEAD
[Unreleased]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.3.0...HEAD
[0.3.0]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.2.2...v0.3.0
[0.2.2]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.2.1...v0.2.2
[0.2.1]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.2.0..v0.2.1
[0.2.0]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.1.1..v0.2.0
Expand Down
17 changes: 14 additions & 3 deletions cmd/tea/gen_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func unmarshalUint64(i *uint64, idTypeName string, data []byte) error {
}
uintNum, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return fmt.Errorf("parse %q id value: %w", idTypeName, err)
return fmt.Errorf("parsing %q id value: %w", idTypeName, err)
}
*i = uintNum
return nil
Expand All @@ -96,7 +96,14 @@ func (i *{{ . }}) UnmarshalJSON(data []byte) error {
const uuidTemplate = `
func unmarshalUUID(u *uuid.UUID, idTypeName string, data []byte) error {
if err := u.UnmarshalText(data); err != nil {
return fmt.Errorf("parse %q id value: %w", idTypeName, err)
return fmt.Errorf("parsing %q id value: %w", idTypeName, err)
}
return nil
}
func scanUUID(u *uuid.UUID, idTypeName string, data any) error {
if err := u.Scan(data); err != nil {
return fmt.Errorf("scanning %q id value: %w", idTypeName, err)
}
return nil
}
Expand Down Expand Up @@ -128,6 +135,10 @@ func (i *{{ . }}) UnmarshalText(data []byte) error {
func (i *{{ . }}) UnmarshalJSON(data []byte) error {
return unmarshalUUID((*uuid.UUID)(i), "{{ . }}", data)
}
func (i *{{ . }}) Scan(data any) error {
return scanUUID((*uuid.UUID)(i), "{{ . }}", data)
}
{{ end }}`

// IDs stores multiple ID names under one kind.
Expand Down Expand Up @@ -212,8 +223,8 @@ func (i IDs) generateHeader() []byte {
if _, ok := i["uint64"]; ok {
d = append(d, "\t\"strconv\"\n"...)
}
d = append(d, "\n"...)
if _, ok := i["uuid.UUID"]; ok {
d = append(d, "\n"...)
d = append(d, "\t\"github.com/google/uuid\"\n"...)
}

Expand Down

0 comments on commit 795e0d0

Please sign in to comment.