Skip to content

Commit

Permalink
Fix generated types for macaddr8, money and cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Jun 4, 2024
1 parent 9dc93b9 commit 64e6da8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

This makes it possible to support more queries.

- Use `netip.Addr` instead of `netip.Prefix` for Postgres `cidr` type.
- Use `decimal.Decimal` instead of `string` for Postgres `money` type.
- Use `net.HardwareAddr` for Postgres `macaddr8` type, in addition to the `macaddr` type.

### Removed

- Remove `TableWhere` function from the generated code. It was not used by the rest of the generated code and offered no clear benefit.
Expand Down
11 changes: 11 additions & 0 deletions gen/bobgen-helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ func Types() drivers.Types {
CompareExpr: `AAA.Equal(BBB)`,
NoScannerValuerTest: true,
},
"types.Text[netip.Addr, *netip.Addr]": {
Imports: importers.List{
`"net/netip"`,
`"github.com/stephenafamo/bob/types"`,
},
RandomExpr: `var addr [4]byte
rand.Read(addr[:])
ipAddr := netip.AddrFrom4(addr)
return any(types.Text[netip.Addr, *netip.Addr]{Val: ipAddr}).(T)`,
RandomExprImports: importers.List{`"crypto/rand"`},
},
"types.Text[netip.Prefix, *netip.Prefix]": {
Imports: importers.List{
`"net/netip"`,
Expand Down
10 changes: 6 additions & 4 deletions gen/bobgen-psql/driver/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func (d *driver) translateColumnType(c drivers.Column, info colInfo) drivers.Col
c.Type = "int16"
case "smallserial":
c.Type = "uint16"
case "decimal", "numeric":
case "decimal", "numeric", "money":
c.Type = "decimal.Decimal"
case "double precision":
c.Type = "float64"
case "real":
c.Type = "float32"
case "bit", "interval", "uuint", "bit varying", "character", "money", "character varying", "text", "xml":
case "bit", "interval", "uuint", "bit varying", "character", "character varying", "text", "xml":
c.Type = "string"
case "json", "jsonb":
c.Type = "types.JSON[json.RawMessage]"
Expand All @@ -71,9 +71,11 @@ func (d *driver) translateColumnType(c drivers.Column, info colInfo) drivers.Col
c.Type = "pgeo.Polygon"
case "uuid":
c.Type = "uuid.UUID"
case "inet", "cidr":
case "inet":
c.Type = "types.Text[netip.Prefix, *netip.Prefix]"
case "macaddr":
case "cidr":
c.Type = "types.Text[netip.Addr, *netip.Addr]"
case "macaddr", "macaddr8":
c.Type = "types.Stringer[net.HardwareAddr]"
case "ENUM":
c.Type = "string"
Expand Down

0 comments on commit 64e6da8

Please sign in to comment.