Skip to content

Commit

Permalink
specialize options via generics
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroshade committed Feb 13, 2024
1 parent 1391f3b commit 254c6a3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,50 @@ var (
)

// WithAwsConfig sets the AWS configuration for the catalog.
func WithAwsConfig(cfg aws.Config) Option {
func WithAwsConfig(cfg aws.Config) Option[GlueCatalog] {
return func(o *options) {
o.awsConfig = cfg
}
}

func WithCredential(cred string) Option {
func WithCredential(cred string) Option[RestCatalog] {
return func(o *options) {
o.credential = cred
}
}

func WithOAuthToken(token string) Option {
func WithOAuthToken(token string) Option[RestCatalog] {
return func(o *options) {
o.oauthToken = token
}
}

func WithTLSConfig(config *tls.Config) Option {
func WithTLSConfig(config *tls.Config) Option[RestCatalog] {
return func(o *options) {
o.tlsConfig = config
}
}

func WithWarehouseLocation(loc string) Option {
func WithWarehouseLocation(loc string) Option[RestCatalog] {
return func(o *options) {
o.warehouseLocation = loc
}
}

func WithMetadataLocation(loc string) Option {
func WithMetadataLocation(loc string) Option[RestCatalog] {
return func(o *options) {
o.metadataLocation = loc
}
}

func WithSigV4() Option {
func WithSigV4() Option[RestCatalog] {
return func(o *options) {
o.enableSigv4 = true
o.sigv4Service = "execute-api"
}
}

func WithSigV4RegionSvc(region, service string) Option {
func WithSigV4RegionSvc(region, service string) Option[RestCatalog] {
return func(o *options) {
o.enableSigv4 = true
o.sigv4Region = region
Expand All @@ -102,19 +102,19 @@ func WithSigV4RegionSvc(region, service string) Option {
}
}

func WithAuthURI(uri *url.URL) Option {
func WithAuthURI(uri *url.URL) Option[RestCatalog] {
return func(o *options) {
o.authUri = uri
}
}

func WithPrefix(prefix string) Option {
func WithPrefix(prefix string) Option[RestCatalog] {
return func(o *options) {
o.prefix = prefix
}
}

type Option func(*options)
type Option[T GlueCatalog | RestCatalog] func(*options)

type options struct {
awsConfig aws.Config
Expand Down
2 changes: 1 addition & 1 deletion catalog/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type GlueCatalog struct {
glueSvc glueAPI
}

func NewGlueCatalog(opts ...Option) *GlueCatalog {
func NewGlueCatalog(opts ...Option[GlueCatalog]) *GlueCatalog {
glueOps := &options{}

for _, o := range opts {
Expand Down
2 changes: 1 addition & 1 deletion catalog/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ type RestCatalog struct {
props iceberg.Properties
}

func NewRestCatalog(name, uri string, opts ...Option) (*RestCatalog, error) {
func NewRestCatalog(name, uri string, opts ...Option[RestCatalog]) (*RestCatalog, error) {
ops := &options{}
for _, o := range opts {
o(ops)
Expand Down
18 changes: 9 additions & 9 deletions cmd/iceberg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ func main() {
log.Fatal("unimplemented output type")
}

opts := []catalog.Option{}
if len(cfg.Cred) > 0 {
opts = append(opts, catalog.WithCredential(cfg.Cred))
}

if len(cfg.Warehouse) > 0 {
opts = append(opts, catalog.WithWarehouseLocation(cfg.Warehouse))
}

var cat catalog.Catalog
switch catalog.CatalogType(cfg.Catalog) {
case catalog.REST:
opts := []catalog.Option[catalog.RestCatalog]{}
if len(cfg.Cred) > 0 {
opts = append(opts, catalog.WithCredential(cfg.Cred))
}

if len(cfg.Warehouse) > 0 {
opts = append(opts, catalog.WithWarehouseLocation(cfg.Warehouse))
}

if cat, err = catalog.NewRestCatalog("rest", cfg.URI, opts...); err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 254c6a3

Please sign in to comment.