This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PrioritizedEntity interface | |
type PrioritizedEntity interface { | |
GetPriority() int | |
AccessResource(interface{}) | |
} | |
// GreedyMutex a mutex that handles greedy entities with higher priority | |
type GreedyMutex struct { | |
sync.Mutex | |
TimeToWait time.Duration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Update update data | |
func (crud *CRUD) Update(data interface{}) error { | |
_, err := crud.Config.DB.NamedExec(crud.Config.sqlCRUDUpdate, data) | |
if err != nil { | |
return errors.Wrap(err, "error crud update") | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (crud *CRUD) registerU() gomHTTP.ServerRoute { | |
// build update sql | |
fields := crud.Config.updateFields | |
if len(fields) == 0 { | |
// allow update all fields | |
fields = crud.Config.fields | |
} | |
fieldNames := make([]string, len(fields)) | |
for i, field := range fields { | |
fieldNames[i] = field.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Read read data | |
func (crud *CRUD) Read(data interface{}) (interface{}, error) { | |
rv := reflect.ValueOf(data) | |
rv = reflect.Indirect(rv) | |
pk := rv.Field(crud.Config.pk.index) | |
if !pk.CanInterface() { | |
return nil, errors.Errorf("table %s with primary key has wrong interface type", | |
crud.Config.TableName, crud.Config.pk.index) | |
} | |
rows, err := crud.Config.DB.Queryx(crud.Config.sqlCRUDRead, pk.Interface()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (crud *CRUD) registerR() gomHTTP.ServerRoute { | |
// build select sql | |
fields := crud.Config.selectFields | |
if len(fields) == 0 { | |
// allow select all fields | |
fields = crud.Config.fields | |
} | |
fieldNames := make([]string, len(fields)) | |
for i, field := range fields { | |
fieldNames[i] = field.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create creates from map | |
func (crud *CRUD) Create(data interface{}) error { | |
// build fields | |
result, err := crud.Config.DB.NamedExec(crud.Config.sqlCRUDCreate, data) | |
if err != nil { | |
return errors.Wrap(err, "error crud create") | |
} | |
// set primary key | |
rv := reflect.ValueOf(data) | |
rv = reflect.Indirect(rv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (crud *CRUD) registerC() gomHTTP.ServerRoute { | |
// build create sql | |
fields := crud.Config.createFields | |
if len(fields) == 0 { | |
// allow create all fields | |
fields = crud.Config.fields | |
} | |
fieldNames := make([]string, len(fields)) | |
for i, field := range fields { | |
fieldNames[i] = field.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Validator func(method string, obj interface{}) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Object target crud object | |
type Object interface { | |
Get() interface{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Config defines crud properties | |
type Config struct { | |
DB *sqlx.DB | |
TableName string | |
Object Object | |
L bool | |
C bool | |
R bool | |
U bool | |
D bool |