...

Source file src/github.com/go-kivik/kivik/v4/internal/registry/registry.go

Documentation: github.com/go-kivik/kivik/v4/internal/registry

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  // Package registry handles driver registrations. It's in a separate package
    14  // to facilitate testing.
    15  package registry
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/go-kivik/kivik/v4/driver"
    21  )
    22  
    23  var (
    24  	driversMu sync.RWMutex
    25  	drivers   = make(map[string]driver.Driver)
    26  )
    27  
    28  // Register makes a database driver available by the provided name. If Register
    29  // is called twice with the same name or if driver is nil, it panics.
    30  //
    31  // driver must be a driver.Driver or driver.LegacyDriver.
    32  func Register(name string, drv driver.Driver) {
    33  	if drv == nil {
    34  		panic("kivik: Register driver is nil")
    35  	}
    36  	driversMu.Lock()
    37  	defer driversMu.Unlock()
    38  	if _, dup := drivers[name]; dup {
    39  		panic("kivik: Register called twice for driver " + name)
    40  	}
    41  	drivers[name] = drv
    42  }
    43  
    44  // Driver returns the driver registered with the requested name, or nil if
    45  // it has not been registered.
    46  func Driver(name string) driver.Driver {
    47  	driversMu.RLock()
    48  	defer driversMu.RUnlock()
    49  	return drivers[name]
    50  }
    51  

View as plain text