Skip to content

Commit

Permalink
Comment public functions and types, make public API stable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbub committed Feb 11, 2022
1 parent 549be7a commit 5b84572
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.2.1

- Bump license year.
- Make public API stable.
- Comment public functions and types.

## v0.2.0

- Add support for drivers which do not implement `driver.DriverContext`.
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

Go implementation of https://google.github.io/sqlcommenter/.

Public API is not stable, expect breaking changes.

## Usage with pgx stdlib driver

```go
Expand Down
4 changes: 4 additions & 0 deletions attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
)

// AttrPairs builds Attrs from multiple key value pairs.
func AttrPairs(pairs ...string) Attrs {
if len(pairs)%2 == 1 {
panic("got odd number of pairs")
Expand All @@ -15,13 +16,16 @@ func AttrPairs(pairs ...string) Attrs {
return attrs
}

// Attr represents attribute with key and value.
type Attr struct {
Key string
Value string
}

// Attrs wrap map of attributes.
type Attrs map[string]string

// Update updates map from other Attrs.
func (a Attrs) Update(other Attrs) {
for k, v := range other {
a[k] = v
Expand Down
1 change: 1 addition & 0 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
commentEnd = "*/"
)

// Comment adds comments to query using provided options.
func Comment(ctx context.Context, query string, opts ...Option) string {
if len(opts) == 0 {
return query
Expand Down
10 changes: 9 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
)

// WrapDriver wraps sql driver with sqlcommenter support.
func WrapDriver(drv driver.Driver, opts ...Option) driver.Driver {
return &commentDriver{
drv: drv,
Expand All @@ -28,7 +29,7 @@ func (d *commentDriver) Open(name string) (driver.Conn, error) {
func (d *commentDriver) OpenConnector(name string) (driver.Connector, error) {
drvCtx, ok := d.drv.(driver.DriverContext)
if !ok {
return &dsnConnector{dsn: name, drv: d}, nil
return newDSNConnector(name, d), nil
}

ctr, err := drvCtx.OpenConnector(name)
Expand Down Expand Up @@ -62,6 +63,13 @@ func (c *connector) Driver() driver.Driver {
return c.drv
}

func newDSNConnector(dsn string, drv *commentDriver) *dsnConnector {
return &dsnConnector{
dsn: dsn,
drv: drv,
}
}

type dsnConnector struct {
dsn string
drv *commentDriver
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import (
"context"
)

// Option configures commenter.
type Option func(cmt *commenter)

// AttrProvider provides Attrs from context.Context.
type AttrProvider interface {
GetAttrs(context.Context) Attrs
}

// AttrProviderFunc adapts func to AttrProvider.
type AttrProviderFunc func(context.Context) Attrs

// GetAttrs returns Attrs.
func (f AttrProviderFunc) GetAttrs(ctx context.Context) Attrs {
return f(ctx)
}

// WithAttrs configures commenter with Attrs.
func WithAttrs(attrs Attrs) Option {
return func(cmt *commenter) {
cmt.providers = append(cmt.providers, AttrProviderFunc(func(ctx context.Context) Attrs {
Expand All @@ -24,6 +29,7 @@ func WithAttrs(attrs Attrs) Option {
}
}

// WithAttrPairs configures commenter with attr pairs.
func WithAttrPairs(pairs ...string) Option {
return func(cmt *commenter) {
cmt.providers = append(cmt.providers, AttrProviderFunc(func(ctx context.Context) Attrs {
Expand All @@ -32,12 +38,14 @@ func WithAttrPairs(pairs ...string) Option {
}
}

// WithAttrProvider configures commenter with AttrProvider.
func WithAttrProvider(prov AttrProvider) Option {
return func(cmt *commenter) {
cmt.providers = append(cmt.providers, prov)
}
}

// WithAttrFunc configures commenter with AttrProviderFunc.
func WithAttrFunc(fn AttrProviderFunc) Option {
return func(cmt *commenter) {
cmt.providers = append(cmt.providers, fn)
Expand Down

0 comments on commit 5b84572

Please sign in to comment.