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 CreateNewUser() { | |
vo, err := CreateUserVO.New(params) // params - map with vo params | |
if err != nil { | |
// just handle errors: vo.GetErrors() | |
} | |
// ... | |
db.SaveUser(vo) | |
mailer.SendEmail(vo) | |
search.AddUser(vo) | |
} |
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 CreateUserVO | |
import "errors" | |
type Instance struct { | |
errors map[string]string | |
name string | |
email string | |
} |
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 (i Instance) GetErrors() map[string]string { | |
return i.errors | |
} | |
func (i Instance) GetName() string { | |
return i.name | |
} | |
func (i Instance) GetEmail() string { | |
return i.email |
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
// initName performs naive name validation (just for sake of example) | |
// and provides 100% guarantee that vo contains valid name. | |
func (i *Instance) initName(data map[string]string) { | |
name, exists := data["name"] | |
if !exists || name == "" { | |
i.errors["name"] = "invalid name" | |
return | |
} | |
i.name = 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
// New creates new value object instance and performs validation. | |
func New(data map[string]string) (Instance, error) { // Instance intentionally value, not pointer | |
vo := Instance{} | |
vo.errors = make(map[string]string) | |
vo.initName(data) | |
vo.initEmail(data) | |
if len(vo.errors) > 0 { | |
// if you like panic, you may panic here |
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 CreateUserVO | |
import "errors" | |
type Instance struct { | |
// errors contains all validation errors. | |
errors map[string]string | |
name string // intentionally non-exported | |
email string // intentionally non-exported |
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 UserInput struct { | |
Name string | |
Email string | |
} |
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 CreateNewUser() { | |
name := ... | |
email := ... | |
// ... | |
db.SaveUser(name, email) | |
mailer.SendEmail(name, email) | |
search.AddUser(name, email) | |
} |
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
#!/bin/bash | |
# set -x | |
f() { | |
echo "got: ${1}" | |
} | |
for i in $(seq 1 3); do | |
r=$(f ${i}) |
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
// service | |
func SignUp(username string) error { | |
if err := validation(username); err != nil { | |
return fmt.Errorf("validation failed, error: %s", err) | |
} | |
if err := signUpFacebook(username); err != nil { | |
return fmt.Errorf("facebook sign up failed, error: %s", err) | |
} | |
if err := signUpTwitter(username); err != nil { |