Repository: syncthing/syncthing
File: lib/fs/types.go (Go)

Code before:
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package fs

import "sync"

type FilesystemType string

// Option modifies a filesystem at creation. An option might be specific
// to a filesystem-type.
//
// String is used to detect options with the same effect, i.e. must be different
// for options with different effects. Meaning if an option has parameters, a
// representation of those must be part of the returned string.
type Option interface {
	String() string
	apply(Filesystem) Filesystem
}

// Factory function type for constructing a custom file system. It takes the following
// parameters:
// 1. The fsType (string, e.g. "basic", "fake")
// 2. The file system URI (string)
// 3. Options for the file system
type FilesystemFactory func(FilesystemType, string, ...Option) (Filesystem, error)

// For each registered file system type, a function to construct a file system.
var filesystemFactories map[FilesystemType]FilesystemFactory = make(map[FilesystemType]FilesystemFactory)
var filesystemFactoriesMutex sync.Mutex = sync.Mutex{}

// Register a function to be called when a filesystem is to be constructed with
// the specified fsType. The function will receive the URI for the file system as well
// as all options.
func RegisterFilesystemType(fsType FilesystemType, fn FilesystemFactory) {
	filesystemFactoriesMutex.Lock()
	defer filesystemFactoriesMutex.Unlock()
	filesystemFactories[fsType] = fn
}


Review comment (refactor):
I don't think the factory needs to get the FilesystemType as a parameter, as it is tied to the type we registered it under, slightly simplifying the interface. (If we had the same factory function for lots of different types for some reason we could wrap it when registering instead.)

Code after:
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package fs

import "sync"

type FilesystemType string

// Option modifies a filesystem at creation. An option might be specific
// to a filesystem-type.
//
// String is used to detect options with the same effect, i.e. must be different
// for options with different effects. Meaning if an option has parameters, a
// representation of those must be part of the returned string.
type Option interface {
	String() string
	apply(Filesystem) Filesystem
}

// Factory function type for constructing a custom file system. It takes the URI
// and options as its parameters.
type FilesystemFactory func(string, ...Option) (Filesystem, error)

// For each registered file system type, a function to construct a file system.
var filesystemFactories map[FilesystemType]FilesystemFactory = make(map[FilesystemType]FilesystemFactory)
var filesystemFactoriesMutex sync.Mutex = sync.Mutex{}

// Register a function to be called when a filesystem is to be constructed with
// the specified fsType. The function will receive the URI for the file system as well
// as all options.
func RegisterFilesystemType(fsType FilesystemType, fn FilesystemFactory) {
	filesystemFactoriesMutex.Lock()
	defer filesystemFactoriesMutex.Unlock()
	filesystemFactories[fsType] = fn
}