alexandre-normand commented on code in PR #709: URL: https://github.com/apache/iceberg-go/pull/709#discussion_r2765066464
########## io/registry.go: ########## @@ -0,0 +1,106 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package io + +import ( + "context" + "fmt" + "maps" + "net/url" + "slices" + "sync" +) + +type registry map[string]SchemeFactory + +func (r registry) getKeys() []string { + regMutex.Lock() + defer regMutex.Unlock() + + return slices.Collect(maps.Keys(r)) +} + +func (r registry) set(scheme string, factory SchemeFactory) { + regMutex.Lock() + defer regMutex.Unlock() + r[scheme] = factory +} + +func (r registry) get(scheme string) (SchemeFactory, bool) { + regMutex.Lock() + defer regMutex.Unlock() + factory, ok := r[scheme] + + return factory, ok +} + +func (r registry) remove(scheme string) { + regMutex.Lock() + defer regMutex.Unlock() + delete(r, scheme) +} + +var ( + regMutex sync.Mutex + defaultRegistry = registry{} +) + +// SchemeFactory is a function that creates an IO implementation for a given URI and properties. +type SchemeFactory func(ctx context.Context, parsed *url.URL, props map[string]string) (IO, error) + +// Register adds a new scheme factory to the registry. If the scheme is already registered, it will be replaced. +func Register(scheme string, factory SchemeFactory) { + if factory == nil { + panic("io: Register factory is nil") + } + defaultRegistry.set(scheme, factory) Review Comment: Should we have something here to handle a case where there's already something registered for a given scheme? In this current version, it looks like a user importing two packages registering themselves on the same scheme could result in a surprising behavior. For reference, `database/sql` [actually checks for prior existence of a driver and panics](https://cs.opensource.google/go/go/+/refs/tags/go1.25.7:src/database/sql/sql.go;l=56) in case of a duplicate registration. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
