Skip to content

Instantly share code, notes, and snippets.

@cn007b
cn007b / ValueObject.static.php
Last active August 20, 2018 13:59
Value Object instead of Form - ValueObject static
<?php
class ValueObject
{
protected $name;
private function __construct()
{}
public static function fromArray(array $args)
{
if (!isset($args['name']) || $args['name'] === '') {
throw new InvalidArgumentException('name cannot be blank');
@cn007b
cn007b / Implementation1.controller.go
Last active September 24, 2018 20:16
eop - Implementation1 controller
// controller
func SignUp(username string) {
msg := "ok"
if err := service.SignUp(username); err != nil {
msg = err.Error()
}
fmt.Printf("[error] SignUp: %s \n", msg)
}
@cn007b
cn007b / Implementation1.service.go
Created September 24, 2018 20:19
eop - Implementation1 service
// 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 {
@cn007b
cn007b / superSimpleBashExample.sh
Created October 10, 2018 18:08
Super simple #bash example
#!/bin/bash
# set -x
f() {
echo "got: ${1}"
}
for i in $(seq 1 3); do
r=$(f ${i})
@cn007b
cn007b / example1.go
Last active October 26, 2018 20:54
GO ValueObject - example 1
func CreateNewUser() {
name := ...
email := ...
// ...
db.SaveUser(name, email)
mailer.SendEmail(name, email)
search.AddUser(name, email)
}
@cn007b
cn007b / example2.go
Created October 26, 2018 21:43
GO ValueObject - example 2
type UserInput struct {
Name string
Email string
}
@cn007b
cn007b / vo1.go
Last active October 26, 2018 22:24
GO ValueObject - vo part 1
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
@cn007b
cn007b / vo2.go
Last active October 26, 2018 22:50
GO ValueObject - vo part 2
// 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
@cn007b
cn007b / vo3.go
Last active October 27, 2018 13:06
GO ValueObject - vo part 3
// 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
@cn007b
cn007b / vo4.go
Created October 27, 2018 13:13
GO ValueObject - vo part 4
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