cloth

package module
v0.0.0-...-f9f445b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2016 License: MIT Imports: 10 Imported by: 1

README

Cloth

Build Status Coverage Status Go Report Card GoDoc GitHub license

Under development

ORM over Cloud Bigtable by Golang

Installation

$ go get -u github.com/abema/cloth

Quick Start

package main

import (
	"fmt"
	"time"

	"github.com/abema/cloth"
	"golang.org/x/net/context"
	"google.golang.org/cloud"
	"google.golang.org/cloud/bigtable"
	"google.golang.org/cloud/bigtable/bttest"
	"google.golang.org/grpc"
)

const (
	project = "cloth"
	zone    = "local"
	cluster = "dummy"

	tbl = "myTable"
	cf  = "myFamily"
)

// User data model.
type User struct {
	ID         string `bigtable:",rowkey"`
	Name       string `bigtable:"name"`
	Content    string `bigtable:"content, omitempty"`
	Purchased  bool   `bigtable:"purchased"`
	LoggedInAt int64  `bigtable:"-"`
	CreatedAt  int64  `bigtable:"createdAt"`
}

var srv *bttest.Server

func init() {

	var err error

	srv, err = bttest.NewServer()
	if err != nil {
		panic(err)
	}

	conn, err := grpc.Dial(srv.Addr)
	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	admin, err := bigtable.NewAdminClient(ctx, project, zone, cluster, cloud.WithBaseGRPC(conn))
	if err != nil {
		panic(err)
	}
	defer admin.Close()

	err = admin.CreateTable(ctx, tbl)
	if err != nil {
		panic(err)
	}

	err = admin.CreateColumnFamily(ctx, tbl, cf)
	if err != nil {
		panic(err)
	}
}

func main() {

	// test struct
	user := User{
		ID:         "vytxeTZskVKR7C7WgdSP3d",
		Name:       "osamingo",
		Content:    "WRYYY!",
		Purchased:  true,
		LoggedInAt: time.Now().Unix(),
		CreatedAt:  time.Now().Add(-24 * time.Hour).Unix(),
	}

	// dialing to test server
	conn, err := grpc.Dial(srv.Addr)
	if err != nil {
		fmt.Println(err)
		return
	}

	// generate a client
	client, err := bigtable.NewClient(context.Background(), project, zone, cluster, cloud.WithBaseGRPC(conn))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer client.Close()

	// generate Mutation
	mutation, err := cloth.GenerateMutation(cf, bigtable.Now(), &user)
	if err != nil {
		fmt.Println(err)
		return
	}

	// apply
	err = client.Open(tbl).Apply(context.Background(), user.ID, mutation)
	if err != nil {
		fmt.Println(err)
		return
	}

	// read a row
	row, err := client.Open(tbl).ReadRow(context.Background(), user.ID)
	if err != nil {
		fmt.Println(err)
		return
	}

	// convert
	target := User{}
	err = cloth.ReadItems(row[cf], &target)
	if err != nil {
		fmt.Println(err)
		return
	}

	// check
	if target.ID == user.ID {
		fmt.Println("ID:\t\tOK")
	}
	if target.Name == user.Name {
		fmt.Println("Name:\t\tOK")
	}
	if target.Content == user.Content {
		fmt.Println("Content:\tOK")
	}
	if target.Purchased == user.Purchased {
		fmt.Println("Purchased:\tOK")
	}
	if target.LoggedInAt == 0 {
		fmt.Println("LoggedInAt:\tOK (shoud be zero value)")
	}
	if target.CreatedAt == user.CreatedAt {
		fmt.Println("CreatedAt:\tOK")
	}
}

License

Released under the MIT License

Documentation

Index

Constants

View Source
const (
	// BigtableTagName is a "bigtable"
	BigtableTagName = "bigtable"
	// ColumnQualifierDelimiter is a ":"
	ColumnQualifierDelimiter = ":"
)

Variables

This section is empty.

Functions

func GenerateColumnQualifiersMutation

func GenerateColumnQualifiersMutation(family string, t time.Time, slice interface{}) (m *bigtable.Mutation, err error)

GenerateColumnQualifiersMutation generates Mutation from Slice.

func GenerateColumnsMutation

func GenerateColumnsMutation(family string, t time.Time, i interface{}) (m *bigtable.Mutation, err error)

GenerateColumnsMutation generates Mutation from Struct.

func ReadColumnQualifier

func ReadColumnQualifier(ris []bigtable.ReadItem) (cqs []string)

ReadColumnQualifier returns column qualifiers.

func ReadItems

func ReadItems(ris []bigtable.ReadItem, s interface{}) (err error)

ReadItems converts Mutation into Struct.

func SetColumnQualifiers

func SetColumnQualifiers(family string, t time.Time, m *bigtable.Mutation, slice interface{}) (err error)

SetColumnQualifiers sets column qualifiers of Mutation by Slice.

func SetColumns

func SetColumns(family string, t time.Time, m *bigtable.Mutation, i interface{}) (err error)

SetColumns sets columns of Mutation by Struct.

Types

type TagInfo

type TagInfo struct {
	Ignore    bool
	Omitempty bool
	RowKey    bool
	Qualifier bool
	Column    string
}

TagInfo is a field tag information.

func GetBigtableTagInfo

func GetBigtableTagInfo(tag string) (ti TagInfo)

GetBigtableTagInfo gets TagInfo by a field tag.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL