Skip to content

Commit

Permalink
Fix concurrency issue (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
crossworth authored Aug 24, 2021
1 parent 609fd7d commit 13abc55
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"regexp"
"strings"
"sync"
)

// A matching is between struct fields and database columns.
Expand Down Expand Up @@ -52,6 +53,9 @@ func (m *matching) columnOf(field string) *column {
//
// Panics if datatype does not point at a struct, or if the struct has no usable fields.
func matchingOf(datatype interface{}) *matching {
matchingCacheMu.Lock()
defer matchingCacheMu.Unlock()

v := reflect.ValueOf(datatype)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
panic("sx: expected a pointer to a struct")
Expand Down Expand Up @@ -107,6 +111,7 @@ func matchingOf(datatype interface{}) *matching {

// Cache to keep track of struct types that have been seen and therefore analyzed.
var matchingCache = make(map[reflect.Type]*matching)
var matchingCacheMu sync.Mutex

// Snake-casing logic.

Expand Down

0 comments on commit 13abc55

Please sign in to comment.