Skip to content

Commit

Permalink
Merge pull request #229 from stephenafamo/pg-types
Browse files Browse the repository at this point in the history
Fix generated types for macaddr8, money and cidr
  • Loading branch information
stephenafamo committed Jun 4, 2024
2 parents a85e5ea + e85bde3 commit 1f92805
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
// After
models.SelectJoins.Jets.InnerJoin.Pilots(ctx)
```

- Changed the `Count()` function on `Views` to clone the query instead of changing the existing one. This makes queries reusable and the `Count()` function to behave as one would expect.

```go
Expand All @@ -64,11 +64,19 @@ 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.
- Removed `As` starter. It takes an `Expression` and is not needed since the `Expression` has an `As` method which can be used directly.

### Fixed

- Fix a bug with `types.Stringer[T]` where the wrong value was returned in the `Value()` method.

## [v0.26.1] - 2024-05-26

### Fixed
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
16 changes: 8 additions & 8 deletions gen/bobgen-psql/driver/psql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "types.Text[netip.Prefix, *netip.Prefix]"
"type": "types.Text[netip.Addr, *netip.Addr]"
},
{
"name": "cidr_nnull",
Expand All @@ -1258,7 +1258,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "types.Text[netip.Prefix, *netip.Prefix]"
"type": "types.Text[netip.Addr, *netip.Addr]"
},
{
"name": "circle_null",
Expand Down Expand Up @@ -1401,7 +1401,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "string"
"type": "decimal.Decimal"
},
{
"name": "money_nnull",
Expand All @@ -1412,7 +1412,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "string"
"type": "decimal.Decimal"
},
{
"name": "path_null",
Expand Down Expand Up @@ -3046,7 +3046,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "types.Text[netip.Prefix, *netip.Prefix]"
"type": "types.Text[netip.Addr, *netip.Addr]"
},
{
"name": "cidr_nnull",
Expand All @@ -3057,7 +3057,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "types.Text[netip.Prefix, *netip.Prefix]"
"type": "types.Text[netip.Addr, *netip.Addr]"
},
{
"name": "circle_null",
Expand Down Expand Up @@ -3200,7 +3200,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "string"
"type": "decimal.Decimal"
},
{
"name": "money_nnull",
Expand All @@ -3211,7 +3211,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "string"
"type": "decimal.Decimal"
},
{
"name": "path_null",
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
2 changes: 1 addition & 1 deletion types/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Stringer[T interface {
}

func (s Stringer[T]) Value() (driver.Value, error) {
return []byte(s.Val), nil
return s.Val.String(), nil
}

func (s *Stringer[T]) Scan(value any) error {
Expand Down

0 comments on commit 1f92805

Please sign in to comment.