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
package main | |
// Job define job interface | |
type Job interface { | |
Execute() | |
} | |
// JobQueue queue of jobs | |
var JobQueue chan Job |
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
package main | |
import ( | |
"sync" | |
"time" | |
) | |
// Job define job interface | |
type Job interface { | |
Execute() |
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
package main | |
import ( | |
"errors" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
"sync" |
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 |
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
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
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
// 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) 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
// 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()) |
OlderNewer