Skip to content

Commit

Permalink
Refactor registries (#32)
Browse files Browse the repository at this point in the history
* Refactor registries.

* Remove unused code. Lengthen the timeout for the golangci-lint GitHub Action.
  • Loading branch information
bobg authored May 13, 2023
1 parent 71cb1db commit 64ad97a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
args: --timeout=2m

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
14 changes: 3 additions & 11 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"reflect"
"sort"
"sync"

"github.com/bobg/errors"
"github.com/bobg/go-generics/v2/maps"
Expand All @@ -19,10 +18,7 @@ import (
"gopkg.in/yaml.v3"
)

var (
fileRegistryMu sync.Mutex
fileRegistry = make(map[string]*files)
)
var filesRegistry = newRegistry[*files]()

// Files creates a target that contains a list of input files
// and a list of expected output files.
Expand Down Expand Up @@ -81,11 +77,9 @@ func Files(target Target, in, out []string) Target {
Out: out,
}

fileRegistryMu.Lock()
for _, o := range out {
fileRegistry[o] = result
filesRegistry.add(o, result)
}
fileRegistryMu.Unlock()

return result
}
Expand Down Expand Up @@ -177,13 +171,11 @@ func (ft *files) computeHash(ctx context.Context, con *Controller) ([]byte, erro
func (ft *files) runPrereqs(ctx context.Context, con *Controller) error {
var prereqs []Target

fileRegistryMu.Lock()
for _, in := range ft.In {
if target, ok := fileRegistry[in]; ok {
if target, ok := filesRegistry.lookup(in); ok {
prereqs = append(prereqs, target)
}
}
fileRegistryMu.Unlock()

if len(prereqs) == 0 {
return nil
Expand Down
1 change: 1 addition & 0 deletions golang/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestDeps(t *testing.T) {
"../proto/proto_test.go",
"../register.go",
"../register_test.go",
"../registry.go",
"../runner.go",
"../runner_test.go",
"../seq.go",
Expand Down
25 changes: 25 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fab

import "sync"

type registry[T any] struct {
mu sync.Mutex
items map[string]T
}

func newRegistry[T any]() *registry[T] {
return &registry[T]{items: make(map[string]T)}
}

func (r *registry[T]) add(name string, val T) {
r.mu.Lock()
r.items[name] = val
r.mu.Unlock()
}

func (r *registry[T]) lookup(name string) (T, bool) {
r.mu.Lock()
val, ok := r.items[name]
r.mu.Unlock()
return val, ok
}
31 changes: 7 additions & 24 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@ type (
)

var (
yamlTargetRegistryMu sync.Mutex
yamlTargetRegistry = make(map[string]YAMLTargetFunc)

yamlStringListRegistryMu sync.Mutex
yamlStringListRegistry = make(map[string]YAMLStringListFunc)
yamlTargetRegistry = newRegistry[YAMLTargetFunc]()
yamlStringListRegistry = newRegistry[YAMLStringListFunc]()
)

// RegisterYAMLTarget places a function in the YAML target registry with the given name.
// Use a YAML `!name` tag to introduce a node that should be parsed using this function.
func RegisterYAMLTarget(name string, fn YAMLTargetFunc) {
yamlTargetRegistryMu.Lock()
yamlTargetRegistry[name] = fn
yamlTargetRegistryMu.Unlock()
yamlTargetRegistry.add(name, fn)
}

// YAMLTarget parses a [Target] from a YAML node.
Expand All @@ -50,10 +45,7 @@ func RegisterYAMLTarget(name string, fn YAMLTargetFunc) {
// (e.g. x/foo or ../a/b/foo).
func (con *Controller) YAMLTarget(node *yaml.Node, dir string) (Target, error) {
if tag := normalizeTag(node.Tag); tag != "" {
yamlTargetRegistryMu.Lock()
fn, ok := yamlTargetRegistry[tag]
yamlTargetRegistryMu.Unlock()

fn, ok := yamlTargetRegistry.lookup(tag)
if !ok {
return nil, fmt.Errorf("unknown YAML target type %s", tag)
}
Expand Down Expand Up @@ -281,9 +273,7 @@ func openFabYAML(dir string) (*os.File, error) {
// RegisterYAMLStringList places a function in the YAML string-list registry with the given name.
// Use a YAML `!name` tag to introduce a node that should be parsed using this function.
func RegisterYAMLStringList(name string, fn YAMLStringListFunc) {
yamlStringListRegistryMu.Lock()
yamlStringListRegistry[name] = fn
yamlStringListRegistryMu.Unlock()
yamlStringListRegistry.add(name, fn)
}

// YAMLStringList parses a []string from a YAML node.
Expand All @@ -300,14 +290,10 @@ func YAMLStringList(node *yaml.Node) ([]string, error) {
tag := normalizeTag(node.Tag)

if tag != "" {
yamlStringListRegistryMu.Lock()
fn, ok := yamlStringListRegistry[tag]
yamlStringListRegistryMu.Unlock()

fn, ok := yamlStringListRegistry.lookup(tag)
if !ok {
return nil, UnknownStringListTagError{Tag: tag}
}

return fn(node)
}

Expand Down Expand Up @@ -357,10 +343,7 @@ func YAMLStringListFromNodes(nodes []*yaml.Node) ([]string, error) {
return nil, BadYAMLNodeKindError{Got: node.Kind, Want: yaml.ScalarNode}
}

yamlStringListRegistryMu.Lock()
fn, ok := yamlStringListRegistry[tag]
yamlStringListRegistryMu.Unlock()

fn, ok := yamlStringListRegistry.lookup(tag)
if !ok {
return nil, UnknownStringListTagError{Tag: tag}
}
Expand Down

0 comments on commit 64ad97a

Please sign in to comment.