Skip to content

Instantly share code, notes, and snippets.

View romanyx's full-sized avatar

Roman Budnikov romanyx

View GitHub Profile
@romanyx
romanyx / user_factory.go
Created May 24, 2018 10:40
Golang factory example
type UserFactory struct {
db *reform.DB
}
func NewUserFactory(db *sql.DB) *UserFactory {
return &UserFactory{
db: NewDB(db),
}
}
type Option func(*Client)
func SetHTTPClient(httpClient *http.Client) Option {
return func(cli *Client) {
cli.httpClient = httpClient
}
}
func NewClient(api, secret string, options ...Option) *Client {
cli := Client{
const (
okResponse = `{
"users": [
{"id": 1, "name": "Roman"},
{"id": 2, "name": "Dmitry"}
]
}`
)
func TestClientGetUsers(t *testing.T) {
func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
s := httptest.NewServer(handler)
cli := &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, _ string) (net.Conn, error) {
return net.Dial(network, s.Listener.Addr().String())
},
},
}
package example
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
@romanyx
romanyx / Vim hardmode
Created May 6, 2018 15:15
Vim disable arrows, backspace and delete
" vim hardmode
inoremap <Up> <NOP>
inoremap <Down> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Del> <NOP>
inoremap <BS> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
import nullable "github.com/romanyx/nullable"
// BlogUpdateRequest Blog update request
// swagger:model BlogUpdateRequest
type BlogUpdateRequest struct {
// Blog markdown body
// Required: true
Markdown *string `json:"markdown"`
"BlogUpdateRequest": {
"title": "Blog update request",
"type": "object",
"properties": {
"title": {
"description": "Blog title",
"type": "string"
},
"markdown": {
"NullableInt": {
"type": "integer",
"example": 1525360737,
"x-go-type": {
"import": {
"package": "github.com/romanyx/nullable"
},
"type": "Int"
}
}
func updateBlog(db *sql.DB, id int64, req *models.BlogUpdateRequest) error {
columns := []string{"title", "body"}
values := []interface{*req.Title, *req.Body}
if req.PublishedAt.Present {
if req.PublishedAt.Valid {
values = append(values, req.PublishedAt.Value)
} else {
values = append(values, sql.NullInt64{})
}