<commit_before>// consistentHash project consistentHash.go
package consistentHash

import (
	"errors"
	"fmt"
	"github.com/spaolacci/murmur3"
	"sort"
	"strconv"
	"sync"
)

var (
	noMembersError                   = errors.New("no members added")
	notEnoughMembersError            = errors.New("not enough members")
	notAvailableOnceMembersAddedEror = errors.New("not available once members are added")
	invalidVnodeCountError           = errors.New("vnodeCount must be > 0")
)

const (
	// DefaultVnodeCount is a tradeoff of memory and ~ log(N) speed versus how well the hash spreads
	DefaultVnodeCount = 200
)

type vnode struct {
	token   uint64
	address string
}

type vnodes []vnode

type consistentHash struct {
	vnodes     vnodes
	nodes      map[string]bool
	vnodeCount int
	mutex      sync.Mutex
}

// NewConsistentHash creates a new consistentHash pointer and initializes all the necessary fields
func New() *consistentHash {
	ch := new(consistentHash)
	ch.nodes = make(map[string]bool)
	ch.vnodes = make(vnodes, 0)
	ch.vnodeCount = DefaultVnodeCount
	return ch
}

// dumpVnodes prints the vnode slice to stdout, only useful for debugging
func (ch *consistentHash) dumpVnodes() {
	for _, vn := range ch.vnodes {
		fmt.Printf("%v\n", vn)
	}
}

// addressToKey converts an address and an integer to a []byte that we are sure won't be duplicated with a later valid IP
// or hostname
func addressToKey(address string, increment int) []byte {
	return []byte(strconv.Itoa(increment) + "=" + address)
}

// SetVnodeCount sets the number of vnodes that will be added for every server
// This must be called before any Add() calls
func (ch *consistentHash) SetVnodeCount(count int) error {
	if len(ch.nodes) > 0 {
		return notAvailableOnceMembersAddedEror
	}
	if count < 1 {
		return invalidVnodeCountError
	}
	ch.vnodeCount = count
	return nil
}

// Add adds a server to the consistentHash
func (ch *consistentHash) Add(address string) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	// if the address has already been added, there is no work to do
	if _, found := ch.nodes[address]; found {
		return
	}
	ch.nodes[address] = true
	for i := 0; i < ch.vnodeCount; i++ {
		token := murmur3.Sum64(addressToKey(address, i))
		newVnode := vnode{token, address}
		ch.insertVnode(newVnode)
	}
}

// Remove removes a server from the consistentHash
func (ch *consistentHash) Remove(address string) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	if _, found := ch.nodes[address]; !found {
		return
	}
	for i := 0; i < ch.vnodeCount; i++ {
		token := murmur3.Sum64(addressToKey(address, i))
		ch.removeVnode(token)
	}
	delete(ch.nodes, address)
}

func (v *vnode) String() string {
	return fmt.Sprintf("token=%d address=%s", v.token, v.address)
}

// Get finds the closest member for a given key
func (ch *consistentHash) Get(key []byte) (string, error) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	if len(ch.vnodes) == 0 {
		return "", noMembersError
	}
	token := murmur3.Sum64(key)
	return ch.vnodes[ch.closest(token)].address, nil
}

// Get2 finds the closest 2 members for a given key and is just a helper function
// calling into GetN
func (ch *consistentHash) Get2(key []byte) (string, string, error) {
	// don't use the mutex since GetN will use it
	servers, err := ch.GetN(key, 2)
	if err != nil {
		return "", "", err
	}
	return servers[0], servers[1], nil

}

// GetN finds the closest N members for a given key
func (ch *consistentHash) GetN(key []byte, count int) ([]string, error) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	if len(ch.nodes) < count {
		return nil, notEnoughMembersError
	}
	token := murmur3.Sum64(key)
	addressMap := make(map[string]bool)
	addresses := make([]string, count)
	index := ch.closest(token)
	found := 0
	for found < count {
		if exists := addressMap[ch.vnodes[index].address]; !exists {
			addressMap[ch.vnodes[index].address] = true
			addresses[found] = ch.vnodes[index].address
			found++
		}
		index++
		if index == len(ch.vnodes) {
			index = 0
		}
	}
	return addresses, nil

}

// removeVnode removes a vnode from the ring
func (ch *consistentHash) removeVnode(token uint64) {
	index := ch.index(token)
	if index == len(ch.vnodes) {
		ch.vnodes = ch.vnodes[:index-1]
		return
	}
	ch.vnodes = append(ch.vnodes[:index], ch.vnodes[index+1:]...)
}

// insertVnode adds a vnode into the appropriate location of the ring
func (ch *consistentHash) insertVnode(vn vnode) {
	index := ch.index(vn.token)
	ch.vnodes = append(ch.vnodes[:index], append(vnodes{vn}, ch.vnodes[index:]...)...)
}

// index returns the position where we should insert a new vnode
// differs from closest in that if the new token is bigger than the current highest token
// the index returned should be the end
func (ch *consistentHash) index(token uint64) int {
	index := sort.Search(len(ch.vnodes), func(i int) bool {
		return ch.vnodes[i].token >= token
	})
	return index
}

// closest returns the index of the vnode greater than or equal to the token
func (ch *consistentHash) closest(token uint64) int {
	index := sort.Search(len(ch.vnodes), func(i int) bool {
		return ch.vnodes[i].token >= token
	})
	if index == len(ch.vnodes) {
		index = 0
	}
	return index
}
<commit_msg>export struct<commit_after>// consistentHash project consistentHash.go
package consistentHash

import (
	"errors"
	"fmt"
	"github.com/spaolacci/murmur3"
	"sort"
	"strconv"
	"sync"
)

var (
	noMembersError                   = errors.New("no members added")
	notEnoughMembersError            = errors.New("not enough members")
	notAvailableOnceMembersAddedEror = errors.New("not available once members are added")
	invalidVnodeCountError           = errors.New("vnodeCount must be > 0")
)

const (
	// DefaultVnodeCount is a tradeoff of memory and ~ log(N) speed versus how well the hash spreads
	DefaultVnodeCount = 200
)

type vnode struct {
	token   uint64
	address string
}

type vnodes []vnode

type ConsistentHash struct {
	vnodes     vnodes
	nodes      map[string]bool
	vnodeCount int
	mutex      sync.Mutex
}

// NewConsistentHash creates a new consistentHash pointer and initializes all the necessary fields
func New() *ConsistentHash {
	ch := new(ConsistentHash)
	ch.nodes = make(map[string]bool)
	ch.vnodes = make(vnodes, 0)
	ch.vnodeCount = DefaultVnodeCount
	return ch
}

// dumpVnodes prints the vnode slice to stdout, only useful for debugging
func (ch *ConsistentHash) dumpVnodes() {
	for _, vn := range ch.vnodes {
		fmt.Printf("%v\n", vn)
	}
}

// addressToKey converts an address and an integer to a []byte that we are sure won't be duplicated with a later valid IP
// or hostname
func addressToKey(address string, increment int) []byte {
	return []byte(strconv.Itoa(increment) + "=" + address)
}

// SetVnodeCount sets the number of vnodes that will be added for every server
// This must be called before any Add() calls
func (ch *ConsistentHash) SetVnodeCount(count int) error {
	if len(ch.nodes) > 0 {
		return notAvailableOnceMembersAddedEror
	}
	if count < 1 {
		return invalidVnodeCountError
	}
	ch.vnodeCount = count
	return nil
}

// Add adds a server to the consistentHash
func (ch *ConsistentHash) Add(address string) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	// if the address has already been added, there is no work to do
	if _, found := ch.nodes[address]; found {
		return
	}
	ch.nodes[address] = true
	for i := 0; i < ch.vnodeCount; i++ {
		token := murmur3.Sum64(addressToKey(address, i))
		newVnode := vnode{token, address}
		ch.insertVnode(newVnode)
	}
}

// Remove removes a server from the consistentHash
func (ch *ConsistentHash) Remove(address string) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	if _, found := ch.nodes[address]; !found {
		return
	}
	for i := 0; i < ch.vnodeCount; i++ {
		token := murmur3.Sum64(addressToKey(address, i))
		ch.removeVnode(token)
	}
	delete(ch.nodes, address)
}

func (v *vnode) String() string {
	return fmt.Sprintf("token=%d address=%s", v.token, v.address)
}

// Get finds the closest member for a given key
func (ch *ConsistentHash) Get(key []byte) (string, error) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	if len(ch.vnodes) == 0 {
		return "", noMembersError
	}
	token := murmur3.Sum64(key)
	return ch.vnodes[ch.closest(token)].address, nil
}

// Get2 finds the closest 2 members for a given key and is just a helper function
// calling into GetN
func (ch *ConsistentHash) Get2(key []byte) (string, string, error) {
	// don't use the mutex since GetN will use it
	servers, err := ch.GetN(key, 2)
	if err != nil {
		return "", "", err
	}
	return servers[0], servers[1], nil

}

// GetN finds the closest N members for a given key
func (ch *ConsistentHash) GetN(key []byte, count int) ([]string, error) {
	ch.mutex.Lock()
	defer ch.mutex.Unlock()
	if len(ch.nodes) < count {
		return nil, notEnoughMembersError
	}
	token := murmur3.Sum64(key)
	addressMap := make(map[string]bool)
	addresses := make([]string, count)
	index := ch.closest(token)
	found := 0
	for found < count {
		if exists := addressMap[ch.vnodes[index].address]; !exists {
			addressMap[ch.vnodes[index].address] = true
			addresses[found] = ch.vnodes[index].address
			found++
		}
		index++
		if index == len(ch.vnodes) {
			index = 0
		}
	}
	return addresses, nil

}

// removeVnode removes a vnode from the ring
func (ch *ConsistentHash) removeVnode(token uint64) {
	index := ch.index(token)
	if index == len(ch.vnodes) {
		ch.vnodes = ch.vnodes[:index-1]
		return
	}
	ch.vnodes = append(ch.vnodes[:index], ch.vnodes[index+1:]...)
}

// insertVnode adds a vnode into the appropriate location of the ring
func (ch *ConsistentHash) insertVnode(vn vnode) {
	index := ch.index(vn.token)
	ch.vnodes = append(ch.vnodes[:index], append(vnodes{vn}, ch.vnodes[index:]...)...)
}

// index returns the position where we should insert a new vnode
// differs from closest in that if the new token is bigger than the current highest token
// the index returned should be the end
func (ch *ConsistentHash) index(token uint64) int {
	index := sort.Search(len(ch.vnodes), func(i int) bool {
		return ch.vnodes[i].token >= token
	})
	return index
}

// closest returns the index of the vnode greater than or equal to the token
func (ch *ConsistentHash) closest(token uint64) int {
	index := sort.Search(len(ch.vnodes), func(i int) bool {
		return ch.vnodes[i].token >= token
	})
	if index == len(ch.vnodes) {
		index = 0
	}
	return index
}
<|endoftext|>


<commit_before>package dingo

import (
	"errors"
	"fmt"
	"sync"
	"sync/atomic"

	"github.com/mission-liao/dingo/broker"
	"github.com/mission-liao/dingo/common"
	"github.com/mission-liao/dingo/transport"
)

//
// mapper container
//

type _mappers struct {
	workers *_workers
	mappers *common.Routines
	toLock  sync.Mutex
	to      atomic.Value
}

// allocating more mappers
//
// parameters:
// - tasks: input channel for transport.Task
// - receipts: output channel for broker.Receipt
func (me *_mappers) more(tasks <-chan *transport.Task, receipts chan<- *broker.Receipt) {
	go me._mapper_routine_(me.mappers.New(), me.mappers.Wait(), me.mappers.Events(), tasks, receipts)
}

// dispatching a 'transport.Task'
//
// parameters:
// - t: the task
// returns:
// - err: any error
func (me *_mappers) dispatch(t *transport.Task) (err error) {
	all := me.to.Load().(map[string]chan *transport.Task)
	if out, ok := all[t.Name()]; ok {
		out <- t
	} else {
		err = errWorkerNotFound
	}
	return
}

//
// proxy of _workers
//

func (me *_mappers) allocateWorkers(name string, count, share int) ([]<-chan *transport.Report, int, error) {
	me.toLock.Lock()
	defer me.toLock.Unlock()

	all := me.to.Load().(map[string]chan *transport.Task)
	if _, ok := all[name]; ok {
		return nil, count, errors.New(fmt.Sprintf("already registered: %v", name))
	}
	t := make(chan *transport.Task, 10)
	r, n, err := me.workers.allocate(name, t, nil, count, share)
	if err != nil {
		return r, n, err
	}

	alln := make(map[string]chan *transport.Task)
	for k := range all {
		alln[k] = all[k]
	}
	alln[name] = t
	me.to.Store(alln)
	return r, n, err
}

//
// common.Object interface
//

func (me *_mappers) Events() (ret []<-chan *common.Event, err error) {
	ret, err = me.workers.Events()
	if err != nil {
		return
	}

	ret = append(ret, me.mappers.Events())
	return
}

func (m *_mappers) Close() (err error) {
	m.mappers.Close()
	err = m.workers.Close()

	m.toLock.Lock()
	defer m.toLock.Unlock()

	all := m.to.Load().(map[string]chan *transport.Task)
	for _, v := range all {
		close(v)
	}
	m.to.Store(make(map[string]chan *transport.Task))

	return
}

// factory function
// parameters:
// - tasks: input channel
// returns:
// ...
func newMappers(trans *transport.Mgr) (m *_mappers, err error) {
	w, err := newWorkers(trans)
	if err != nil {
		return
	}

	m = &_mappers{
		workers: w,
		mappers: common.NewRoutines(),
	}

	m.to.Store(make(map[string]chan *transport.Task))
	return
}

//
// mapper routine
//

func (m *_mappers) _mapper_routine_(
	quit <-chan int,
	wait *sync.WaitGroup,
	events chan<- *common.Event,
	tasks <-chan *transport.Task,
	receipts chan<- *broker.Receipt,
) {
	defer wait.Done()
	for {
		select {
		case t, ok := <-tasks:
			if !ok {
				goto cleanup
			}

			// find registered worker
			err := m.dispatch(t)

			// compose a receipt
			var rpt broker.Receipt
			if err != nil {
				// send an error event
				events <- common.NewEventFromError(common.InstT.MAPPER, err)

				if err == errWorkerNotFound {
					rpt = broker.Receipt{
						Status: broker.Status.WORKER_NOT_FOUND,
					}
				} else {
					rpt = broker.Receipt{
						Status:  broker.Status.NOK,
						Payload: err,
					}
				}
			} else {
				rpt = broker.Receipt{
					Status: broker.Status.OK,
				}
			}
			receipts <- &rpt

		case <-quit:
			// clean up code below
			goto cleanup
		}
	}
cleanup:
	// TODO: cleanup
	close(receipts)
	return
}
<commit_msg>clean TODOs<commit_after>package dingo

import (
	"errors"
	"fmt"
	"sync"
	"sync/atomic"

	"github.com/mission-liao/dingo/broker"
	"github.com/mission-liao/dingo/common"
	"github.com/mission-liao/dingo/transport"
)

//
// mapper container
//

type _mappers struct {
	workers *_workers
	mappers *common.Routines
	toLock  sync.Mutex
	to      atomic.Value
}

// allocating more mappers
//
// parameters:
// - tasks: input channel for transport.Task
// - receipts: output channel for broker.Receipt
func (me *_mappers) more(tasks <-chan *transport.Task, receipts chan<- *broker.Receipt) {
	go me._mapper_routine_(me.mappers.New(), me.mappers.Wait(), me.mappers.Events(), tasks, receipts)
}

// dispatching a 'transport.Task'
//
// parameters:
// - t: the task
// returns:
// - err: any error
func (me *_mappers) dispatch(t *transport.Task) (err error) {
	all := me.to.Load().(map[string]chan *transport.Task)
	if out, ok := all[t.Name()]; ok {
		out <- t
	} else {
		err = errWorkerNotFound
	}
	return
}

//
// proxy of _workers
//

func (me *_mappers) allocateWorkers(name string, count, share int) ([]<-chan *transport.Report, int, error) {
	me.toLock.Lock()
	defer me.toLock.Unlock()

	all := me.to.Load().(map[string]chan *transport.Task)
	if _, ok := all[name]; ok {
		return nil, count, errors.New(fmt.Sprintf("already registered: %v", name))
	}
	t := make(chan *transport.Task, 10)
	r, n, err := me.workers.allocate(name, t, nil, count, share)
	if err != nil {
		return r, n, err
	}

	alln := make(map[string]chan *transport.Task)
	for k := range all {
		alln[k] = all[k]
	}
	alln[name] = t
	me.to.Store(alln)
	return r, n, err
}

//
// common.Object interface
//

func (me *_mappers) Events() (ret []<-chan *common.Event, err error) {
	ret, err = me.workers.Events()
	if err != nil {
		return
	}

	ret = append(ret, me.mappers.Events())
	return
}

func (m *_mappers) Close() (err error) {
	m.mappers.Close()
	err = m.workers.Close()

	m.toLock.Lock()
	defer m.toLock.Unlock()

	all := m.to.Load().(map[string]chan *transport.Task)
	for _, v := range all {
		close(v)
	}
	m.to.Store(make(map[string]chan *transport.Task))

	return
}

// factory function
// parameters:
// - tasks: input channel
// returns:
// ...
func newMappers(trans *transport.Mgr) (m *_mappers, err error) {
	w, err := newWorkers(trans)
	if err != nil {
		return
	}

	m = &_mappers{
		workers: w,
		mappers: common.NewRoutines(),
	}

	m.to.Store(make(map[string]chan *transport.Task))
	return
}

//
// mapper routine
//

func (m *_mappers) _mapper_routine_(
	quit <-chan int,
	wait *sync.WaitGroup,
	events chan<- *common.Event,
	tasks <-chan *transport.Task,
	receipts chan<- *broker.Receipt,
) {
	defer wait.Done()
	defer close(receipts)

	receive := func(t *transport.Task) {
		// find registered worker
		err := m.dispatch(t)

		// compose a receipt
		var rpt broker.Receipt
		if err != nil {
			// send an error event
			events <- common.NewEventFromError(common.InstT.MAPPER, err)

			if err == errWorkerNotFound {
				rpt = broker.Receipt{
					Status: broker.Status.WORKER_NOT_FOUND,
				}
			} else {
				rpt = broker.Receipt{
					Status:  broker.Status.NOK,
					Payload: err,
				}
			}
		} else {
			rpt = broker.Receipt{
				Status: broker.Status.OK,
			}
		}
		receipts <- &rpt
	}

finished:
	for {
		select {
		case t, ok := <-tasks:
			if !ok {
				break finished
			}
			receive(t)

		case <-quit:
			// clean up code below
			break finished
		}
	}

done:
	// consuming remaining tasks in channel.
	for {
		select {
		case t, ok := <-tasks:
			if !ok {
				break done
			}
			receive(t)
		default:
			break done
		}
	}
	return
}
<|endoftext|>


<commit_before>package qb

import (
	"fmt"
	"github.com/fatih/structs"
	"github.com/serenize/snaker"
	"strings"
)

const tagPrefix = "qb"

// NewMapper instantiates a new mapper object and returns it as a mapper pointer
func NewMapper(driver string) *Mapper {
	return &Mapper{
		driver: driver,
	}
}

// Mapper is the generic struct for struct to table mapping
type Mapper struct {
	driver string
}

func (m *Mapper) extractValue(value string) string {

	hasParams := strings.Contains(value, "(") && strings.Contains(value, ")")

	if hasParams {
		startIndex := strings.Index(value, "(")
		endIndex := strings.Index(value, ")")
		return value[startIndex+1 : endIndex]
	}

	return ""
}

// ConvertType returns the type mapping of column.
// If tagType is, then colType would automatically be resolved.
// If tagType is not "", then automatic type resolving would be overridden by tagType
func (m *Mapper) ConvertType(colType string, tagType string) *Type {

	// convert tagType
	if tagType != "" {
		tagType = strings.ToUpper(tagType)
		return &Type{func() string { return tagType }}
	}

	// convert default type
	switch colType {
	case "string":
		return VarChar()
	case "int":
		return Int()
	case "int64":
		return BigInt()
	case "float32":
		return Float()
	case "float64":
		return Float()
	case "bool":
		return Boolean()
	case "uuid.UUID":
		if m.driver == "postgres" {
			return UUID()
		}
		return VarChar(36)
	case "time.Time":
		return Timestamp()
	case "*time.Time":
		return Timestamp()
	default:
		return VarChar()
	}
}

// Convert parses struct and converts it to a new table
func (m *Mapper) Convert(model interface{}) (*Table, error) {

	modelName := snaker.CamelToSnake(structs.Name(model))

	table := &Table{
		name:        modelName,
		columns:     []Column{},
		constraints: []Constraint{},
		builder:     NewBuilder(),
	}

	fmt.Printf("model name: %s\n\n", modelName)

	var col Column
	var rawTag string

	for _, f := range structs.Fields(model) {

		colName := snaker.CamelToSnake(f.Name())
		colType := fmt.Sprintf("%T", f.Value())

		rawTag = f.Tag(tagPrefix)

		constraints := []Constraint{}
		fmt.Printf("field name: %s\n", colName)
		fmt.Printf("field raw tag: %s\n", rawTag)
		fmt.Printf("field type name: %T\n", f.Value())
		fmt.Printf("field constraints: %v\n", constraints)

		// clean trailing spaces of tag
		rawTag = strings.Replace(f.Tag(tagPrefix), " ", "", 1)

		// parse tag
		tag, err := ParseTag(rawTag)
		if err != nil {
			return nil, err
		}

		// convert tag into constraints
		var constraint Constraint
		for _, v := range tag.Constraints {
			if v == "null" {
				constraint = Null()
			} else if v == "notnull" {
				constraint = NotNull()
			} else if v == "unique" {
				constraint = Constraint{
					Name: "UNIQUE",
				}
			} else if v == "index" {
				constraint = Constraint{
					Name: "INDEX",
				}
			} else if strings.Contains(v, "default") {
				constraint = Default(m.extractValue(v))
			} else if strings.Contains(v, "primary_key") {
				table.AddPrimary(colName)
				continue
			} else if strings.Contains(v, "ref") && strings.Contains(v, "(") && strings.Contains(v, ")") {
				tc := strings.Split(m.extractValue(v), ".")
				table.AddRef(colName, tc[0], tc[1])
				continue
			} else {
				return nil, fmt.Errorf("Invalid constraint: %s", v)
			}
			constraints = append(constraints, constraint)
		}

		fmt.Printf("field tag.Type: %s\n", tag.Type)
		fmt.Printf("field tag.Constraints: %v\n", tag.Constraints)

		col = Column{
			Name:        colName,
			Constraints: constraints,
			Type:        m.ConvertType(colType, tag.Type),
		}

		table.AddColumn(col)

		fmt.Println()
	}

	return table, nil
}
<commit_msg>remove index tag temporarily, remove uuid type check because go standard lib doesn't have uuid library<commit_after>package qb

import (
	"fmt"
	"github.com/fatih/structs"
	"github.com/serenize/snaker"
	"strings"
)

const tagPrefix = "qb"

// NewMapper instantiates a new mapper object and returns it as a mapper pointer
func NewMapper(driver string) *Mapper {
	return &Mapper{
		driver: driver,
	}
}

// Mapper is the generic struct for struct to table mapping
type Mapper struct {
	driver string
}

func (m *Mapper) extractValue(value string) string {

	hasParams := strings.Contains(value, "(") && strings.Contains(value, ")")

	if hasParams {
		startIndex := strings.Index(value, "(")
		endIndex := strings.Index(value, ")")
		return value[startIndex+1 : endIndex]
	}

	return ""
}

// ConvertType returns the type mapping of column.
// If tagType is, then colType would automatically be resolved.
// If tagType is not "", then automatic type resolving would be overridden by tagType
func (m *Mapper) ConvertType(colType string, tagType string) *Type {

	// convert tagType
	if tagType != "" {
		tagType = strings.ToUpper(tagType)
		return &Type{func() string { return tagType }}
	}

	// convert default type
	switch colType {
	case "string":
		return VarChar()
	case "int":
		return Int()
	case "int64":
		return BigInt()
	case "float32":
		return Float()
	case "float64":
		return Float()
	case "bool":
		return Boolean()
	case "time.Time":
		return Timestamp()
	case "*time.Time":
		return Timestamp()
	default:
		return VarChar()
	}
}

// Convert parses struct and converts it to a new table
func (m *Mapper) Convert(model interface{}) (*Table, error) {

	modelName := snaker.CamelToSnake(structs.Name(model))

	table := &Table{
		name:        modelName,
		columns:     []Column{},
		constraints: []Constraint{},
		builder:     NewBuilder(),
	}

	fmt.Printf("model name: %s\n\n", modelName)

	var col Column
	var rawTag string

	for _, f := range structs.Fields(model) {

		colName := snaker.CamelToSnake(f.Name())
		colType := fmt.Sprintf("%T", f.Value())

		rawTag = f.Tag(tagPrefix)

		constraints := []Constraint{}
		fmt.Printf("field name: %s\n", colName)
		fmt.Printf("field raw tag: %s\n", rawTag)
		fmt.Printf("field type name: %T\n", f.Value())
		fmt.Printf("field constraints: %v\n", constraints)

		// clean trailing spaces of tag
		rawTag = strings.Replace(f.Tag(tagPrefix), " ", "", 1)

		// parse tag
		tag, err := ParseTag(rawTag)
		if err != nil {
			return nil, err
		}

		// convert tag into constraints
		var constraint Constraint
		for _, v := range tag.Constraints {
			if v == "null" {
				constraint = Null()
			} else if v == "notnull" {
				constraint = NotNull()
			} else if v == "unique" {
				constraint = Constraint{
					Name: "UNIQUE",
				}
			} else if strings.Contains(v, "default") {
				constraint = Default(m.extractValue(v))
			} else if strings.Contains(v, "primary_key") {
				table.AddPrimary(colName)
				continue
			} else if strings.Contains(v, "ref") && strings.Contains(v, "(") && strings.Contains(v, ")") {
				tc := strings.Split(m.extractValue(v), ".")
				table.AddRef(colName, tc[0], tc[1])
				continue
			} else {
				return nil, fmt.Errorf("Invalid constraint: %s", v)
			}
			constraints = append(constraints, constraint)
		}

		fmt.Printf("field tag.Type: %s\n", tag.Type)
		fmt.Printf("field tag.Constraints: %v\n", tag.Constraints)

		col = Column{
			Name:        colName,
			Constraints: constraints,
			Type:        m.ConvertType(colType, tag.Type),
		}

		table.AddColumn(col)

		fmt.Println()
	}

	return table, nil
}
<|endoftext|>


<commit_before>package api

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestGetHosts(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/hosts" {
			t.Errorf("expected HTTP request to /hosts, got %s", r.URL.Path)
		}

		fmt.Fprintln(w, `[
      {
        "id": "14dff6d8-3b9a-41be-9ffd-d0d054a17492",
        "url": "http://dummy/hosts/default_bfirsh",
        "name": "default_bfirsh"
      }
    ]`)
	}))
	defer ts.Close()

	client := HTTPClient{ts.URL, "dummy_token"}

	hosts, err := client.GetHosts()
	if err != nil {
		t.Error(err)
	}
	if len(hosts) != 1 {
		t.Errorf("expected 1 element, got %d (hosts: %v)", len(hosts), hosts)
	}
	if hosts[0].Name != "default_bfirsh" {
		t.Errorf("expected default_bfirsh, got %s (hosts: %v)", hosts[0].Name, hosts)
	}
}

func TestCreateHost(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/hosts" {
			t.Errorf("expected HTTP request to /hosts, got %s", r.URL.Path)
		}

		if r.Header.Get("Content-Type") != "application/json" {
			t.Errorf("expected application/json, got %s", r.Header.Get("Content-Type"))
		}

		body, _ := ioutil.ReadAll(r.Body)
		var data map[string]string
		json.Unmarshal(body, &data)

		if data["name"] != "newhost" {
			t.Errorf("expected 'newhost', got '%s'", data["name"])
		}

		w.WriteHeader(201)
		fmt.Fprintln(w, `{
      "id": "14dff6d8-3b9a-41be-9ffd-d0d054a17492",
      "url": "http://dummy/hosts/newhost",
      "name": "newhost"
    }`)
	}))

	client := HTTPClient{ts.URL, "dummy_token"}

	host, err := client.CreateHost("newhost")
	if err != nil {
		t.Error(err)
	}
	if host.Name != "newhost" {
		t.Errorf("expected 'newhost', got '%s' (host: %v)", host.Name, host)
	}
}

func TestDeleteHost(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "DELETE" || r.URL.Path != "/hosts/myhost" {
			t.Errorf("expected DELETE request to /hosts/myhost, got %s request to %s", r.Method, r.URL.Path)
		}

		fmt.Fprintln(w, "")
	}))

	client := HTTPClient{ts.URL, "dummy_token"}

	err := client.DeleteHost("myhost")
	if err != nil {
		t.Error(err)
	}
}

func TestDeleteHostError(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
		fmt.Fprintln(w, "I broke :(")
	}))

	client := HTTPClient{ts.URL, "dummy_token"}

	err := client.DeleteHost("myhost")
	if err == nil {
		t.Error("expected DeleteHost() to return an error")
	}
}
<commit_msg>Fix API test<commit_after>package api

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestGetHosts(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/hosts" {
			t.Errorf("expected HTTP request to /hosts, got %s", r.URL.Path)
		}

		fmt.Fprintln(w, `[
      {
        "id": "14dff6d8-3b9a-41be-9ffd-d0d054a17492",
        "url": "http://dummy/hosts/default_bfirsh",
        "name": "default_bfirsh"
      }
    ]`)
	}))
	defer ts.Close()

	client := HTTPClient{ts.URL, "dummy_token"}

	hosts, err := client.GetHosts()
	if err != nil {
		t.Error(err)
	}
	if len(hosts) != 1 {
		t.Errorf("expected 1 element, got %d (hosts: %v)", len(hosts), hosts)
	}
	if hosts[0].Name != "default_bfirsh" {
		t.Errorf("expected default_bfirsh, got %s (hosts: %v)", hosts[0].Name, hosts)
	}
}

func TestCreateHost(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/hosts" {
			t.Errorf("expected HTTP request to /hosts, got %s", r.URL.Path)
		}

		if r.Header.Get("Content-Type") != "application/json" {
			t.Errorf("expected application/json, got %s", r.Header.Get("Content-Type"))
		}

		body, _ := ioutil.ReadAll(r.Body)
		var data map[string]interface{}
		json.Unmarshal(body, &data)

		if data["name"] != "newhost" {
			t.Errorf("expected 'newhost', got '%s'", data["name"])
		}

		if int(data["size"].(float64)) != 512 {
			t.Errorf("expected 512, got %#v", data["size"])
		}

		w.WriteHeader(201)
		fmt.Fprintln(w, `{
      "id": "14dff6d8-3b9a-41be-9ffd-d0d054a17492",
      "url": "http://dummy/hosts/newhost",
      "name": "newhost"
    }`)
	}))

	client := HTTPClient{ts.URL, "dummy_token"}

	host, err := client.CreateHost("newhost", 512)
	if err != nil {
		t.Error(err)
	}
	if host.Name != "newhost" {
		t.Errorf("expected 'newhost', got '%s' (host: %v)", host.Name, host)
	}
}

func TestDeleteHost(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "DELETE" || r.URL.Path != "/hosts/myhost" {
			t.Errorf("expected DELETE request to /hosts/myhost, got %s request to %s", r.Method, r.URL.Path)
		}

		fmt.Fprintln(w, "")
	}))

	client := HTTPClient{ts.URL, "dummy_token"}

	err := client.DeleteHost("myhost")
	if err != nil {
		t.Error(err)
	}
}

func TestDeleteHostError(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
		fmt.Fprintln(w, "I broke :(")
	}))

	client := HTTPClient{ts.URL, "dummy_token"}

	err := client.DeleteHost("myhost")
	if err == nil {
		t.Error("expected DeleteHost() to return an error")
	}
}
<|endoftext|>


<commit_before>// Package matrix provides functions for simple linear algebra
package matrix

import (
	"bytes"
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"strings"
)

// Describes error during calculations
type MatrixError struct {
	ErrorString string
}

func (err *MatrixError) Error() string { return err.ErrorString }

var (
	ErrIncompatibleSizes = &MatrixError{"Incompatible sizes of matricies"}
	ErrOutOfBounds       = &MatrixError{"The element you are trying to access is out of bounds."}
)

// A function that will apply an abitary transformation to an element in the matrix
type ApplyFunc func(index int, value float64) float64

type Matrix struct {
	rows, cols int
	values     []float64
}

// Creates a new Matrix and initializes all values to 0
func Zeros(rows, cols int) *Matrix {
	A := new(Matrix)

	A.rows = rows
	A.cols = cols
	A.values = make([]float64, rows*cols)

	return A
}

// Creates a new Matrix and initializes all values to 1
func Ones(rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	A.AddNum(1)

	return A
}

// Creates a new identity matrix
func Eye(size int) *Matrix {
	A := Zeros(size, size)

	for i := 1; i <= size; i++ {
		A.Set(i, i, 1)
	}

	return A
}

// Constructs a new matrix with random values in range [0;1)
func Rand(rows, cols int) *Matrix {
	A := Zeros(rows, cols)

	for i := range A.values {
		A.values[i] = rand.Float64()
	}

	return A
}

// Constructs a new Matrix from a Matlab style representation
func FromMatlab(str string) *Matrix {
	rows := strings.Split(str, ";")

	for i, row := range rows {
		rows[i] = strings.Replace(row, ",", " ", -1)
	}

	nRows := len(rows)
	nColumns := len(strings.Fields(rows[0]))

	A := Zeros(nRows, nColumns)

	for i, row := range rows {
		row = strings.Trim(row, "[] ")
		strNums := strings.Fields(row)

		for j, num := range strNums {
			n, _ := strconv.ParseFloat(num, 64)
			A.Set(i+1, j+1, n)
		}
	}

	return A
}

// Return a Matlab representation of the matrix
func (A *Matrix) ToMatlab() string {
	buffer := new(bytes.Buffer)
	buffer.WriteString("[")

	for i, v := range A.values {
		buffer.WriteString(fmt.Sprintf("%f ", v))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("; ")
		}
	}

	buffer.WriteString("]")

	return buffer.String()
}

// Gives the dimensions of the matrix
func (A *Matrix) Dim() (int, int) {
	return A.rows, A.cols
}

// Return the number of rows in the matrix
func (A *Matrix) Rows() int {
	return A.rows
}

// Return the number of columns in the matrix
func (A *Matrix) Columns() int {
	return A.cols
}

// Returns an array of all the values
func (A *Matrix) Values() []float64 {
	tmp := make([]float64, len(A.values))
	copy(tmp, A.values)
	return tmp
}

// Returns an exact copy of the matrix
func (A *Matrix) Copy() *Matrix {
	B := Zeros(A.rows, A.cols)
	copy(B.values, A.values)

	return B
}

func (A *Matrix) Apply(f ApplyFunc) *Matrix {
	for i, v := range A.values {
		A.values[i] = f(i, v)
	}

	return A
}

// Returns a string representation of the matrix
func (A *Matrix) String() string {
	buffer := new(bytes.Buffer)

	for i, elem := range A.values {
		buffer.WriteString(fmt.Sprintf("%.3f ", elem))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("\n")
		}
	}

	return buffer.String()
}

// Retrieve value at [row, col]
func (A *Matrix) Get(row, col int) (float64, error) {

	if A.isOutOfBounds(row, col) {
		return 0, ErrOutOfBounds
	}

	return A.values[(row-1)*A.cols+col-1], nil
}

// Set the element at [row, col] to val
func (A *Matrix) Set(row, col int, val float64) error {

	if A.isOutOfBounds(row, col) {
		return ErrOutOfBounds
	}

	A.values[(row-1)*A.cols+col-1] = val

	return nil
}

func (A *Matrix) Sigmoid() *Matrix {
	for i, v := range A.values {
		A.values[i] = sigmoid(v)
	}

	return A
}

// Transpose the matrix (in-place, costly)
func (A *Matrix) Transpose() *Matrix {
	B := Zeros(A.cols, A.rows)

	for i := 1; i <= A.rows; i++ {
		for j := 1; j <= A.cols; j++ {
			v, _ := A.Get(i, j)
			B.Set(j, i, v)
		}
	}

	A.rows, A.cols = A.cols, A.rows
	copy(A.values, B.values)
	return A
}

// Add B to the matrix A (in-place)
func (A *Matrix) Add(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	for i, val := range B.values {
		A.values[i] += val
	}

	return A, nil
}

// Subtract B from the matrix A (in-place)
func (A *Matrix) Sub(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	for i, val := range B.values {
		A.values[i] -= val
	}

	return A, nil
}

// Multiply 2 matricies with each other returning a new matrix
func (A *Matrix) Mul(B *Matrix) (*Matrix, error) {
	if !columnIsRow(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, B.cols)

	for i := 0; i < C.rows; i++ {
		for j := 0; j < C.cols; j++ {
			sum := float64(0)

			for k := 0; k < A.cols; k++ {
				sum += A.values[i*A.cols+k] * B.values[k*B.cols+j]
			}

			C.values[i*C.rows+j] = sum
		}
	}

	return C, nil
}

// Standard scalar product of 2 matricies
func (A *Matrix) Dot(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	for i, v := range B.values {
		A.values[i] *= v
	}
	return A, nil
}

// Scale the matrix in-place by the factor f
func (A *Matrix) Scale(f float64) *Matrix {
	for i := range A.values {
		A.values[i] *= f
	}

	return A
}

// Take every element of the matrix to the power of n (in-place)
func (A *Matrix) Power(n float64) *Matrix {
	for i, elem := range A.values {
		A.values[i] = math.Pow(elem, n)
	}

	return A
}

// Add n to all elements in the matrix (in-place)
func (A *Matrix) AddNum(n float64) *Matrix {
	for i := range A.values {
		A.values[i] += n
	}

	return A
}

func (A *Matrix) isOutOfBounds(row, col int) bool {
	index := (row-1)*A.cols + col - 1

	if index >= len(A.values) {
		return true
	}

	return false
}

func sameSize(A, B *Matrix) bool {
	return A.rows == B.rows && A.cols == B.cols
}

func columnIsRow(A, B *Matrix) bool {
	return A.cols == B.rows
}

// Sigmoid function
func sigmoid(z float64) float64 {
	return 1.0 / (1.0 + math.Pow(math.E, -1.0*z))
}
<commit_msg>removed sigmoid functionality<commit_after>// Package matrix provides functions for simple linear algebra
package matrix

import (
	"bytes"
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"strings"
)

// Describes error during calculations
type MatrixError struct {
	ErrorString string
}

func (err *MatrixError) Error() string { return err.ErrorString }

var (
	ErrIncompatibleSizes = &MatrixError{"Incompatible sizes of matricies"}
	ErrOutOfBounds       = &MatrixError{"The element you are trying to access is out of bounds."}
)

// A function that will apply an abitary transformation to an element in the matrix
type ApplyFunc func(index int, value float64) float64

type Matrix struct {
	rows, cols int
	values     []float64
}

// Creates a new Matrix and initializes all values to 0
func Zeros(rows, cols int) *Matrix {
	A := new(Matrix)

	A.rows = rows
	A.cols = cols
	A.values = make([]float64, rows*cols)

	return A
}

// Creates a new Matrix and initializes all values to 1
func Ones(rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	A.AddNum(1)

	return A
}

// Creates a new identity matrix
func Eye(size int) *Matrix {
	A := Zeros(size, size)

	for i := 1; i <= size; i++ {
		A.Set(i, i, 1)
	}

	return A
}

// Constructs a new matrix with random values in range [0;1)
func Rand(rows, cols int) *Matrix {
	A := Zeros(rows, cols)

	for i := range A.values {
		A.values[i] = rand.Float64()
	}

	return A
}

// Constructs a new Matrix from a Matlab style representation
func FromMatlab(str string) *Matrix {
	rows := strings.Split(str, ";")

	for i, row := range rows {
		rows[i] = strings.Replace(row, ",", " ", -1)
	}

	nRows := len(rows)
	nColumns := len(strings.Fields(rows[0]))

	A := Zeros(nRows, nColumns)

	for i, row := range rows {
		row = strings.Trim(row, "[] ")
		strNums := strings.Fields(row)

		for j, num := range strNums {
			n, _ := strconv.ParseFloat(num, 64)
			A.Set(i+1, j+1, n)
		}
	}

	return A
}

// Return a Matlab representation of the matrix
func (A *Matrix) ToMatlab() string {
	buffer := new(bytes.Buffer)
	buffer.WriteString("[")

	for i, v := range A.values {
		buffer.WriteString(fmt.Sprintf("%f ", v))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("; ")
		}
	}

	buffer.WriteString("]")

	return buffer.String()
}

// Gives the dimensions of the matrix
func (A *Matrix) Dim() (int, int) {
	return A.rows, A.cols
}

// Return the number of rows in the matrix
func (A *Matrix) Rows() int {
	return A.rows
}

// Return the number of columns in the matrix
func (A *Matrix) Columns() int {
	return A.cols
}

// Returns an array of all the values
func (A *Matrix) Values() []float64 {
	tmp := make([]float64, len(A.values))
	copy(tmp, A.values)
	return tmp
}

// Returns an exact copy of the matrix
func (A *Matrix) Copy() *Matrix {
	B := Zeros(A.rows, A.cols)
	copy(B.values, A.values)

	return B
}

func (A *Matrix) Apply(f ApplyFunc) *Matrix {
	for i, v := range A.values {
		A.values[i] = f(i, v)
	}

	return A
}

// Returns a string representation of the matrix
func (A *Matrix) String() string {
	buffer := new(bytes.Buffer)

	for i, elem := range A.values {
		buffer.WriteString(fmt.Sprintf("%.3f ", elem))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("\n")
		}
	}

	return buffer.String()
}

// Retrieve value at [row, col]
func (A *Matrix) Get(row, col int) (float64, error) {

	if A.isOutOfBounds(row, col) {
		return 0, ErrOutOfBounds
	}

	return A.values[(row-1)*A.cols+col-1], nil
}

// Set the element at [row, col] to val
func (A *Matrix) Set(row, col int, val float64) error {

	if A.isOutOfBounds(row, col) {
		return ErrOutOfBounds
	}

	A.values[(row-1)*A.cols+col-1] = val

	return nil
}

// Transpose the matrix (in-place, costly)
func (A *Matrix) Transpose() *Matrix {
	B := Zeros(A.cols, A.rows)

	for i := 1; i <= A.rows; i++ {
		for j := 1; j <= A.cols; j++ {
			v, _ := A.Get(i, j)
			B.Set(j, i, v)
		}
	}

	A.rows, A.cols = A.cols, A.rows
	copy(A.values, B.values)
	return A
}

// Add B to the matrix A (in-place)
func (A *Matrix) Add(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	for i, val := range B.values {
		A.values[i] += val
	}

	return A, nil
}

// Subtract B from the matrix A (in-place)
func (A *Matrix) Sub(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	for i, val := range B.values {
		A.values[i] -= val
	}

	return A, nil
}

// Multiply 2 matricies with each other returning a new matrix
func (A *Matrix) Mul(B *Matrix) (*Matrix, error) {
	if !columnIsRow(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, B.cols)

	for i := 0; i < C.rows; i++ {
		for j := 0; j < C.cols; j++ {
			sum := float64(0)

			for k := 0; k < A.cols; k++ {
				sum += A.values[i*A.cols+k] * B.values[k*B.cols+j]
			}

			C.values[i*C.rows+j] = sum
		}
	}

	return C, nil
}

// Standard scalar product of 2 matricies
func (A *Matrix) Dot(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	for i, v := range B.values {
		A.values[i] *= v
	}
	return A, nil
}

// Scale the matrix in-place by the factor f
func (A *Matrix) Scale(f float64) *Matrix {
	for i := range A.values {
		A.values[i] *= f
	}

	return A
}

// Take every element of the matrix to the power of n (in-place)
func (A *Matrix) Power(n float64) *Matrix {
	for i, elem := range A.values {
		A.values[i] = math.Pow(elem, n)
	}

	return A
}

// Add n to all elements in the matrix (in-place)
func (A *Matrix) AddNum(n float64) *Matrix {
	for i := range A.values {
		A.values[i] += n
	}

	return A
}

func (A *Matrix) isOutOfBounds(row, col int) bool {
	index := (row-1)*A.cols + col - 1

	if index >= len(A.values) {
		return true
	}

	return false
}

func sameSize(A, B *Matrix) bool {
	return A.rows == B.rows && A.cols == B.cols
}

func columnIsRow(A, B *Matrix) bool {
	return A.cols == B.rows
}
<|endoftext|>


<commit_before>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
	"context"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"errors"
	"math/big"
	"time"

	"go.uber.org/zap"

	"knative.dev/pkg/logging"
)

const (
	organization = "knative.dev"
)

// Create the common parts of the cert. These don't change between
// the root/CA cert and the server cert.
func createCertTemplate(name, namespace string, notAfter time.Time) (*x509.Certificate, error) {
	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
	if err != nil {
		return nil, errors.New("failed to generate serial number: " + err.Error())
	}

	serviceName := name + "." + namespace
	commonName := serviceName + ".svc"
	serviceNames := []string{
		name,
		serviceName,
		commonName,
		serviceName + ".svc.cluster.local",
	}

	tmpl := x509.Certificate{
		SerialNumber: serialNumber,
		Subject: pkix.Name{
			Organization: []string{organization},
			CommonName:   commonName,
		},
		SignatureAlgorithm:    x509.SHA256WithRSA,
		NotBefore:             time.Now(),
		NotAfter:              notAfter,
		BasicConstraintsValid: true,
		DNSNames:              serviceNames,
	}
	return &tmpl, nil
}

// Create cert template suitable for CA and hence signing
func createCACertTemplate(name, namespace string, notAfter time.Time) (*x509.Certificate, error) {
	rootCert, err := createCertTemplate(name, namespace, notAfter)
	if err != nil {
		return nil, err
	}
	// Make it into a CA cert and change it so we can use it to sign certs
	rootCert.IsCA = true
	rootCert.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature
	rootCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}
	return rootCert, nil
}

// Create cert template that we can use on the server for TLS
func createServerCertTemplate(name, namespace string, notAfter time.Time) (*x509.Certificate, error) {
	serverCert, err := createCertTemplate(name, namespace, notAfter)
	if err != nil {
		return nil, err
	}
	serverCert.KeyUsage = x509.KeyUsageDigitalSignature
	serverCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
	return serverCert, err
}

// Actually sign the cert and return things in a form that we can use later on
func createCert(template, parent *x509.Certificate, pub, parentPriv interface{}) (
	cert *x509.Certificate, certPEM []byte, err error) {

	certDER, err := x509.CreateCertificate(rand.Reader, template, parent, pub, parentPriv)
	if err != nil {
		return
	}
	cert, err = x509.ParseCertificate(certDER)
	if err != nil {
		return
	}
	b := pem.Block{Type: "CERTIFICATE", Bytes: certDER}
	certPEM = pem.EncodeToMemory(&b)
	return
}

func createCA(ctx context.Context, name, namespace string, notAfter time.Time) (*rsa.PrivateKey, *x509.Certificate, []byte, error) {
	logger := logging.FromContext(ctx)
	rootKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		logger.Errorw("error generating random key", zap.Error(err))
		return nil, nil, nil, err
	}

	rootCertTmpl, err := createCACertTemplate(name, namespace, notAfter)
	if err != nil {
		logger.Errorw("error generating CA cert", zap.Error(err))
		return nil, nil, nil, err
	}

	rootCert, rootCertPEM, err := createCert(rootCertTmpl, rootCertTmpl, &rootKey.PublicKey, rootKey)
	if err != nil {
		logger.Errorw("error signing the CA cert", zap.Error(err))
		return nil, nil, nil, err
	}
	return rootKey, rootCert, rootCertPEM, nil
}

// CreateCerts creates and returns a CA certificate and certificate and
// key for the server. serverKey and serverCert are used by the server
// to establish trust for clients, CA certificate is used by the
// client to verify the server authentication chain. notAfter specifies
// the expiration date.
func CreateCerts(ctx context.Context, name, namespace string, notAfter time.Time) (serverKey, serverCert, caCert []byte, err error) {
	logger := logging.FromContext(ctx)
	// First create a CA certificate and private key
	caKey, caCertificate, caCertificatePEM, err := createCA(ctx, name, namespace, notAfter)
	if err != nil {
		return nil, nil, nil, err
	}

	// Then create the private key for the serving cert
	servKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		logger.Errorw("error generating random key", zap.Error(err))
		return nil, nil, nil, err
	}
	servCertTemplate, err := createServerCertTemplate(name, namespace, notAfter)
	if err != nil {
		logger.Errorw("failed to create the server certificate template", zap.Error(err))
		return nil, nil, nil, err
	}

	// create a certificate which wraps the server's public key, sign it with the CA private key
	_, servCertPEM, err := createCert(servCertTemplate, caCertificate, &servKey.PublicKey, caKey)
	if err != nil {
		logger.Errorw("error signing server certificate template", zap.Error(err))
		return nil, nil, nil, err
	}
	servKeyPEM := pem.EncodeToMemory(&pem.Block{
		Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(servKey),
	})
	return servKeyPEM, servCertPEM, caCertificatePEM, nil
}
<commit_msg>feat: get cluster domain use utility (#1795)<commit_after>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
	"context"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"errors"
	"math/big"
	"time"

	"go.uber.org/zap"

	"knative.dev/pkg/logging"
	"knative.dev/pkg/network"
)

const (
	organization = "knative.dev"
)

// Create the common parts of the cert. These don't change between
// the root/CA cert and the server cert.
func createCertTemplate(name, namespace string, notAfter time.Time) (*x509.Certificate, error) {
	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
	if err != nil {
		return nil, errors.New("failed to generate serial number: " + err.Error())
	}

	serviceName := name + "." + namespace
	commonName := serviceName + ".svc"
	serviceHostname := network.GetServiceHostname(name, namespace)
	serviceNames := []string{
		name,
		serviceName,
		commonName,
		serviceHostname,
	}

	tmpl := x509.Certificate{
		SerialNumber: serialNumber,
		Subject: pkix.Name{
			Organization: []string{organization},
			CommonName:   commonName,
		},
		SignatureAlgorithm:    x509.SHA256WithRSA,
		NotBefore:             time.Now(),
		NotAfter:              notAfter,
		BasicConstraintsValid: true,
		DNSNames:              serviceNames,
	}
	return &tmpl, nil
}

// Create cert template suitable for CA and hence signing
func createCACertTemplate(name, namespace string, notAfter time.Time) (*x509.Certificate, error) {
	rootCert, err := createCertTemplate(name, namespace, notAfter)
	if err != nil {
		return nil, err
	}
	// Make it into a CA cert and change it so we can use it to sign certs
	rootCert.IsCA = true
	rootCert.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature
	rootCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}
	return rootCert, nil
}

// Create cert template that we can use on the server for TLS
func createServerCertTemplate(name, namespace string, notAfter time.Time) (*x509.Certificate, error) {
	serverCert, err := createCertTemplate(name, namespace, notAfter)
	if err != nil {
		return nil, err
	}
	serverCert.KeyUsage = x509.KeyUsageDigitalSignature
	serverCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
	return serverCert, err
}

// Actually sign the cert and return things in a form that we can use later on
func createCert(template, parent *x509.Certificate, pub, parentPriv interface{}) (
	cert *x509.Certificate, certPEM []byte, err error) {

	certDER, err := x509.CreateCertificate(rand.Reader, template, parent, pub, parentPriv)
	if err != nil {
		return
	}
	cert, err = x509.ParseCertificate(certDER)
	if err != nil {
		return
	}
	b := pem.Block{Type: "CERTIFICATE", Bytes: certDER}
	certPEM = pem.EncodeToMemory(&b)
	return
}

func createCA(ctx context.Context, name, namespace string, notAfter time.Time) (*rsa.PrivateKey, *x509.Certificate, []byte, error) {
	logger := logging.FromContext(ctx)
	rootKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		logger.Errorw("error generating random key", zap.Error(err))
		return nil, nil, nil, err
	}

	rootCertTmpl, err := createCACertTemplate(name, namespace, notAfter)
	if err != nil {
		logger.Errorw("error generating CA cert", zap.Error(err))
		return nil, nil, nil, err
	}

	rootCert, rootCertPEM, err := createCert(rootCertTmpl, rootCertTmpl, &rootKey.PublicKey, rootKey)
	if err != nil {
		logger.Errorw("error signing the CA cert", zap.Error(err))
		return nil, nil, nil, err
	}
	return rootKey, rootCert, rootCertPEM, nil
}

// CreateCerts creates and returns a CA certificate and certificate and
// key for the server. serverKey and serverCert are used by the server
// to establish trust for clients, CA certificate is used by the
// client to verify the server authentication chain. notAfter specifies
// the expiration date.
func CreateCerts(ctx context.Context, name, namespace string, notAfter time.Time) (serverKey, serverCert, caCert []byte, err error) {
	logger := logging.FromContext(ctx)
	// First create a CA certificate and private key
	caKey, caCertificate, caCertificatePEM, err := createCA(ctx, name, namespace, notAfter)
	if err != nil {
		return nil, nil, nil, err
	}

	// Then create the private key for the serving cert
	servKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		logger.Errorw("error generating random key", zap.Error(err))
		return nil, nil, nil, err
	}
	servCertTemplate, err := createServerCertTemplate(name, namespace, notAfter)
	if err != nil {
		logger.Errorw("failed to create the server certificate template", zap.Error(err))
		return nil, nil, nil, err
	}

	// create a certificate which wraps the server's public key, sign it with the CA private key
	_, servCertPEM, err := createCert(servCertTemplate, caCertificate, &servKey.PublicKey, caKey)
	if err != nil {
		logger.Errorw("error signing server certificate template", zap.Error(err))
		return nil, nil, nil, err
	}
	servKeyPEM := pem.EncodeToMemory(&pem.Block{
		Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(servKey),
	})
	return servKeyPEM, servCertPEM, caCertificatePEM, nil
}
<|endoftext|>


<commit_before>package api

import (
	"encoding/json"
	"fmt"
	log "github.com/Sirupsen/logrus"
)

type ErrorResponse struct {
	Error string `json:"error"`
}

type VolumeResponse struct {
	UUID string
	Base string
	Size int64
}

type SnapshotResponse struct {
	UUID       string
	VolumeUUID string
}

type BlockStoreResponse struct {
	UUID      string
	Kind      string
	BlockSize int64
}

func ResponseError(format string, a ...interface{}) {
	response := ErrorResponse{Error: fmt.Sprintf(format, a...)}
	j, err := json.MarshalIndent(&response, "", "\t")
	if err != nil {
		panic(fmt.Sprintf("Failed to generate response for error:", err))
	}
	fmt.Println(string(j[:]))
}

func ResponseLogAndError(format string, a ...interface{}) {
	log.Errorf(format, a...)
	ResponseError(format, a...)
}

func ResponseOutput(v interface{}) {
	j, err := json.MarshalIndent(v, "", "\t")
	if err != nil {
		panic(fmt.Sprintf("Failed to generate response for error:", err))
	}
	fmt.Println(string(j[:]))
}
<commit_msg>Remove unnecessary json tag<commit_after>package api

import (
	"encoding/json"
	"fmt"
	log "github.com/Sirupsen/logrus"
)

type ErrorResponse struct {
	Error string
}

type VolumeResponse struct {
	UUID string
	Base string
	Size int64
}

type SnapshotResponse struct {
	UUID       string
	VolumeUUID string
}

type BlockStoreResponse struct {
	UUID      string
	Kind      string
	BlockSize int64
}

func ResponseError(format string, a ...interface{}) {
	response := ErrorResponse{Error: fmt.Sprintf(format, a...)}
	j, err := json.MarshalIndent(&response, "", "\t")
	if err != nil {
		panic(fmt.Sprintf("Failed to generate response for error:", err))
	}
	fmt.Println(string(j[:]))
}

func ResponseLogAndError(format string, a ...interface{}) {
	log.Errorf(format, a...)
	ResponseError(format, a...)
}

func ResponseOutput(v interface{}) {
	j, err := json.MarshalIndent(v, "", "\t")
	if err != nil {
		panic(fmt.Sprintf("Failed to generate response for error:", err))
	}
	fmt.Println(string(j[:]))
}
<|endoftext|>


<commit_before>package apis

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"strings"

	"github.com/ghchinoy/atmotool/cm"
	"github.com/ghchinoy/atmotool/control"
)

const (
	// APIGetInfo is the endpoint to get info about an API
	APIGetInfo = "/api/apis/%s"
	// APIGetInfoIncludeDefault is the endpoint to get info about an API and its default version
	APIGetInfoIncludeDefault = "/api/apis/%s?includeDefaultVersion=true"
	// APIGetInfoIncludeDefaultAndSettings retrieves info about the API, its default version, and settings
	APIGetInfoIncludeDefaultAndSettings = "/api/apis/%s?IncludeDefaultVersion=true&IncludeSettings=true"
	// APIGetVersionInfo is the endpoint pattern for getting information about a version, defaulting endpoint enclusion
	APIGetVersionInfo = "/api/apis/versions/%s?IncludeEndpoints=true"
	// APIVersionImplementations is the endpoint for getting implementation info
	APIVersionImplementations = "/api/apis/versions/implementations"
	// APISettings is the endpoint to retrieve only the API's settings, ref. http://docs.akana.com/cm/api/apis/m_apis_getAPISettings.htm
	APISettings = "/api/apis/%s/settings"
)

// ShowDetailsforAPIID outputs API details
func ShowDetailsforAPIID(apiID string, useVersion bool, config control.Configuration, debug bool) error {
	client, _, err := control.LoginToCM(config, debug)
	if err != nil {
		log.Fatalln(err)
		return err
	}

	var pattern string
	if useVersion {
		pattern = APIGetVersionInfo
	} else {
		pattern = APIGetInfoIncludeDefault
	}

	url := config.URL + fmt.Sprintf(pattern, apiID)
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Add("Accept", "application/json")
	if debug {
		log.Println("Calling", url)
		control.DebugRequestHeader(req)
	}
	resp, err := client.Do(req)
	defer resp.Body.Close()
	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	if debug {
		control.DebugResponseHeader(resp)
	}
	var apiInfo cm.ApisResponse
	err = json.Unmarshal(bodyBytes, &apiInfo)
	if err != nil {
		return err
	}
	if resp.StatusCode == 500 {
		var message string
		if strings.Contains(apiInfo.FaultMessage, "[apiversion]") {
			message = "Please provide an API ID. An API ID was expected; instead, an API Version ID was provided.\nPlease use the --ver flag."
		}
		if strings.Contains(apiInfo.FaultMessage, "[api]") {
			message = "Please provide an API Version ID. An API Version ID was expected; instead, an API ID was provided.\nPlease remove the --ver flag."
		}
		if debug {
			fmt.Printf("%s : %s\n", resp.Status, apiInfo.FaultMessage)
		}
		return errors.New(message)
	}
	if useVersion {
		var api cm.APIVersion
		err = json.Unmarshal(bodyBytes, &api)
		if err != nil {
			return err
		}
		outputAPIVersion(api)
	} else {
		var api cm.APIDetails
		err = json.Unmarshal(bodyBytes, &api)
		if err != nil {
			return err
		}
		outputAPI(api)
	}
	//fmt.Printf("%s: %s\n", resp.Status, bodyBytes)
	return nil
}

func outputAPIVersion(api cm.APIVersion) {

	fmt.Printf("API: %s (%s)\n", api.Description, api.APIID)
	fmt.Printf("Version: %s (%s)\n", api.Name, api.APIVersionID)
	for _, v := range api.Endpoints.Endpoint {
		var visibility string
		for _, t := range v.ConnectionProperties {
			if t.Name == "visibility" {
				visibility = t.Value
			}
		}
		fmt.Printf("%s (%s): %s\n", v.ImplementationCode, visibility, v.URI)
	}
}

func outputAPI(api cm.APIDetails) {

	fmt.Printf("API: %s (%s)", api.Description, api.APIID)
	fmt.Printf("Latest version ID: %s", api.LatestVersionID)
}
<commit_msg>output \n's<commit_after>package apis

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"strings"

	"github.com/ghchinoy/atmotool/cm"
	"github.com/ghchinoy/atmotool/control"
)

const (
	// APIGetInfo is the endpoint to get info about an API
	APIGetInfo = "/api/apis/%s"
	// APIGetInfoIncludeDefault is the endpoint to get info about an API and its default version
	APIGetInfoIncludeDefault = "/api/apis/%s?includeDefaultVersion=true"
	// APIGetInfoIncludeDefaultAndSettings retrieves info about the API, its default version, and settings
	APIGetInfoIncludeDefaultAndSettings = "/api/apis/%s?IncludeDefaultVersion=true&IncludeSettings=true"
	// APIGetVersionInfo is the endpoint pattern for getting information about a version, defaulting endpoint enclusion
	APIGetVersionInfo = "/api/apis/versions/%s?IncludeEndpoints=true"
	// APIVersionImplementations is the endpoint for getting implementation info
	APIVersionImplementations = "/api/apis/versions/implementations"
	// APISettings is the endpoint to retrieve only the API's settings, ref. http://docs.akana.com/cm/api/apis/m_apis_getAPISettings.htm
	APISettings = "/api/apis/%s/settings"
)

// ShowDetailsforAPIID outputs API details
func ShowDetailsforAPIID(apiID string, useVersion bool, config control.Configuration, debug bool) error {
	client, _, err := control.LoginToCM(config, debug)
	if err != nil {
		log.Fatalln(err)
		return err
	}

	var pattern string
	if useVersion {
		pattern = APIGetVersionInfo
	} else {
		pattern = APIGetInfoIncludeDefault
	}

	url := config.URL + fmt.Sprintf(pattern, apiID)
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Add("Accept", "application/json")
	if debug {
		log.Println("Calling", url)
		control.DebugRequestHeader(req)
	}
	resp, err := client.Do(req)
	defer resp.Body.Close()
	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	if debug {
		control.DebugResponseHeader(resp)
	}
	var apiInfo cm.ApisResponse
	err = json.Unmarshal(bodyBytes, &apiInfo)
	if err != nil {
		return err
	}
	if resp.StatusCode == 500 {
		var message string
		if strings.Contains(apiInfo.FaultMessage, "[apiversion]") {
			message = "Please provide an API ID. An API ID was expected; instead, an API Version ID was provided.\nPlease use the --ver flag."
		}
		if strings.Contains(apiInfo.FaultMessage, "[api]") {
			message = "Please provide an API Version ID. An API Version ID was expected; instead, an API ID was provided.\nPlease remove the --ver flag."
		}
		if debug {
			fmt.Printf("%s : %s\n", resp.Status, apiInfo.FaultMessage)
		}
		return errors.New(message)
	}
	if useVersion {
		var api cm.APIVersion
		err = json.Unmarshal(bodyBytes, &api)
		if err != nil {
			return err
		}
		outputAPIVersion(api)
	} else {
		var api cm.APIDetails
		err = json.Unmarshal(bodyBytes, &api)
		if err != nil {
			return err
		}
		outputAPI(api)
	}
	//fmt.Printf("%s: %s\n", resp.Status, bodyBytes)
	return nil
}

func outputAPIVersion(api cm.APIVersion) {

	fmt.Printf("API: %s (%s)\n", api.Description, api.APIID)
	fmt.Printf("Version: %s (%s)\n", api.Name, api.APIVersionID)
	for _, v := range api.Endpoints.Endpoint {
		var visibility string
		for _, t := range v.ConnectionProperties {
			if t.Name == "visibility" {
				visibility = t.Value
			}
		}
		fmt.Printf("%s (%s): %s\n", v.ImplementationCode, visibility, v.URI)
	}
}

func outputAPI(api cm.APIDetails) {

	fmt.Printf("API: %s (%s)\n", api.Description, api.APIID)
	fmt.Printf("Latest version ID: %s\n", api.LatestVersionID)
}
<|endoftext|>


<commit_before>// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/version.go
// Copyright (C) 2015-2017 The Lightning Network Developers

package build

import (
	"bytes"
	"fmt"
	"strings"
)

// Commit stores the current commit hash of this build, this should be set using
// the -ldflags during compilation.
var Commit string

// semanticAlphabet
const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"

// These constants define the application version and follow the semantic
// versioning 2.0.0 spec (http://semver.org/).
const (
	appMajor uint = 0
	appMinor uint = 6
	appPatch uint = 1

	// appPreRelease MUST only contain characters from semanticAlphabet
	// per the semantic versioning spec.
	appPreRelease = "beta"
)

// Version returns the application version as a properly formed string per the
// semantic versioning 2.0.0 spec (http://semver.org/).
func Version() string {
	// Start with the major, minor, and patch versions.
	version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)

	// Append pre-release version if there is one.  The hyphen called for
	// by the semantic versioning spec is automatically appended and should
	// not be contained in the pre-release string.  The pre-release version
	// is not appended if it contains invalid characters.
	preRelease := normalizeVerString(appPreRelease)
	if preRelease != "" {
		version = fmt.Sprintf("%s-%s", version, preRelease)
	}

	// Append commit hash of current build to version.
	version = fmt.Sprintf("%s commit=%s", version, Commit)

	return version
}

// normalizeVerString returns the passed string stripped of all characters which
// are not valid according to the semantic versioning guidelines for pre-release
// version and build metadata strings.  In particular they MUST only contain
// characters in semanticAlphabet.
func normalizeVerString(str string) string {
	var result bytes.Buffer
	for _, r := range str {
		if strings.ContainsRune(semanticAlphabet, r) {
			result.WriteRune(r)
		}
	}
	return result.String()
}
<commit_msg>lnd: bump version to 0.7<commit_after>// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/version.go
// Copyright (C) 2015-2017 The Lightning Network Developers

package build

import (
	"bytes"
	"fmt"
	"strings"
)

// Commit stores the current commit hash of this build, this should be set using
// the -ldflags during compilation.
var Commit string

// semanticAlphabet
const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"

// These constants define the application version and follow the semantic
// versioning 2.0.0 spec (http://semver.org/).
const (
	appMajor uint = 0
	appMinor uint = 7
	appPatch uint = 0

	// appPreRelease MUST only contain characters from semanticAlphabet
	// per the semantic versioning spec.
	appPreRelease = "beta"
)

// Version returns the application version as a properly formed string per the
// semantic versioning 2.0.0 spec (http://semver.org/).
func Version() string {
	// Start with the major, minor, and patch versions.
	version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)

	// Append pre-release version if there is one.  The hyphen called for
	// by the semantic versioning spec is automatically appended and should
	// not be contained in the pre-release string.  The pre-release version
	// is not appended if it contains invalid characters.
	preRelease := normalizeVerString(appPreRelease)
	if preRelease != "" {
		version = fmt.Sprintf("%s-%s", version, preRelease)
	}

	// Append commit hash of current build to version.
	version = fmt.Sprintf("%s commit=%s", version, Commit)

	return version
}

// normalizeVerString returns the passed string stripped of all characters which
// are not valid according to the semantic versioning guidelines for pre-release
// version and build metadata strings.  In particular they MUST only contain
// characters in semanticAlphabet.
func normalizeVerString(str string) string {
	var result bytes.Buffer
	for _, r := range str {
		if strings.ContainsRune(semanticAlphabet, r) {
			result.WriteRune(r)
		}
	}
	return result.String()
}
<|endoftext|>


<commit_before>package hmm

import (
	"code.google.com/p/biogo.matrix"
	"github.com/akualab/gjoa/model"
	"github.com/akualab/gjoa/model/gaussian"
	//"math"
	"testing"
)

// Tests

/*
   DISCUSSION:
   If you look at the sample data and model params. I manufactured the
   data as if it was emitted with the following sequence:

   t:  0   1   2   3   4   5   6   7   8   9   10  11
   q:  s0  s0  s0  s0  s0  s0  s1  s1  s1  s1  s0  s0
   o:  0.1 0.3 1.1 1.2 0.7 0.7 5.5 7.8 10  5.2 1.1 1.3 <=
   data I created given the Gaussians [1,1] and [4,4]

   I got the following gamma:

   γ0: -0.03 -0.03 -0.05 -0.05 -0.04 -0.11 -9.02 -21 -36 -7.8 -0.15 -0.11
   γ1: -3.35 -3.41 -3.01 -2.92 -3.13 -2.24 -0.00 -0  -0  -0   -1.91 -2.21

   As you can see choosing the gamma with highest prob for each state give
   us the hidden sequence of states.

   gamma gives you the most likely state at time t. In this case the result is what we expect.

   Viterbi gives you the P(q | O,  model), that is, it maximizes of over the whole sequence.
*/

// Test ColumnAt() function.
func TestColumnAt(t *testing.T) {

	mat, e := matrix.NewDense([][]float64{
		{10, 11, 12, 13}, {20, 21, 22, 23}, {30, 31, 32, 33}})
	if e != nil {
		t.Fatal(e)
	}

	col := ColumnAt(mat, 1)

	t.Logf("col: \n%+v", col)

	for i, expected := range []float64{11.0, 21.0, 31.0} {
		v := col.At(i, 0)
		if !model.Comparef64(expected, v) {
			t.Errorf("Wrong value. Expected: [%f], Got: [%f]", expected, v)
		}
	}
}

func MakeNewDenseMatrix(t *testing.T, m [][]float64) *matrix.Dense {
	mm, emm := matrix.NewDense(m)
	if emm != nil {
		t.Fatal(emm)
	}
	return mm
}

func MakeHMM(t *testing.T) *HMM {

	// Gaussian 1.
	mean1 := MakeNewDenseMatrix(t, [][]float64{{1}})
	var1 := MakeNewDenseMatrix(t, [][]float64{{1}})
	g1, eg1 := gaussian.NewGaussian(1, mean1, var1, true, true, "g1")
	if eg1 != nil {
		t.Fatal(eg1)
	}
	// Gaussian 2.
	mean2 := MakeNewDenseMatrix(t, [][]float64{{4}})
	var2 := MakeNewDenseMatrix(t, [][]float64{{4}})
	var2, ev2 := matrix.NewDense([][]float64{{4}})
	if ev2 != nil {
		t.Fatal(ev2)
	}
	g2, eg2 := gaussian.NewGaussian(1, mean2, var2, true, true, "g2")
	if eg2 != nil {
		t.Fatal(eg2)
	}

	initialStateProbs := MakeNewDenseMatrix(t, [][]float64{{0.8}, {0.2}})

	transProbs := MakeNewDenseMatrix(t, [][]float64{{0.9, 0.1}, {0.3, 0.7}})

	// These are the models.
	models := []*gaussian.Gaussian{g1, g2}

	// To pass the to an HMM we need to convert []*gaussian.Gaussian[]
	// to []model.Modeler
	// see http://golang.org/doc/faq#convert_slice_of_interface
	m := make([]model.Modeler, len(models))
	for i, v := range models {
		m[i] = v
	}
	hmm, e := NewHMM(transProbs, initialStateProbs, m)
	if e != nil {
		t.Fatal(e)
	}
	return hmm
}

var (
	obs0 = [][]float64{
		{0.1, 0.3, 1.1, 1.2, 0.7, 0.7, 5.5, 7.8, 10.0, 5.2, 1.1, 1.3}}
	alpha0 = []float64{
		-1.54708208451888,
		-2.80709238811418,
		-3.83134003758912,
		-4.86850442594034,
		-5.92973730403429,
		-6.99328952650412,
		-18.1370692144982,
		-36.3195887463382,
		-57.4758051059185,
		-32.2645657649804,
		-25.5978716740632,
		-26.5391830081456}
	alpha1 = []float64{
		-5.12277362619872,
		-6.99404330419337,
		-7.67194890763762,
		-8.58593275227677,
		-9.98735773434079,
		-11.0914094981902,
		-11.0792560557189,
		-14.8528937698143,
		-21.3216544274498,
		-23.4704150851531,
		-26.4904040834703,
		-29.0712307616184}
	beta0 = []float64{
		-24.9258011954291,
		-23.661415171904,
		-22.6397641116887,
		-21.6045197079498,
		-20.549461075003,
		-19.579339900188,
		-17.3294657178329,
		-13.5557050615525,
		-7.07931720879328,
		-2.06607429111337,
		-1.04620524392834,
		0}
	beta1 = []float64{
		-25.9309053603105,
		-24.6182012641994,
		-23.5726285099546,
		-22.4540945910146,
		-20.5873818254665,
		-17.6335597285231,
		-15.3835555701327,
		-11.6097949124971,
		-5.14103425479379,
		-2.99265648819389,
		-1.76872378444132,
		0}
)

func CompareSliceFloat(t *testing.T, expected []float64, actual *matrix.Dense, row int, message string) {
	for i, _ := range expected {
		if !model.Comparef64(expected[i], actual.At(row, i)) {
			t.Errorf("[%s]. Expected: [%f], Got: [%f]",
				message, expected[i], actual.At(i, 0))
		}
	}
}

func TestEvaluationAlpha(t *testing.T) {

	hmm := MakeHMM(t)
	obs, eobs := matrix.NewDense(obs0)
	if eobs != nil {
		t.Fatal(eobs)
	}
	alpha, logProb, err_alpha := hmm.alpha(obs)
	if err_alpha != nil {
		t.Fatal(err_alpha)
	}
	t.Logf("logProb:\n%+v\n", logProb)
	message := "Error in alpha"
	CompareSliceFloat(t, alpha0, alpha, 0, message)
	CompareSliceFloat(t, alpha1, alpha, 1, message)
}

func TestEvaluationBeta(t *testing.T) {

	hmm := MakeHMM(t)
	obs := MakeNewDenseMatrix(t, obs0)
	beta, err_beta := hmm.beta(obs)
	if err_beta != nil {
		t.Fatal(err_beta)
	}
	message := "Error in beta"
	CompareSliceFloat(t, beta0, beta, 0, message)
	CompareSliceFloat(t, beta1, beta, 1, message)
}

func TestEvaluationGamma(t *testing.T) {

	hmm := MakeHMM(t)
	obs := MakeNewDenseMatrix(t, obs0)
	alpha, _, err_alpha := hmm.alpha(obs)
	if err_alpha != nil {
		t.Fatal(err_alpha)
	}
	beta, err_beta := hmm.beta(obs)
	if err_beta != nil {
		t.Fatal(err_beta)
	}
	gamma, err_gamma := hmm.gamma(alpha, beta)
	if err_gamma != nil {
		t.Fatal(err_gamma)
	}
	t.Logf("gamma:\n%+v\n", gamma)
}

/*
func TestEvaluationXi(t *testing.T) {
	obs := MakeNewDenseMatrix(t, obs0)
	xi, err_xi := hmm.xi(obs, alpha, beta)
	if err_xi != nil {
		t.Fatal(err_xi)
	}
	t.Logf("xi:\n%+v\n", xi)
}*/
<commit_msg>Tested Gamma.<commit_after>package hmm

import (
	"code.google.com/p/biogo.matrix"
	"github.com/akualab/gjoa/model"
	"github.com/akualab/gjoa/model/gaussian"
	//"math"
	"testing"
)

// Tests

/*
   DISCUSSION:
   If you look at the sample data and model params. I manufactured the
   data as if it was emitted with the following sequence:

   t:  0   1   2   3   4   5   6   7   8   9   10  11
   q:  s0  s0  s0  s0  s0  s0  s1  s1  s1  s1  s0  s0
   o:  0.1 0.3 1.1 1.2 0.7 0.7 5.5 7.8 10  5.2 1.1 1.3 <=
   data I created given the Gaussians [1,1] and [4,4]

   I got the following gamma:

   γ0: -0.03 -0.03 -0.05 -0.05 -0.04 -0.11 -9.02 -21 -36 -7.8 -0.15 -0.11
   γ1: -3.35 -3.41 -3.01 -2.92 -3.13 -2.24 -0.00 -0  -0  -0   -1.91 -2.21

   As you can see choosing the gamma with highest prob for each state give
   us the hidden sequence of states.

   gamma gives you the most likely state at time t. In this case the result is what we expect.

   Viterbi gives you the P(q | O,  model), that is, it maximizes of over the whole sequence.
*/

// Test ColumnAt() function.
func TestColumnAt(t *testing.T) {

	mat, e := matrix.NewDense([][]float64{
		{10, 11, 12, 13}, {20, 21, 22, 23}, {30, 31, 32, 33}})
	if e != nil {
		t.Fatal(e)
	}

	col := ColumnAt(mat, 1)

	t.Logf("col: \n%+v", col)

	for i, expected := range []float64{11.0, 21.0, 31.0} {
		v := col.At(i, 0)
		if !model.Comparef64(expected, v) {
			t.Errorf("Wrong value. Expected: [%f], Got: [%f]", expected, v)
		}
	}
}

func MakeNewDenseMatrix(t *testing.T, m [][]float64) *matrix.Dense {
	mm, emm := matrix.NewDense(m)
	if emm != nil {
		t.Fatal(emm)
	}
	return mm
}

func MakeHMM(t *testing.T) *HMM {

	// Gaussian 1.
	mean1 := MakeNewDenseMatrix(t, [][]float64{{1}})
	var1 := MakeNewDenseMatrix(t, [][]float64{{1}})
	g1, eg1 := gaussian.NewGaussian(1, mean1, var1, true, true, "g1")
	if eg1 != nil {
		t.Fatal(eg1)
	}
	// Gaussian 2.
	mean2 := MakeNewDenseMatrix(t, [][]float64{{4}})
	var2 := MakeNewDenseMatrix(t, [][]float64{{4}})
	var2, ev2 := matrix.NewDense([][]float64{{4}})
	if ev2 != nil {
		t.Fatal(ev2)
	}
	g2, eg2 := gaussian.NewGaussian(1, mean2, var2, true, true, "g2")
	if eg2 != nil {
		t.Fatal(eg2)
	}

	initialStateProbs := MakeNewDenseMatrix(t, [][]float64{{0.8}, {0.2}})

	transProbs := MakeNewDenseMatrix(t, [][]float64{{0.9, 0.1}, {0.3, 0.7}})

	// These are the models.
	models := []*gaussian.Gaussian{g1, g2}

	// To pass the to an HMM we need to convert []*gaussian.Gaussian[]
	// to []model.Modeler
	// see http://golang.org/doc/faq#convert_slice_of_interface
	m := make([]model.Modeler, len(models))
	for i, v := range models {
		m[i] = v
	}
	hmm, e := NewHMM(transProbs, initialStateProbs, m)
	if e != nil {
		t.Fatal(e)
	}
	return hmm
}

var (
	obs0 = [][]float64{
		{0.1, 0.3, 1.1, 1.2, 0.7, 0.7, 5.5, 7.8, 10.0, 5.2, 1.1, 1.3}}
	alpha0 = []float64{
		-1.54708208451888,
		-2.80709238811418,
		-3.83134003758912,
		-4.86850442594034,
		-5.92973730403429,
		-6.99328952650412,
		-18.1370692144982,
		-36.3195887463382,
		-57.4758051059185,
		-32.2645657649804,
		-25.5978716740632,
		-26.5391830081456}
	alpha1 = []float64{
		-5.12277362619872,
		-6.99404330419337,
		-7.67194890763762,
		-8.58593275227677,
		-9.98735773434079,
		-11.0914094981902,
		-11.0792560557189,
		-14.8528937698143,
		-21.3216544274498,
		-23.4704150851531,
		-26.4904040834703,
		-29.0712307616184}
	beta0 = []float64{
		-24.9258011954291,
		-23.661415171904,
		-22.6397641116887,
		-21.6045197079498,
		-20.549461075003,
		-19.579339900188,
		-17.3294657178329,
		-13.5557050615525,
		-7.07931720879328,
		-2.06607429111337,
		-1.04620524392834,
		0}
	beta1 = []float64{
		-25.9309053603105,
		-24.6182012641994,
		-23.5726285099546,
		-22.4540945910146,
		-20.5873818254665,
		-17.6335597285231,
		-15.3835555701327,
		-11.6097949124971,
		-5.14103425479379,
		-2.99265648819389,
		-1.76872378444132,
		0}
	gamma0 = []float64{
		-0.0101945977044363,
		-0.00581887777458709,
		-0.00841546703429051,
		-0.0103354516465726,
		-0.0165096967936958,
		-0.10994074444856,
		-9.00384625008746,
		-23.4126051256471,
		-38.0924336324682,
		-7.86795137385019,
		-0.181388235747997,
		-0.076494325902054}
	gamma1 = []float64{
		-4.59099030426571,
		-5.14955588614921,
		-4.78188873534866,
		-4.5773386610478,
		-4.11205087756371,
		-2.2622805444697,
		-0.000122943608043396,
		-6.79257761172257e-11,
		0,
		-0.000382891103450269,
		-1.79643918566812,
		-2.60854207937483}
)

func CompareSliceFloat(t *testing.T, expected []float64, actual *matrix.Dense, row int, message string) {
	for i, _ := range expected {
		if !model.Comparef64(expected[i], actual.At(row, i)) {
			t.Errorf("[%s]. Expected: [%f], Got: [%f]",
				message, expected[i], actual.At(i, 0))
		}
	}
}

func CompareFloats(t *testing.T, expected float64, actual float64, message string) {
	if !model.Comparef64(expected, actual) {
		t.Errorf("[%s]. Expected: [%f], Got: [%f]",
			message, expected, actual)
	}
}

func TestEvaluationAlpha(t *testing.T) {

	hmm := MakeHMM(t)
	obs := MakeNewDenseMatrix(t, obs0)
	alpha, logProb, err_alpha := hmm.alpha(obs)
	if err_alpha != nil {
		t.Fatal(err_alpha)
	}
	expectedLogProb := -26.4626886822436
	CompareFloats(t, expectedLogProb, logProb, "Error in logProb")
	message := "Error in alpha"
	CompareSliceFloat(t, alpha0, alpha, 0, message)
	CompareSliceFloat(t, alpha1, alpha, 1, message)
}

func TestEvaluationBeta(t *testing.T) {

	hmm := MakeHMM(t)
	obs := MakeNewDenseMatrix(t, obs0)
	beta, err_beta := hmm.beta(obs)
	if err_beta != nil {
		t.Fatal(err_beta)
	}
	message := "Error in beta"
	CompareSliceFloat(t, beta0, beta, 0, message)
	CompareSliceFloat(t, beta1, beta, 1, message)
}

func TestEvaluationGamma(t *testing.T) {

	hmm := MakeHMM(t)
	obs := MakeNewDenseMatrix(t, obs0)
	alpha, _, err_alpha := hmm.alpha(obs)
	if err_alpha != nil {
		t.Fatal(err_alpha)
	}
	beta, err_beta := hmm.beta(obs)
	if err_beta != nil {
		t.Fatal(err_beta)
	}
	gamma, err_gamma := hmm.gamma(alpha, beta)
	if err_gamma != nil {
		t.Fatal(err_gamma)
	}
	message := "Error in gamma"
	CompareSliceFloat(t, gamma0, gamma, 0, message)
	CompareSliceFloat(t, gamma1, gamma, 1, message)
}

/*
func TestEvaluationXi(t *testing.T) {
	obs := MakeNewDenseMatrix(t, obs0)
	xi, err_xi := hmm.xi(obs, alpha, beta)
	if err_xi != nil {
		t.Fatal(err_xi)
	}
	t.Logf("xi:\n%+v\n", xi)
}*/
<|endoftext|>


<commit_before>package uploads

import (
	"github.com/materials-commons/mcstore/pkg/app"
	dmocks "github.com/materials-commons/mcstore/pkg/db/dai/mocks"
	"github.com/materials-commons/mcstore/pkg/db/schema"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("FinishRequest", func() {
	var (
		files *dmocks.Files
		dirs  *dmocks.Dirs
		f     *finisher
	)

	BeforeEach(func() {
		files = dmocks.NewMFiles()
		dirs = dmocks.NewMDirs()
		f = &finisher{
			files: files,
			dirs:  dirs,
		}
	})

	Describe("parentID method tests", func() {
		It("Should return a parent when there is one", func() {
			parentFile := &schema.File{
				ID: "parent",
			}
			files.On("ByPath", "file.name", "dir").Return(parentFile, nil)
			parentID, err := f.parentID("file.name", "dir")
			Expect(err).To(BeNil())
			Expect(parentID).To(Equal("parent"))
		})

		It("Should not return a parent when there isn't one", func() {
			var nilFile *schema.File = nil
			files.On("ByPath", "file.name", "dir").Return(nilFile, app.ErrNotFound)
			parentID, err := f.parentID("file.name", "dir")
			Expect(err).To(BeNil())
			Expect(parentID).To(Equal(""))
		})

		It("Should return an error when ByPath returns an error other than app.ErrNotFound", func() {
			var nilFile *schema.File = nil
			files.On("ByPath", "file.name", "dir").Return(nilFile, app.ErrInvalid)
			parentID, err := f.parentID("file.name", "dir")
			Expect(err).To(Equal(app.ErrInvalid))
			Expect(parentID).To(Equal(""))
		})
	})

	Describe("fileInDir method tests", func() {
		It("Should return false if the file isn't in the directory", func() {
			var noFiles []schema.File
			dirs.On("Files", "dir").Return(noFiles, app.ErrNotFound)
			Expect(f.fileInDir("checksum", "file.name", "dir")).To(BeFalse())
		})

		It("Should return false if there is a matching file with a different checksum", func() {
			matchingFile := schema.File{
				Name:     "file.name",
				Checksum: "wrongchecksum",
			}
			var matching []schema.File = []schema.File{matchingFile}
			dirs.On("Files", "dir").Return(matching, nil)
			Expect(f.fileInDir("abc123", "file.name", "dir")).To(BeFalse())
		})

		It("Should return true if the file with exact checksum is in the directory", func() {
			matchingFile := schema.File{
				Name:     "file.name",
				Checksum: "abc123",
			}
			var matching []schema.File = []schema.File{matchingFile}
			dirs.On("Files", "dir").Return(matching, nil)
			Expect(f.fileInDir("abc123", "file.name", "dir")).To(BeTrue())
		})
	})

	Describe("finish method tests", func() {
	})
})
<commit_msg>Add more mock services.<commit_after>package uploads

import (
	"github.com/materials-commons/gohandy/file"
	"github.com/materials-commons/mcstore/pkg/app"
	dmocks "github.com/materials-commons/mcstore/pkg/db/dai/mocks"
	"github.com/materials-commons/mcstore/pkg/db/schema"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("FinishRequest", func() {
	var (
		files  *dmocks.Files
		dirs   *dmocks.Dirs
		files2 *dmocks.Files2
		f      *finisher
	)

	BeforeEach(func() {
		files = dmocks.NewMFiles()
		dirs = dmocks.NewMDirs()
		files2 = dmocks.NewMFiles2()
		f = &finisher{
			files: files,
			dirs:  dirs,
			fops:  file.MockOps,
		}
	})

	Describe("parentID method tests", func() {
		It("Should return a parent when there is one", func() {
			parentFile := &schema.File{
				ID: "parent",
			}
			files.On("ByPath", "file.name", "dir").Return(parentFile, nil)
			parentID, err := f.parentID("file.name", "dir")
			Expect(err).To(BeNil())
			Expect(parentID).To(Equal("parent"))
		})

		It("Should not return a parent when there isn't one", func() {
			var nilFile *schema.File = nil
			files.On("ByPath", "file.name", "dir").Return(nilFile, app.ErrNotFound)
			parentID, err := f.parentID("file.name", "dir")
			Expect(err).To(BeNil())
			Expect(parentID).To(Equal(""))
		})

		It("Should return an error when ByPath returns an error other than app.ErrNotFound", func() {
			var nilFile *schema.File = nil
			files.On("ByPath", "file.name", "dir").Return(nilFile, app.ErrInvalid)
			parentID, err := f.parentID("file.name", "dir")
			Expect(err).To(Equal(app.ErrInvalid))
			Expect(parentID).To(Equal(""))
		})
	})

	Describe("fileInDir method tests", func() {
		It("Should return false if the file isn't in the directory", func() {
			var noFiles []schema.File
			dirs.On("Files", "dir").Return(noFiles, app.ErrNotFound)
			Expect(f.fileInDir("checksum", "file.name", "dir")).To(BeFalse())
		})

		It("Should return false if there is a matching file with a different checksum", func() {
			matchingFile := schema.File{
				Name:     "file.name",
				Checksum: "wrongchecksum",
			}
			var matching []schema.File = []schema.File{matchingFile}
			dirs.On("Files", "dir").Return(matching, nil)
			Expect(f.fileInDir("abc123", "file.name", "dir")).To(BeFalse())
		})

		It("Should return true if the file with exact checksum is in the directory", func() {
			matchingFile := schema.File{
				Name:     "file.name",
				Checksum: "abc123",
			}
			var matching []schema.File = []schema.File{matchingFile}
			dirs.On("Files", "dir").Return(matching, nil)
			Expect(f.fileInDir("abc123", "file.name", "dir")).To(BeTrue())
		})
	})

	Describe("finish method tests", func() {

		It("Should fail if the size is wrong.", func() {

		})
	})
})
<|endoftext|>


<commit_before>package upload

import (
	"github.com/emicklei/go-restful"
	"github.com/inconshreveable/log15"
	"github.com/materials-commons/mcstore/pkg/app"
	"github.com/materials-commons/mcstore/pkg/app/flow"
	"github.com/materials-commons/mcstore/pkg/db/schema"
	"github.com/materials-commons/mcstore/pkg/ws/rest"
	"github.com/materials-commons/mcstore/server/mcstored/service/uploads"
)

// An uploadResource handles all upload requests.
type uploadResource struct {
	uploader      *uploader
	log           log15.Logger
	factory       AssemblerFactory
	createService uploads.CreateService
}

// NewResources creates a new upload resource
func NewResource(uploader *uploader, factory AssemblerFactory) rest.Service {
	return &uploadResource{
		uploader:      uploader,
		log:           app.NewLog("resource", "upload"),
		factory:       factory,
		createService: uploads.NewCreateService(),
	}
}

// WebService creates an instance of the upload web service.
func (r *uploadResource) WebService() *restful.WebService {
	ws := new(restful.WebService)

	ws.Path("/upload").Produces(restful.MIME_JSON)
	ws.Route(ws.POST("").To(rest.RouteHandler(r.createUploadRequest)).
		Doc("Creates a new upload request").
		Reads(uploadCreateRequest{}).
		Writes(uploadCreateResponse{}))
	ws.Route(ws.POST("/chunk").To(rest.RouteHandler1(r.uploadFileChunk)).
		Consumes("multipart/form-data").
		Doc("Upload a file chunk"))

	return ws
}

// uploadFileChunk uploads a new file chunk.
func (r *uploadResource) uploadFileChunk(request *restful.Request, response *restful.Response, user schema.User) error {
	flowRequest, err := form2FlowRequest(request)
	if err != nil {
		r.log.Error(app.Logf("Error converting form to flow.Request: %s", err))
		return err
	}

	if err := r.uploader.processRequest(flowRequest); err != nil {
		return err
	}

	if r.uploader.allBlocksUploaded(flowRequest) {
		go r.assembler(flowRequest)
	}

	return nil
}

// assembler builds a new Assembler to assemble the pieces of the file.
func (r *uploadResource) assembler(request *flow.Request) {
	if assembler := r.factory.Assembler(request); assembler != nil {
		assembler.Assemble()
	}
}
<commit_msg>Change service to also consume JSON. Make the createService a parameter rather than creating it.<commit_after>package upload

import (
	"github.com/emicklei/go-restful"
	"github.com/inconshreveable/log15"
	"github.com/materials-commons/mcstore/pkg/app"
	"github.com/materials-commons/mcstore/pkg/app/flow"
	"github.com/materials-commons/mcstore/pkg/db/schema"
	"github.com/materials-commons/mcstore/pkg/ws/rest"
	"github.com/materials-commons/mcstore/server/mcstored/service/uploads"
)

// An uploadResource handles all upload requests.
type uploadResource struct {
	uploader      *uploader
	log           log15.Logger
	factory       AssemblerFactory
	createService uploads.CreateService
}

// NewResources creates a new upload resource
func NewResource(uploader *uploader, factory AssemblerFactory, createService uploads.CreateService) rest.Service {
	return &uploadResource{
		uploader:      uploader,
		log:           app.NewLog("resource", "upload"),
		factory:       factory,
		createService: createService,
	}
}

// WebService creates an instance of the upload web service.
func (r *uploadResource) WebService() *restful.WebService {
	ws := new(restful.WebService)

	ws.Path("/upload").Produces(restful.MIME_JSON).Consumes(restful.MIME_JSON)
	ws.Route(ws.POST("").To(rest.RouteHandler(r.createUploadRequest)).
		Doc("Creates a new upload request").
		Reads(uploadCreateRequest{}).
		Writes(uploadCreateResponse{}))
	ws.Route(ws.POST("/chunk").To(rest.RouteHandler1(r.uploadFileChunk)).
		Consumes("multipart/form-data").
		Doc("Upload a file chunk"))

	return ws
}

// uploadFileChunk uploads a new file chunk.
func (r *uploadResource) uploadFileChunk(request *restful.Request, response *restful.Response, user schema.User) error {
	flowRequest, err := form2FlowRequest(request)
	if err != nil {
		r.log.Error(app.Logf("Error converting form to flow.Request: %s", err))
		return err
	}

	if err := r.uploader.processRequest(flowRequest); err != nil {
		return err
	}

	if r.uploader.allBlocksUploaded(flowRequest) {
		go r.assembler(flowRequest)
	}

	return nil
}

// assembler builds a new Assembler to assemble the pieces of the file.
func (r *uploadResource) assembler(request *flow.Request) {
	if assembler := r.factory.Assembler(request); assembler != nil {
		assembler.Assemble()
	}
}
<|endoftext|>


<commit_before>package tai64

import (
	"fmt"
	"syscall"
	"time"
)

type Tai struct {
	x uint64
}

type Taia struct {
	sec  Tai
	nano uint64
	atto uint64
}

const TAICONST = 4611686018427387914
const Tai_Count = 8
const Taia_Count = 16

func Tai_now() Tai {
	var result Tai
	result.x = TAICONST + uint64(time.Now().Unix())
	return result
}

func Taia_now() Taia {
	var result Taia
	now := new(syscall.Timeval)
	err := syscall.Gettimeofday(now)
	if err == nil {
		var t Tai
		t.x = TAICONST + uint64(now.Sec)
		result.sec = t
		result.nano = uint64(1000*uint64(now.Usec) + 500)
		result.atto = 0
	} else {
		fmt.Println(err)
	}
	return result
}

func Tai_pack(t Tai) []byte {
	result := make([]byte, Tai_Count)
	x := t.x
	result[7] = byte(x & 255)
	x >>= 8
	result[6] = byte(x & 255)
	x >>= 8
	result[5] = byte(x & 255)
	x >>= 8
	result[4] = byte(x & 255)
	x >>= 8
	result[3] = byte(x & 255)
	x >>= 8
	result[2] = byte(x & 255)
	x >>= 8
	result[1] = byte(x & 255)
	x >>= 8
	result[0] = byte(x)
	return result

}

func Tai_unpack(s []byte) Tai {
	var result Tai
	var x uint64
	x = uint64(s[0])
	x <<= 8
	x += uint64(s[1])
	x <<= 8
	x += uint64(s[2])
	x <<= 8
	x += uint64(s[3])
	x <<= 8
	x += uint64(s[4])
	x <<= 8
	x += uint64(s[5])
	x <<= 8
	x += uint64(s[6])
	x <<= 8
	x += uint64(s[7])
	result.x = x
	return result
}

func Taia_pack(t Taia) []byte {
	result := make([]byte, Taia_Count)
	zz := make([]byte, Tai_Count)
	zz = Tai_pack(t.sec)
	for i := 0; i < Tai_Count; i++ {
		result[i+Tai_Count] = zz[i]
	}
	x := t.atto
	result[7] = byte(x & 255)
	x >>= 8
	result[6] = byte(x & 255)
	x >>= 8
	result[5] = byte(x & 255)
	x >>= 8
	result[4] = byte(x)

	x = t.nano
	result[3] = byte(x & 255)
	x >>= 8
	result[2] = byte(x & 255)
	x >>= 8
	result[1] = byte(x & 255)
	x >>= 8
	result[0] = byte(x)

	return result
}

func Taia_unpack(s []byte) Taia {
	var result Taia
	var zz Tai
	zz = Tai_unpack(s[8:])
	result.sec = zz
	x := uint64(s[4])
	x <<= 8
	x += uint64(s[5])
	x <<= 8
	x += uint64(s[6])
	x <<= 8
	x += uint64(s[7])
	result.atto = x
	x = uint64(s[0])
	x <<= 8
	x += uint64(s[1])
	x <<= 8
	x += uint64(s[2])
	x <<= 8
	x += uint64(s[3])
	result.nano = x
	return result

}
<commit_msg>gtclock: partially obeyed golint<commit_after>package tai64

import (
	"fmt"
	"syscall"
	"time"
)

type Tai struct {
	x uint64
}

type Taia struct {
	sec  Tai
	nano uint64
	atto uint64
}

const TAICONST = 4611686018427387914
const TaiCount = 8
const TaiaCount = 16

func TaiNow() Tai {
	var result Tai
	result.x = TAICONST + uint64(time.Now().Unix())
	return result
}

func TaiaNow() Taia {
	var result Taia
	now := new(syscall.Timeval)
	err := syscall.Gettimeofday(now)
	if err == nil {
		var t Tai
		t.x = TAICONST + uint64(now.Sec)
		result.sec = t
		result.nano = uint64(1000*uint64(now.Usec) + 500)
		result.atto = 0
	} else {
		fmt.Println(err)
	}
	return result
}

func TaiPack(t Tai) []byte {
	result := make([]byte, TaiCount)
	x := t.x
	result[7] = byte(x & 255)
	x >>= 8
	result[6] = byte(x & 255)
	x >>= 8
	result[5] = byte(x & 255)
	x >>= 8
	result[4] = byte(x & 255)
	x >>= 8
	result[3] = byte(x & 255)
	x >>= 8
	result[2] = byte(x & 255)
	x >>= 8
	result[1] = byte(x & 255)
	x >>= 8
	result[0] = byte(x)
	return result

}

func TaiUnpack(s []byte) Tai {
	var result Tai
	var x uint64
	x = uint64(s[0])
	x <<= 8
	x += uint64(s[1])
	x <<= 8
	x += uint64(s[2])
	x <<= 8
	x += uint64(s[3])
	x <<= 8
	x += uint64(s[4])
	x <<= 8
	x += uint64(s[5])
	x <<= 8
	x += uint64(s[6])
	x <<= 8
	x += uint64(s[7])
	result.x = x
	return result
}

func TaiaPack(t Taia) []byte {
	result := make([]byte, TaiaCount)
	zz := make([]byte, TaiCount)
	zz = TaiPack(t.sec)
	for i := 0; i < TaiCount; i++ {
		result[i+TaiCount] = zz[i]
	}
	x := t.atto
	result[7] = byte(x & 255)
	x >>= 8
	result[6] = byte(x & 255)
	x >>= 8
	result[5] = byte(x & 255)
	x >>= 8
	result[4] = byte(x)

	x = t.nano
	result[3] = byte(x & 255)
	x >>= 8
	result[2] = byte(x & 255)
	x >>= 8
	result[1] = byte(x & 255)
	x >>= 8
	result[0] = byte(x)

	return result
}

func TaiaUnpack(s []byte) Taia {
	var result Taia
	var zz Tai
	zz = TaiUnpack(s[8:])
	result.sec = zz
	x := uint64(s[4])
	x <<= 8
	x += uint64(s[5])
	x <<= 8
	x += uint64(s[6])
	x <<= 8
	x += uint64(s[7])
	result.atto = x
	x = uint64(s[0])
	x <<= 8
	x += uint64(s[1])
	x <<= 8
	x += uint64(s[2])
	x <<= 8
	x += uint64(s[3])
	result.nano = x
	return result

}
<|endoftext|>