Skip to content

Instantly share code, notes, and snippets.

View jfeng45's full-sized avatar

Jin Feng jfeng45

View GitHub Profile
@jfeng45
jfeng45 / serviceContainer.go
Last active July 25, 2019 11:55
init in app container
type ServiceContainer struct {
FactoryMap map[string]interface{}
AppConfig *config.AppConfig
}
func (sc *ServiceContainer) InitApp(filename string) error {
var err error
config, err := loadConfig(filename)
if err != nil {
return errors.Wrap(err, "loadConfig")
// Build creates concrete type for RegistrationUseCaseInterface
func (rf *RegistrationFactory) Build(c container.Container, appConfig *config.AppConfig, key string) (UseCaseInterface, error) {
uc := appConfig.UseCase.Registration
udi, err := buildUserData(c, &uc.UserDataConfig)
if err != nil {
return nil, errors.Wrap(err, "")
}
tdi, err := buildTxData(c, &uc.TxDataConfig)
if err != nil {
return nil, errors.Wrap(err, "")
@jfeng45
jfeng45 / logFactory.go
Last active July 25, 2019 12:10
log factory
// logger mapp to map logger code to logger builder
var logfactoryBuilderMap = map[string]logFbInterface{
config.ZAP: &ZapFactory{},
config.LOGRUS: &LogrusFactory{},
}
// interface for logger factory
type logFbInterface interface {
Build(*config.LogConfig) error
}
@jfeng45
jfeng45 / zapFactory.go
Last active July 25, 2019 12:10
zap factory
// receiver for zap factory
type ZapFactory struct{}
// build zap logger
func (mf *ZapFactory) Build(lc *config.LogConfig) error {
err := zap.RegisterLog(*lc)
if err != nil {
return errors.Wrap(err, "")
}
return nil
import (
"github.com/go-ozzo/ozzo-validation"
"time"
)
// User has a name, department and created date. Name and created are required, department is optional.
// Id is auto-generated by database after the user is persisted.
// json is for couchdb
type User struct {
Id int `json:"uid"`
// GrpcToUser converts from grpc User type to domain Model user type
func GrpcToUser(user *uspb.User) (*model.User, error) {
if user == nil {
return nil, nil
}
resultUser := model.User{}
resultUser.Id = int(user.Id)
resultUser.Name = user.Name
resultUser.Department = user.Department
// userDataServiceFactory is a empty receiver for Build method
type userDataServiceFactory struct {}
func (udsf *userDataServiceFactory) Build(c container.Container, dataConfig *configs.DataConfig) (DataServiceInterface, error) {
logger.Log.Debug("userDataServiceFactory")
key := dataConfig.Code
if USER_DATA != key {
errMsg := USER_DATA + " in userDataServiceFactory doesn't match key = " + key
return nil, errors.New(errMsg)
}
// RegistrationUseCaseInterface is for users to register themselves to an application. It has registration related functions.
// ModifyAndUnregisterWithTx() is the one supporting transaction, the other are not.
type RegistrationUseCaseInterface interface {
// RegisterUser register a user to an application, basically save it to a database. The returned resultUser that has
// a Id ( auto generated by database) after persisted
RegisterUser(user *model.User) (resultUser *model.User, err error)
// UnregisterUser unregister a user from an application by user name, basically removing it from a database.
UnregisterUser(username string) error
// ModifyUser change user information based on the User.Id passed in.
ModifyUser(user *model.User) error
// RegistrationUseCase implements RegistrationUseCaseInterface.
// It has UserDataInterface, which can be used to access persistence layer
// TxDataInterface is needed to support transaction
type RegistrationUseCase struct {
UserDataInterface dataservice.UserDataInterface
TxDataInterface dataservice.TxDataInterface
}
func (ruc *RegistrationUseCase) RegisterUser(user *model.User) (*model.User, error) {
err := user.Validate()
// UserDataInterface represents interface for user data access through database
type UserDataInterface interface {
// Remove deletes a user by user name from database.
Remove(username string) (rowsAffected int64, err error)
// Find retrieves a user from database based on a user's id
Find(id int) (*model.User, error)
// FindByName retrieves a user from database by User.Name
FindByName(name string) (user *model.User, err error)
// FindAll retrieves all users from database as an array of user
FindAll() ([]model.User, error)