<commit_before>package main_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"

	"github.com/cloudfoundry-incubator/cf-test-helpers/generator"

	"time"

	"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
)

var _ = Describe("Pushing an app", func() {
	It("stops the time for pushing an app", func() {
		startTime := time.Now()

		appName := generator.PrefixedRandomName("APP")
		Expect(
			cf.Cf("push", appName, "-p", "assets/dora", "-d", config.AppsDomain).Wait(cfPushTimeout)).
			To(Exit(0))

		statsdClient.Timing("cf-push", time.Since(startTime).Seconds()*1000)
	})
})
<commit_msg>Don't specify config.AppsDomain<commit_after>package main_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"

	"github.com/cloudfoundry-incubator/cf-test-helpers/generator"

	"time"

	"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
)

var _ = Describe("Pushing an app", func() {
	It("stops the time for pushing an app", func() {
		startTime := time.Now()

		appName := generator.PrefixedRandomName("APP")
		Expect(
			cf.Cf("push", appName, "-p", "assets/dora").Wait(cfPushTimeout)).
			To(Exit(0))

		statsdClient.Timing("cf-push", time.Since(startTime).Seconds()*1000)
	})
})
<|endoftext|>


<commit_before>//  Copyright (c) 2014 Couchbase, Inc.
//  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 cbft

import (
	"encoding/json"
	"fmt"
	"strconv"

	"github.com/couchbase/go-couchbase"
	"github.com/couchbase/gomemcached"
)

func init() {
	if gomemcached.MaxBodyLen < int(3e7) { // 30,000,000.
		gomemcached.MaxBodyLen = int(3e7)
	}
}

func ParsePartitionsToVBucketIds(dests map[string]Dest) ([]uint16, error) {
	vbuckets := make([]uint16, 0, len(dests))
	for partition, _ := range dests {
		if partition != "" {
			vbId, err := strconv.Atoi(partition)
			if err != nil {
				return nil, fmt.Errorf("feed_cb:"+
					" could not parse partition: %s, err: %v", partition, err)
			}
			vbuckets = append(vbuckets, uint16(vbId))
		}
	}
	return vbuckets, nil
}

func VBucketIdToPartitionDest(pf DestPartitionFunc,
	dests map[string]Dest, vbucketId uint16, key []byte) (
	partition string, dest Dest, err error) {
	if vbucketId < uint16(len(vbucketIdStrings)) {
		partition = vbucketIdStrings[vbucketId]
	}
	if partition == "" {
		partition = fmt.Sprintf("%d", vbucketId)
	}
	dest, err = pf(partition, key, dests)
	if err != nil {
		return "", nil, fmt.Errorf("feed_cb: VBucketIdToPartitionDest,"+
			" partition func, vbucketId: %d, err: %v", vbucketId, err)
	}
	return partition, dest, err
}

var vbucketIdStrings []string

func init() {
	vbucketIdStrings = make([]string, 1024)
	for i := 0; i < len(vbucketIdStrings); i++ {
		vbucketIdStrings[i] = fmt.Sprintf("%d", i)
	}
}

// ----------------------------------------------------------------

type CBFeedParams struct {
	AuthUser     string `json:"authUser"` // May be "" for no auth.
	AuthPassword string `json:"authPassword"`
}

func CouchbasePartitions(sourceType, sourceName, sourceUUID, sourceParams,
	server string) (partitions []string, err error) {
	poolName := "default" // TODO: Parameterize poolName.
	bucketName := sourceName

	params := CBFeedParams{}
	if sourceParams != "" {
		err := json.Unmarshal([]byte(sourceParams), &params)
		if err != nil {
			return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
				" failed sourceParams JSON to CBFeedParams,"+
				" server: %s, poolName: %s, bucketName: %s,"+
				" sourceType: %s, sourceParams: %q, err: %v",
				server, poolName, bucketName, sourceType, sourceParams, err)
		}
	}

	var client couchbase.Client

	if params.AuthUser != "" || bucketName != "default" {
		client, err = couchbase.ConnectWithAuthCreds(server,
			params.AuthUser, params.AuthPassword)
	} else {
		client, err = couchbase.Connect(server)
	}
	if err != nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" connection failed, server: %s, poolName: %s,"+
			" bucketName: %s, sourceType: %s, sourceParams: %q, err: %v",
			server, poolName, bucketName, sourceType, sourceParams, err)
	}

	pool, err := client.GetPool(poolName)
	if err != nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" failed GetPool, server: %s, poolName: %s,"+
			" bucketName: %s, sourceType: %s, sourceParams: %q, err: %v",
			server, poolName, bucketName, sourceType, sourceParams, err)
	}

	bucket, err := pool.GetBucket(bucketName)
	if err != nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" failed GetBucket, server: %s, poolName: %s, bucketName: %s, err: %v",
			server, poolName, bucketName, err)
	}
	defer bucket.Close()

	vbm := bucket.VBServerMap()
	if vbm == nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" no VBServerMap, server: %s, poolName: %s, bucketName: %s, err: %v",
			server, poolName, bucketName, err)
	}

	// NOTE: We assume that vbucket numbers are continuous
	// integers starting from 0.
	numVBuckets := len(vbm.VBucketMap)
	rv := make([]string, numVBuckets)
	for i := 0; i < numVBuckets; i++ {
		rv[i] = strconv.Itoa(i)
	}
	return rv, nil
}
<commit_msg>better error message for wrong couchbase server or auth cases<commit_after>//  Copyright (c) 2014 Couchbase, Inc.
//  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 cbft

import (
	"encoding/json"
	"fmt"
	"strconv"

	"github.com/couchbase/go-couchbase"
	"github.com/couchbase/gomemcached"
)

func init() {
	if gomemcached.MaxBodyLen < int(3e7) { // 30,000,000.
		gomemcached.MaxBodyLen = int(3e7)
	}
}

func ParsePartitionsToVBucketIds(dests map[string]Dest) ([]uint16, error) {
	vbuckets := make([]uint16, 0, len(dests))
	for partition, _ := range dests {
		if partition != "" {
			vbId, err := strconv.Atoi(partition)
			if err != nil {
				return nil, fmt.Errorf("feed_cb:"+
					" could not parse partition: %s, err: %v", partition, err)
			}
			vbuckets = append(vbuckets, uint16(vbId))
		}
	}
	return vbuckets, nil
}

func VBucketIdToPartitionDest(pf DestPartitionFunc,
	dests map[string]Dest, vbucketId uint16, key []byte) (
	partition string, dest Dest, err error) {
	if vbucketId < uint16(len(vbucketIdStrings)) {
		partition = vbucketIdStrings[vbucketId]
	}
	if partition == "" {
		partition = fmt.Sprintf("%d", vbucketId)
	}
	dest, err = pf(partition, key, dests)
	if err != nil {
		return "", nil, fmt.Errorf("feed_cb: VBucketIdToPartitionDest,"+
			" partition func, vbucketId: %d, err: %v", vbucketId, err)
	}
	return partition, dest, err
}

var vbucketIdStrings []string

func init() {
	vbucketIdStrings = make([]string, 1024)
	for i := 0; i < len(vbucketIdStrings); i++ {
		vbucketIdStrings[i] = fmt.Sprintf("%d", i)
	}
}

// ----------------------------------------------------------------

type CBFeedParams struct {
	AuthUser     string `json:"authUser"` // May be "" for no auth.
	AuthPassword string `json:"authPassword"`
}

func CouchbasePartitions(sourceType, sourceName, sourceUUID, sourceParams,
	server string) (partitions []string, err error) {
	poolName := "default" // TODO: Parameterize poolName.
	bucketName := sourceName

	params := CBFeedParams{}
	if sourceParams != "" {
		err := json.Unmarshal([]byte(sourceParams), &params)
		if err != nil {
			return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
				" failed sourceParams JSON to CBFeedParams,"+
				" server: %s, poolName: %s, bucketName: %s,"+
				" sourceType: %s, sourceParams: %q, err: %v",
				server, poolName, bucketName, sourceType, sourceParams, err)
		}
	}

	var client couchbase.Client

	if params.AuthUser != "" || bucketName != "default" {
		client, err = couchbase.ConnectWithAuthCreds(server,
			params.AuthUser, params.AuthPassword)
	} else {
		client, err = couchbase.Connect(server)
	}
	if err != nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" connection failed, server: %s, poolName: %s,"+
			" bucketName: %s, sourceType: %s, sourceParams: %q, err: %v,"+
			" please check that your authUser and authPassword are correct"+
			" and that your couchbase server (%q) is available",
			server, poolName, bucketName, sourceType, sourceParams, err, server)
	}

	pool, err := client.GetPool(poolName)
	if err != nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" failed GetPool, server: %s, poolName: %s,"+
			" bucketName: %s, sourceType: %s, sourceParams: %q, err: %v",
			server, poolName, bucketName, sourceType, sourceParams, err)
	}

	bucket, err := pool.GetBucket(bucketName)
	if err != nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" failed GetBucket, server: %s, poolName: %s,"+
			" bucketName: %s, err: %v, please check that your"+
			" bucketName/sourceName (%q) is correct",
			server, poolName, bucketName, err, bucketName)
	}
	defer bucket.Close()

	vbm := bucket.VBServerMap()
	if vbm == nil {
		return nil, fmt.Errorf("feed_cb: DataSourcePartitions/couchbase"+
			" no VBServerMap, server: %s, poolName: %s, bucketName: %s, err: %v",
			server, poolName, bucketName, err)
	}

	// NOTE: We assume that vbucket numbers are continuous
	// integers starting from 0.
	numVBuckets := len(vbm.VBucketMap)
	rv := make([]string, numVBuckets)
	for i := 0; i < numVBuckets; i++ {
		rv[i] = strconv.Itoa(i)
	}
	return rv, nil
}
<|endoftext|>


<commit_before>package main

import("math")

func adsr1 (signal []int32) []int32{
	const riseSamples, decaySamples, releaseSamples = 20, 40, 10
	const sustainLvl = .8
	var baseNeed = int(riseSamples + decaySamples + releaseSamples)

	//Constant rise 0->1
	if baseNeed > len(signal){ //Shrink ranges
		return signal
	}

	var stoppingPoint = riseSamples
	var curve = 0.0

	for i=0; i<stoppingPoint; i++{ //Rise (lin)
		curve = float32(i)/float32(riseSamples)
		signal[i] = signal[i] * int32(curve)
	}
	stoppingPoint = riseSamples + stoppingPointdecaySamples
	var lambda = float32(decaySamples)/Math.Ln(sustainLvl)
	for i=riseSamples; i<stoppingPoint; i++{ //Decay (exp)
		curve = Math.Exp(float32(i-riseSamples+1)/lambda)
		signal[i] = signal[i] * int32(curve)
	}
	stoppingPoint = len(signal)-releaseSamples
	for i=riseSamples+decaySamples; i<stoppingPoint; i++{ //Sustain (const)
		signal[i] = signal[i] * sustainLvl
	}
	stoppingPoint = len(signal)
	lambda = float32(releaseSamples)/Math.Ln(sustainLvl)
	for i=len(singal)-releaseSamples; i<stoppingPoint; i++{ //Release (exp)
		curve = sustainLvl*Math.Exp(float32(i-len(signal)-releaseSamples+1)/lambda)
		signal[i] = signal[i] * int32(curve)
	}

	return signal
}<commit_msg>Fix filter.go<commit_after>package main

import("math")

func adsr1 (signal []int32) []int32{
	const riseSamples, decaySamples, releaseSamples = 20, 40, 10
	const sustainLvl = .8
	var baseNeed = int(riseSamples + decaySamples + releaseSamples)

	//Constant rise 0->1
	if baseNeed > len(signal){ //Shrink ranges
		return signal
	}

	var stoppingPoint = riseSamples
	var curve = 0.0

	for i:=0; i<stoppingPoint; i++{ //Rise (lin)
		curve = float64(i)/float64(riseSamples)
		signal[i] = signal[i] * int32(curve)
	}
	stoppingPoint = riseSamples + decaySamples
	var lambda = float64(decaySamples)/math.Log(sustainLvl)
	for i:=riseSamples; i<stoppingPoint; i++{ //Decay (exp)
		curve = math.Exp(float64(i-riseSamples+1)/lambda)
		signal[i] = signal[i] * int32(curve)
	}
	stoppingPoint = len(signal)-releaseSamples
	for i:=riseSamples+decaySamples; i<stoppingPoint; i++{ //Sustain (const)
		signal[i] = int32(float64(signal[i]) * sustainLvl)
	}
	stoppingPoint = len(signal)
	lambda = float64(releaseSamples)/math.Log(sustainLvl)
	for i:=len(signal)-releaseSamples; i<stoppingPoint; i++{ //Release (exp)
		curve = sustainLvl*math.Exp(float64(i-len(signal)-releaseSamples+1)/lambda)
		signal[i] = signal[i] * int32(curve)
	}

	return signal
}
<|endoftext|>


<commit_before>package auth

import (
	"net/http"

	"github.com/qor/admin"
	"github.com/qor/qor"
	"github.com/qor/qor-example/models/users"
	"github.com/qor/roles"
)

func init() {
	roles.Register("admin", func(req *http.Request, currentUser interface{}) bool {
		return currentUser != nil && currentUser.(*users.User).Role == "Admin"
	})
}

type AdminAuth struct {
}

func (AdminAuth) LoginURL(c *admin.Context) string {
	return "/auth/login"
}

func (AdminAuth) LogoutURL(c *admin.Context) string {
	return "/auth/logout"
}

func (AdminAuth) GetCurrentUser(c *admin.Context) qor.CurrentUser {
	currentUser, _ := Auth.GetCurrentUser(c.Request).(qor.CurrentUser)
	return currentUser
}
<commit_msg>Update admin auth example<commit_after>package auth

import (
	"fmt"
	"net/http"

	"github.com/qor/admin"
	"github.com/qor/qor"
	"github.com/qor/qor-example/models/users"
	"github.com/qor/roles"
)

func init() {
	roles.Register("admin", func(req *http.Request, currentUser interface{}) bool {
		return currentUser != nil && currentUser.(*users.User).Role == "Admin"
	})
}

type AdminAuth struct {
}

func (AdminAuth) LoginURL(c *admin.Context) string {
	return "/auth/login"
}

func (AdminAuth) LogoutURL(c *admin.Context) string {
	return "/auth/logout"
}

func (AdminAuth) GetCurrentUser(c *admin.Context) qor.CurrentUser {
	currentUser := Auth.GetCurrentUser(c.Request)
	if currentUser != nil {
		qorCurrentUser, ok := currentUser.(qor.CurrentUser)
		if !ok {
			fmt.Printf("User %#v haven't implement qor.CurrentUser interface\n", currentUser)
		}
		return qorCurrentUser
	}
	return nil
}
<|endoftext|>


<commit_before>/* gobenchdb is a command line tool for running and storing go benchmark data in a
   database. It runs the go test -bench command in the current working directory
   and parses the output using the parse package. The parsed data is then written to
   sql database of your choice.

   Usage:
       gobenchdb [options...]

       Options:
             -conn        postgres database connection string
             -table       postgres table name
             -test.bench  run only those benchmarks matching the regular expression`
*/
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/yhat/gobenchdb/benchdb"
)

var (
	conn      = flag.String("conn", "", "postgres database connection string")
	table     = flag.String("table", "", "postgres table name")
	testBench = flag.String("test.bench", ".",
		"run only those benchmarks matching the regular expression")
)

const Postgres = "postgres"

var nsha = 7

var usage = `Usage: gobenchdb [options...]

Options:
  -conn        postgres database connection string
  -table       postgres table name
  -test.bench  run only those benchmarks matching the regular expression`

func main() {
	// Parse command line args
	flag.Parse()
	c := *conn
	t := *table
	tregex := *testBench

	if c == "" {
		UsageExit("database conn must be specified")

	}
	if t == "" {
		UsageExit("database table must be specified")
	}

	// Initalize a BenchPSQL to and run benchmarks.
	err := (&benchdb.BenchPSQL{
		Config: &benchdb.BenchDBConfig{
			Regex:  tregex,
			ShaLen: nsha,
		},
		Driver:    Postgres,
		ConnStr:   c,
		TableName: t,
	}).Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}
}

func UsageExit(message string) {
	if message != "" {
		fmt.Fprintf(os.Stderr, message)
		fmt.Fprintf(os.Stderr, "\n")
	}
	fmt.Fprintf(os.Stderr, usage)
	fmt.Fprintf(os.Stderr, "\n")
	os.Exit(1)
}
<commit_msg>adding package docs<commit_after>/* gobenchdb is a command line tool for running and storing go benchmark data in a
database. It runs the go test -bench command in the current working directory
and parses the output using the parse package. The parsed data is then written to
sql database of your choice.

   Usage:
       gobenchdb [options...]

       Options:
             -conn        postgres database connection string
             -table       postgres table name
             -test.bench  run only those benchmarks matching the regular expression`
*/
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/yhat/gobenchdb/benchdb"
)

var (
	conn      = flag.String("conn", "", "postgres database connection string")
	table     = flag.String("table", "", "postgres table name")
	testBench = flag.String("test.bench", ".",
		"run only those benchmarks matching the regular expression")
)

const Postgres = "postgres"

var nsha = 7

var usage = `Usage: gobenchdb [options...]

Options:
  -conn        postgres database connection string
  -table       postgres table name
  -test.bench  run only those benchmarks matching the regular expression`

func main() {
	// Parse command line args
	flag.Parse()
	c := *conn
	t := *table
	tregex := *testBench

	if c == "" {
		UsageExit("database conn must be specified")

	}
	if t == "" {
		UsageExit("database table must be specified")
	}

	// Initalize a BenchPSQL to and run benchmarks.
	err := (&benchdb.BenchPSQL{
		Config: &benchdb.BenchDBConfig{
			Regex:  tregex,
			ShaLen: nsha,
		},
		Driver:    Postgres,
		ConnStr:   c,
		TableName: t,
	}).Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}
}

func UsageExit(message string) {
	if message != "" {
		fmt.Fprintf(os.Stderr, message)
		fmt.Fprintf(os.Stderr, "\n")
	}
	fmt.Fprintf(os.Stderr, usage)
	fmt.Fprintf(os.Stderr, "\n")
	os.Exit(1)
}
<|endoftext|>


<commit_before>package distro

import (
	"time"

	"github.com/drasko/edgex-export"
	"github.com/drasko/edgex-export/mongo"
	"go.uber.org/zap"
)

// To be removed when any other formater is implemented
type dummyFormat struct {
}

func (dummy dummyFormat) Format( /*event*/ ) []byte {
	return []byte("dummy")
}

var dummy dummyFormat

func (reg *RegistrationInfo) update(newReg export.Registration) bool {
	reg.registration = newReg

	reg.format = nil
	switch newReg.Format {
	case export.FormatJSON:
		// TODO reg.format = distro.NewJsonFormat()
		reg.format = dummy
	case export.FormatXML:
		// TODO reg.format = distro.NewXmlFormat()
		reg.format = dummy
	case export.FormatSerialized:
		// TODO reg.format = distro.NewSerializedFormat()
	case export.FormatIoTCoreJSON:
		// TODO reg.format = distro.NewIotCoreFormat()
	case export.FormatAzureJSON:
		// TODO reg.format = distro.NewAzureFormat()
	case export.FormatCSV:
		// TODO reg.format = distro.NewCsvFormat()
	default:
		logger.Info("Format not supported: ", zap.String("format", newReg.Format))
	}

	reg.compression = nil
	switch newReg.Compression {
	case export.CompNone:
		reg.compression = nil
	case export.CompGzip:
		reg.compression = gzipTransformer{}
	case export.CompZip:
		// TODO reg.compression = distro.NewZipComppression()
	default:
		logger.Info("Compression not supported: ", zap.String("compression", newReg.Compression))
	}

	reg.sender = nil
	switch newReg.Destination {
	case export.DestMQTT:
		reg.sender = NewMqttSender(newReg.Addressable)
	case export.DestZMQ:
		logger.Info("Destination ZMQ is not supported")
	case export.DestIotCoreMQTT:
		// TODO reg.sender = distro.NewIotCoreSender("TODO URL")
	case export.DestAzureMQTT:
		// TODO reg.sender = distro.NewAzureSender("TODO URL")
	case export.DestRest:
		reg.sender = NewHttpSender(newReg.Addressable)
	default:
		logger.Info("Destination not supported: ", zap.String("destination", newReg.Destination))
	}
	if reg.format == nil || reg.sender == nil {
		logger.Error("Registration not supported")
		return false
	}

	reg.chRegistration = make(chan *RegistrationInfo)
	reg.chEvent = make(chan bool)

	return true
}

func (reg RegistrationInfo) processEvent( /*, event*/ ) {
	// Valid Event Filter, needed?

	// TODO Device filtering

	// TODO Value filtering
	formated := reg.format.Format( /* event*/ )
	compressed := formated
	if reg.compression != nil {
		compressed = reg.compression.Transform(formated)
	}

	encrypted := compressed
	if reg.encrypt != nil {
		encrypted = reg.encrypt.Transform(compressed)
	}

	reg.sender.Send(encrypted)
}

func registrationLoop(registration RegistrationInfo) {
	logger.Info("registration loop started")
	reg := registration
	for {
		select {
		case /*event :=*/ <-reg.chEvent:
			// fmt.Println("received event", event)
			reg.processEvent()

		case newResgistration := <-reg.chRegistration:
			if newResgistration == nil {
				logger.Info("Terminate registration goroutine")
			} else {
				logger.Info("resgistration update")
			}
		}
	}
}

func Loop(repo *mongo.MongoRepository, errChan chan error) {

	var registrations []RegistrationInfo

	sourceReg := getRegistrations(repo)

	for i := range sourceReg {
		var reg RegistrationInfo
		if reg.update(sourceReg[i]) {
			registrations = append(registrations, reg)
			go registrationLoop(reg)
		}
	}

	logger.Info("Starting resgistration loop")
	for {
		select {
		case e := <-errChan:
			// TODO kill all registration goroutines
			for r := range registrations {
				registrations[r].chRegistration <- nil
			}
			logger.Info("exit msg", zap.Error(e))
			return

		case <-time.After(time.Millisecond / 10):
			//logger.Info("timeout")
			for r := range registrations {
				// TODO only sent event if it is not blocking
				registrations[r].chEvent <- true
			}
		}
	}
}
<commit_msg>Enable zlib compression<commit_after>package distro

import (
	"time"

	"github.com/drasko/edgex-export"
	"github.com/drasko/edgex-export/mongo"
	"go.uber.org/zap"
)

// To be removed when any other formater is implemented
type dummyFormat struct {
}

func (dummy dummyFormat) Format( /*event*/ ) []byte {
	return []byte("dummy")
}

var dummy dummyFormat

func (reg *RegistrationInfo) update(newReg export.Registration) bool {
	reg.registration = newReg

	reg.format = nil
	switch newReg.Format {
	case export.FormatJSON:
		// TODO reg.format = distro.NewJsonFormat()
		reg.format = dummy
	case export.FormatXML:
		// TODO reg.format = distro.NewXmlFormat()
		reg.format = dummy
	case export.FormatSerialized:
		// TODO reg.format = distro.NewSerializedFormat()
	case export.FormatIoTCoreJSON:
		// TODO reg.format = distro.NewIotCoreFormat()
	case export.FormatAzureJSON:
		// TODO reg.format = distro.NewAzureFormat()
	case export.FormatCSV:
		// TODO reg.format = distro.NewCsvFormat()
	default:
		logger.Info("Format not supported: ", zap.String("format", newReg.Format))
	}

	reg.compression = nil
	switch newReg.Compression {
	case export.CompNone:
		reg.compression = nil
	case export.CompGzip:
		reg.compression = gzipTransformer{}
	case export.CompZip:
		reg.compression = zlibTransformer{}
	default:
		logger.Info("Compression not supported: ", zap.String("compression", newReg.Compression))
	}

	reg.sender = nil
	switch newReg.Destination {
	case export.DestMQTT:
		reg.sender = NewMqttSender(newReg.Addressable)
	case export.DestZMQ:
		logger.Info("Destination ZMQ is not supported")
	case export.DestIotCoreMQTT:
		// TODO reg.sender = distro.NewIotCoreSender("TODO URL")
	case export.DestAzureMQTT:
		// TODO reg.sender = distro.NewAzureSender("TODO URL")
	case export.DestRest:
		reg.sender = NewHttpSender(newReg.Addressable)
	default:
		logger.Info("Destination not supported: ", zap.String("destination", newReg.Destination))
	}
	if reg.format == nil || reg.sender == nil {
		logger.Error("Registration not supported")
		return false
	}

	reg.chRegistration = make(chan *RegistrationInfo)
	reg.chEvent = make(chan bool)

	return true
}

func (reg RegistrationInfo) processEvent( /*, event*/ ) {
	// Valid Event Filter, needed?

	// TODO Device filtering

	// TODO Value filtering
	formated := reg.format.Format( /* event*/ )
	compressed := formated
	if reg.compression != nil {
		compressed = reg.compression.Transform(formated)
	}

	encrypted := compressed
	if reg.encrypt != nil {
		encrypted = reg.encrypt.Transform(compressed)
	}

	reg.sender.Send(encrypted)
}

func registrationLoop(registration RegistrationInfo) {
	logger.Info("registration loop started")
	reg := registration
	for {
		select {
		case /*event :=*/ <-reg.chEvent:
			// fmt.Println("received event", event)
			reg.processEvent()

		case newResgistration := <-reg.chRegistration:
			if newResgistration == nil {
				logger.Info("Terminate registration goroutine")
			} else {
				logger.Info("resgistration update")
			}
		}
	}
}

func Loop(repo *mongo.MongoRepository, errChan chan error) {

	var registrations []RegistrationInfo

	sourceReg := getRegistrations(repo)

	for i := range sourceReg {
		var reg RegistrationInfo
		if reg.update(sourceReg[i]) {
			registrations = append(registrations, reg)
			go registrationLoop(reg)
		}
	}

	logger.Info("Starting resgistration loop")
	for {
		select {
		case e := <-errChan:
			// TODO kill all registration goroutines
			for r := range registrations {
				registrations[r].chRegistration <- nil
			}
			logger.Info("exit msg", zap.Error(e))
			return

		case <-time.After(time.Millisecond / 10):
			//logger.Info("timeout")
			for r := range registrations {
				// TODO only sent event if it is not blocking
				registrations[r].chEvent <- true
			}
		}
	}
}
<|endoftext|>


<commit_before>package filter

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log"
	"time"

	vnet "v2ray.com/core/common/net"
	vsession "v2ray.com/core/common/session"
	vrouting "v2ray.com/core/features/routing"

	"github.com/eycorsican/go-tun2socks/route"
)

// Filter is used for filtering IP packets comming from TUN.
type Filter interface {
	io.Writer
}

type icmpFilter struct {
	writer io.Writer
	delay  int
}

func NewICMPFilter(w io.Writer, delay int) Filter {
	return &icmpFilter{writer: w, delay: delay}
}

func (w *icmpFilter) Write(buf []byte) (int, error) {
	if uint8(buf[9]) == route.PROTOCOL_ICMP {
		payload := make([]byte, len(buf))
		copy(payload, buf)
		go func(data []byte) {
			time.Sleep(time.Duration(w.delay) * time.Millisecond)
			_, err := w.writer.Write(data)
			if err != nil {
				log.Fatal("failed to input data to the stack: %v", err)
			}
		}(payload)
		return len(buf), nil
	} else {
		return w.writer.Write(buf)
	}
}

type routingFilter struct {
	writer  io.Writer
	router  vrouting.Router
	gateway string
}

func NewRoutingFilter(w io.Writer, router vrouting.Router, gateway string) Filter {
	return &routingFilter{writer: w, router: router, gateway: gateway}
}

func (w *routingFilter) Write(buf []byte) (int, error) {
	ipVersion := route.PeekIPVersion(buf)
	if ipVersion == route.IPVERSION_6 {
		// TODO No IPv6 support currently
		return w.writer.Write(buf)
	}

	if ipVersion != route.IPVERSION_4 && ipVersion != route.IPVERSION_6 {
		log.Fatal("not an IP packet: %v", buf)
	}

	protocol := route.PeekProtocol(buf)
	if protocol != "tcp" && protocol != "udp" {
		return w.writer.Write(buf)
	}
	if protocol == "tcp" && !route.IsSYNSegment(buf) {
		return w.writer.Write(buf)
	}

	destAddr := route.PeekDestinationAddress(buf)
	destPort := route.PeekDestinationPort(buf)

	dest, err := vnet.ParseDestination(fmt.Sprintf("%s:%s:%d", protocol, destAddr, destPort.Value()))
	if err != nil {
		return 0, errors.New(fmt.Sprintf("failed to parse destination: %v", err))
	}

	ctx := vsession.ContextWithOutbound(context.Background(), &vsession.Outbound{
		Target: dest,
	})

	tag, err := w.router.PickRoute(ctx)
	if err == nil && tag == "direct" {
		err := route.AddRoute(dest.Address.String(), "255.255.255.255", w.gateway)
		if err == nil {
			// Discarding the packet so it will be retransmitted, and hopefully retransmitted packets will
			// use the new route.
			//
			// TODO: On macOS, it appears that even though the route is added to the routing table, subsequent
			// retransmitted packets will continue using the old routing policy. Local client must create a new
			// socket for utilizing the new route. Other platforms are not tested.
			// Maybe this helps: https://www.unix.com/man-page/osx/4/route/
			// log.Printf("added a direct route for destination %v, packets need re-routing, dropped", dest)
			return len(buf), nil
		} else {
			log.Printf("adding route for %v failed: %v", dest, err)
		}
	}

	return w.writer.Write(buf)
}
<commit_msg>create dest directly from addr and port, instead of from string<commit_after>package filter

import (
	"context"
	"io"
	"log"
	"time"

	vnet "v2ray.com/core/common/net"
	vsession "v2ray.com/core/common/session"
	vrouting "v2ray.com/core/features/routing"

	"github.com/eycorsican/go-tun2socks/route"
)

// Filter is used for filtering IP packets comming from TUN.
type Filter interface {
	io.Writer
}

type icmpFilter struct {
	writer io.Writer
	delay  int
}

func NewICMPFilter(w io.Writer, delay int) Filter {
	return &icmpFilter{writer: w, delay: delay}
}

func (w *icmpFilter) Write(buf []byte) (int, error) {
	if uint8(buf[9]) == route.PROTOCOL_ICMP {
		payload := make([]byte, len(buf))
		copy(payload, buf)
		go func(data []byte) {
			time.Sleep(time.Duration(w.delay) * time.Millisecond)
			_, err := w.writer.Write(data)
			if err != nil {
				log.Fatal("failed to input data to the stack: %v", err)
			}
		}(payload)
		return len(buf), nil
	} else {
		return w.writer.Write(buf)
	}
}

type routingFilter struct {
	writer  io.Writer
	router  vrouting.Router
	gateway string
}

func NewRoutingFilter(w io.Writer, router vrouting.Router, gateway string) Filter {
	return &routingFilter{writer: w, router: router, gateway: gateway}
}

func (w *routingFilter) Write(buf []byte) (int, error) {
	ipVersion := route.PeekIPVersion(buf)
	if ipVersion == route.IPVERSION_6 {
		// TODO No IPv6 support currently
		return w.writer.Write(buf)
	}

	if ipVersion != route.IPVERSION_4 && ipVersion != route.IPVERSION_6 {
		log.Fatal("not an IP packet: %v", buf)
	}

	protocol := route.PeekProtocol(buf)
	if protocol != "tcp" && protocol != "udp" {
		return w.writer.Write(buf)
	}
	if protocol == "tcp" && !route.IsSYNSegment(buf) {
		return w.writer.Write(buf)
	}

	destAddr := route.PeekDestinationAddress(buf)
	destPort := route.PeekDestinationPort(buf)

	var dest vnet.Destination
	switch protocol {
	case "tcp":
		dest = vnet.TCPDestination(destAddr, destPort)
	case "udp":
		dest = vnet.UDPDestination(destAddr, destPort)
	default:
		panic("invalid protocol")
	}

	ctx := vsession.ContextWithOutbound(context.Background(), &vsession.Outbound{
		Target: dest,
	})

	tag, err := w.router.PickRoute(ctx)
	if err == nil && tag == "direct" {
		err := route.AddRoute(dest.Address.String(), "255.255.255.255", w.gateway)
		if err == nil {
			// Discarding the packet so it will be retransmitted, and hopefully retransmitted packets will
			// use the new route.
			//
			// TODO: On macOS, it appears that even though the route is added to the routing table, subsequent
			// retransmitted packets will continue using the old routing policy. Local client must create a new
			// socket for utilizing the new route. Other platforms are not tested.
			// Maybe this helps: https://www.unix.com/man-page/osx/4/route/
			// log.Printf("added a direct route for destination %v, packets need re-routing, dropped", dest)
			return len(buf), nil
		} else {
			log.Printf("adding route for %v failed: %v", dest, err)
		}
	}

	return w.writer.Write(buf)
}
<|endoftext|>


<commit_before>package rat

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

// ExpectStatus inspects the response status code.
// If the value is not expected, the complete request, response is logged (iff verbose).
// Return true if the status is as expected.
func ExpectStatus(t T, r *http.Response, status int) bool {
	if r == nil {
		t.Logf("ExpectStatus failed: got nil but want Http response")
		return false
	}
	if r.StatusCode != status {
		t.Errorf("ExpectStatus failed: got status %d but want %d, %s %v", r.StatusCode, status, r.Request.Method, r.Request.URL)
		if testing.Verbose() {
			Dump(t, r)
		}
		return false
	}
	return true
}

// CheckError simply tests the error and fail is not undefined.
// This is implicity called after sending a Http request.
func CheckError(t T, err error) {
	if err != nil {
		t.Errorf("CheckError failed: did not expect to receive err: %v", err)
	}
}

// ExpectHeader inspects the header of the response.
func ExpectHeader(t T, r *http.Response, name, value string) {
	if r == nil {
		t.Errorf("ExpectHeader failed: got nil but want a Http response")
	}
	rname := r.Header.Get(name)
	if rname != value {
		t.Errorf("ExpectHeader failed: got header %s=%s but want %s", name, rname, value)
	}
}

// ExpectJSONHash tries to unmarshal the response body into a Go map callback parameter.
// Fail if the body could not be read or if unmarshalling was not possible.
func ExpectJSONHash(t T, r *http.Response, callback func(hash map[string]interface{})) {
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectJSONHash failed: unable to read response body:%v", err)
		return
	}
	dict := map[string]interface{}{}
	err = json.Unmarshal(data, &dict)
	if err != nil {
		t.Errorf("ExpectJSONHash failed: unable to unmarshal Json:%v", err)
	}
	callback(dict)
}

// ExpectJSONArray tries to unmarshal the response body into a Go slice callback parameter.
// Fail if the body could not be read or if unmarshalling was not possible.
func ExpectJSONArray(t T, r *http.Response, callback func(array []interface{})) {
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectJSONArray failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	slice := []interface{}{}
	err = json.Unmarshal(data, &slice)
	if err != nil {
		t.Errorf("ExpectJSONArray failed: unable to unmarshal Json:%v", err)
	}
	callback(slice)
}

// ExpectString reads the response body into a Go string callback parameter.
// Fail if the body could not be read or unmarshalled.
func ExpectString(t T, r *http.Response, callback func(content string)) {
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectString failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	callback(string(data))
}

// ExpectXMLDocument tries to unmarshal the response body into fields of the provided document (struct).
// Fail if the body could not be read or unmarshalled.
func ExpectXMLDocument(t T, r *http.Response, doc interface{}) {
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectXMLDocument failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	err = xml.Unmarshal(data, doc)
	if err != nil {
		t.Errorf("ExpectXMLDocument failed: unable to unmarshal Xml:%v", err)
	}
}

// ExpectJSONDocument tries to unmarshal the response body into fields of the provided document (struct).
// Fail if the body could not be read or unmarshalled.
func ExpectJSONDocument(t T, r *http.Response, doc interface{}) {
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectJSONDocument failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	err = json.Unmarshal(data, doc)
	if err != nil {
		t.Errorf("ExpectJSONDocument failed: unable to unmarshal Json:%v", err)
	}
}
<commit_msg>check for nil response<commit_after>package rat

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

// ExpectStatus inspects the response status code.
// If the value is not expected, the complete request, response is logged (iff verbose).
// Return true if the status is as expected.
func ExpectStatus(t T, r *http.Response, status int) bool {
	if r == nil {
		t.Logf("ExpectStatus failed: got nil but want Http response")
		return false
	}
	if r.StatusCode != status {
		t.Errorf("ExpectStatus failed: got status %d but want %d, %s %v", r.StatusCode, status, r.Request.Method, r.Request.URL)
		if testing.Verbose() {
			Dump(t, r)
		}
		return false
	}
	return true
}

// CheckError simply tests the error and fail is not undefined.
// This is implicity called after sending a Http request.
func CheckError(t T, err error) {
	if err != nil {
		t.Errorf("CheckError failed: did not expect to receive err: %v", err)
	}
}

// ExpectHeader inspects the header of the response.
func ExpectHeader(t T, r *http.Response, name, value string) {
	if r == nil {
		t.Errorf("ExpectHeader failed: got nil but want a Http response")
	}
	rname := r.Header.Get(name)
	if rname != value {
		t.Errorf("ExpectHeader failed: got header %s=%s but want %s", name, rname, value)
	}
}

// ExpectJSONHash tries to unmarshal the response body into a Go map callback parameter.
// Fail if the body could not be read or if unmarshalling was not possible.
func ExpectJSONHash(t T, r *http.Response, callback func(hash map[string]interface{})) {
	if r == nil {
		t.Errorf("ExpectJSONHash failed: no response available")
		return
	}
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectJSONHash failed: unable to read response body:%v", err)
		return
	}
	dict := map[string]interface{}{}
	err = json.Unmarshal(data, &dict)
	if err != nil {
		t.Errorf("ExpectJSONHash failed: unable to unmarshal Json:%v", err)
	}
	callback(dict)
}

// ExpectJSONArray tries to unmarshal the response body into a Go slice callback parameter.
// Fail if the body could not be read or if unmarshalling was not possible.
func ExpectJSONArray(t T, r *http.Response, callback func(array []interface{})) {
	if r == nil {
		t.Errorf("ExpectJSONArray failed: no response available")
		return
	}
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectJSONArray failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	slice := []interface{}{}
	err = json.Unmarshal(data, &slice)
	if err != nil {
		t.Errorf("ExpectJSONArray failed: unable to unmarshal Json:%v", err)
	}
	callback(slice)
}

// ExpectString reads the response body into a Go string callback parameter.
// Fail if the body could not be read or unmarshalled.
func ExpectString(t T, r *http.Response, callback func(content string)) {
	if r == nil {
		t.Errorf("ExpectString failed: no response available")
		return
	}
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectString failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	callback(string(data))
}

// ExpectXMLDocument tries to unmarshal the response body into fields of the provided document (struct).
// Fail if the body could not be read or unmarshalled.
func ExpectXMLDocument(t T, r *http.Response, doc interface{}) {
	if r == nil {
		t.Errorf("ExpectXMLDocument failed: no response available")
		return
	}
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectXMLDocument failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	err = xml.Unmarshal(data, doc)
	if err != nil {
		t.Errorf("ExpectXMLDocument failed: unable to unmarshal Xml:%v", err)
	}
}

// ExpectJSONDocument tries to unmarshal the response body into fields of the provided document (struct).
// Fail if the body could not be read or unmarshalled.
func ExpectJSONDocument(t T, r *http.Response, doc interface{}) {
	if r == nil {
		t.Errorf("ExpectJSONDocument failed: no response available")
		return
	}
	data, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		t.Errorf("ExpectJSONDocument failed: unable to read response body:%v", err)
		return
	}
	t.Logf("Response body:\n%s", string(data))
	err = json.Unmarshal(data, doc)
	if err != nil {
		t.Errorf("ExpectJSONDocument failed: unable to unmarshal Json:%v", err)
	}
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2013-2014, Jeremy Bingham (<jbingham@gmail.com>)
 *
 * 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 search

import (
	"fmt"
	"github.com/ctdk/goas/v2/logger"
	"github.com/ctdk/goiardi/indexer"
	//"github.com/ctdk/goiardi/util"
	"regexp"
)

type PostgresSearch struct {
}

type PgQuery struct {
	queryChain Queryable
	paths []string
	queryStrs []string
	arguments []string
}

func (p *PostgresSearch) Search(idx string, q string, rows int, sortOrder string, start int, partialData map[string]interface{}) ([]map[string]interface{}, error) {
	// keep up with the ersatz solr.
	qq := &Tokenizer{Buffer: q}
	qq.Init()
	if err := qq.Parse(); err != nil {
		return nil, err
	}
	qq.Execute()
	qchain := qq.Evaluate()

	pgQ := &PgQuery{ queryChain: qchain }

	logger.Debugf("what on earth is the chain? %q", qchain)
	err := pgQ.execute()
	if err != nil {
		return nil, err
	}

	// dummy
	dres := make([]map[string]interface{}, 0)
	return dres, nil
}

func (p *PostgresSearch) GetEndpoints() []string {
	// TODO: deal with possible errors
	endpoints, err := indexer.Endpoints()
	return endpoints
	if err != nil {
		panic(err)
	}
	return endpoints
}

func (pq *PgQuery) execute(startTableID ...*int) error {
	p := pq.queryChain
	curOp := OpNotAnOp
	opMap := map[Op]string{
		OpNotAnOp: "(not an op)",
		OpUnaryNot: "not",
		OpUnaryReq: "req",
		OpUnaryPro: "pro",
		OpBinAnd: "and",
		OpBinOr: "or",
		OpBoost: "boost",
		OpFuzzy: "fuzzy",
		OpStartGroup: "start group",
		OpEndGroup: "end group",
		OpStartIncl: "start inc",
		OpEndIncl: "end inc",
		OpStartExcl: "start exc",
		OpEndExcl: "end exc",
	}
	var t *int
	if len(startTableID) == 0 {
		z := 0
		t = &z
	} else {
		t = startTableID[0]
	}
	for p != nil {
		switch c := p.(type) {
		case *BasicQuery:
			pq.paths = append(pq.paths, string(c.field))
			logger.Debugf("basic t%d: field: %s op: %s term: %+v complete %v", *t, c.field, opMap[c.op], c.term, c.complete)
			args, qstr := buildBasicQuery(c.field, c.term, t)
			pq.args = append(pq.args, args...)
			pq.queryStrs = append(pq.queryStrs, qstr)
			*t++
		case *GroupedQuery:
			pq.paths = append(pq.paths, string(c.field))
			logger.Debugf("grouped t%d: field: %s op: %s terms: %+v complete %v", *t, c.field, opMap[c.op], c.terms, c.complete)
			*t++
		case *RangeQuery:
			pq.paths = append(pq.paths, string(c.field))
			logger.Debugf("range t%d: field %s op %s start %s end %s inclusive %v complete %v", *t, c.field, opMap[c.op], c.start, c.end, c.inclusive, c.complete)
			*t++
		case *SubQuery:
			logger.Debugf("STARTING SUBQUERY: op %s complete %v", opMap[c.op], c.complete)
			newq, nend, nerr := extractSubQuery(c)
			if nerr != nil {
				return nerr
			}
			p = nend
			logger.Debugf("OP NOW: %s", opMap[p.Op()])
			np := &PgQuery{ queryChain: newq }
			err := np.execute(t)
			if err != nil {
				return err
			}
			pq.paths = append(pq.paths, np.paths...)
			logger.Debugf("ENDING SUBQUERY")
		default:
			err := fmt.Errorf("Unknown type %T for query", c)
			return err
		}
		//curOp = p.Op()
		p = p.Next()
	}
	logger.Debugf("paths: %v", pq.paths)
	logger.Debugf("number of tables: %d", *t)
	return nil
}

func buildBasicQuery(field Field, term QueryTerm, tNum *int, op Op) ([]string, string) {
	var cop string
	r := regex.MustCompile(`\*|\?`)
	if r.MatchString(term) {
		cop = "LIKE"
	} else {
		cop = "="
	}
	var opStr string
	if op != OpNotAnOp {
		if op == OpBinAnd {
			opStr = " AND "
		} else {
			opStr = " OR "
		}
	}
	var unaryOp string

	var q string
	args := []string{ field }
	if term == "*" {
		q = fmt.Sprintf("(f%d.path ~ _ARG_)", tNum)
	} else {
		q = fmt.Sprintf("(f%d.path ~ _ARG_ AND f%d.value %s _ARG_)", tNum, tNum, cop)
		args = append(args, term.term)
	}

	return args, q
}
<commit_msg>basic queries getting closer<commit_after>/*
 * Copyright (c) 2013-2014, Jeremy Bingham (<jbingham@gmail.com>)
 *
 * 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 search

import (
	"fmt"
	"github.com/ctdk/goas/v2/logger"
	"github.com/ctdk/goiardi/indexer"
	//"github.com/ctdk/goiardi/util"
	"regexp"
)

type PostgresSearch struct {
}

type PgQuery struct {
	queryChain Queryable
	paths []string
	queryStrs []string
	arguments []string
}

func (p *PostgresSearch) Search(idx string, q string, rows int, sortOrder string, start int, partialData map[string]interface{}) ([]map[string]interface{}, error) {
	// keep up with the ersatz solr.
	qq := &Tokenizer{Buffer: q}
	qq.Init()
	if err := qq.Parse(); err != nil {
		return nil, err
	}
	qq.Execute()
	qchain := qq.Evaluate()

	pgQ := &PgQuery{ queryChain: qchain }

	logger.Debugf("what on earth is the chain? %q", qchain)
	err := pgQ.execute()
	if err != nil {
		return nil, err
	}

	// dummy
	dres := make([]map[string]interface{}, 0)
	return dres, nil
}

func (p *PostgresSearch) GetEndpoints() []string {
	// TODO: deal with possible errors
	endpoints, err := indexer.Endpoints()
	return endpoints
	if err != nil {
		panic(err)
	}
	return endpoints
}

func (pq *PgQuery) execute(startTableID ...*int) error {
	p := pq.queryChain
	curOp := OpNotAnOp
	opMap := map[Op]string{
		OpNotAnOp: "(not an op)",
		OpUnaryNot: "not",
		OpUnaryReq: "req",
		OpUnaryPro: "pro",
		OpBinAnd: "and",
		OpBinOr: "or",
		OpBoost: "boost",
		OpFuzzy: "fuzzy",
		OpStartGroup: "start group",
		OpEndGroup: "end group",
		OpStartIncl: "start inc",
		OpEndIncl: "end inc",
		OpStartExcl: "start exc",
		OpEndExcl: "end exc",
	}
	var t *int
	if len(startTableID) == 0 {
		z := 0
		t = &z
	} else {
		t = startTableID[0]
	}
	for p != nil {
		switch c := p.(type) {
		case *BasicQuery:
			pq.paths = append(pq.paths, string(c.field))
			logger.Debugf("basic t%d: field: %s op: %s term: %+v complete %v", *t, c.field, opMap[c.op], c.term, c.complete)
			args, qstr := buildBasicQuery(c.field, c.term, t, curOp)
			logger.Debugf("qstr: %s", qstr)
			pq.arguments = append(pq.arguments, args...)
			pq.queryStrs = append(pq.queryStrs, qstr)
			*t++
		case *GroupedQuery:
			pq.paths = append(pq.paths, string(c.field))
			logger.Debugf("grouped t%d: field: %s op: %s terms: %+v complete %v", *t, c.field, opMap[c.op], c.terms, c.complete)
			*t++
		case *RangeQuery:
			pq.paths = append(pq.paths, string(c.field))
			logger.Debugf("range t%d: field %s op %s start %s end %s inclusive %v complete %v", *t, c.field, opMap[c.op], c.start, c.end, c.inclusive, c.complete)
			*t++
		case *SubQuery:
			logger.Debugf("STARTING SUBQUERY: op %s complete %v", opMap[c.op], c.complete)
			newq, nend, nerr := extractSubQuery(c)
			if nerr != nil {
				return nerr
			}
			p = nend
			logger.Debugf("OP NOW: %s", opMap[p.Op()])
			np := &PgQuery{ queryChain: newq }
			err := np.execute(t)
			if err != nil {
				return err
			}
			pq.paths = append(pq.paths, np.paths...)
			logger.Debugf("ENDING SUBQUERY")
		default:
			err := fmt.Errorf("Unknown type %T for query", c)
			return err
		}
		curOp = p.Op()
		p = p.Next()
	}
	logger.Debugf("paths: %v", pq.paths)
	logger.Debugf("arguments: %v", pq.arguments)
	logger.Debugf("number of tables: %d", *t)
	return nil
}

func buildBasicQuery(field Field, term QueryTerm, tNum *int, op Op) ([]string, string) {
	opStr := binOp(op)
	cop := matchOp(op, term)

	var q string
	args := []string{ string(field) }
	if term.term == "*" {
		q = fmt.Sprintf("%s(f%d.path ~ _ARG_)", opStr, *tNum)
	} else {
		q = fmt.Sprintf("%s(f%d.path ~ _ARG_ AND f%d.value %s _ARG_)", opStr, *tNum, *tNum, cop)
		args = append(args, string(term.term))
	}

	return args, q
}

func matchOp(op Op, term QueryTerm) string {
	r := regexp.MustCompile(`\*|\?`)
	var cop string
	if r.MatchString(string(term.term)) {
		if term.mod == OpUnaryNot {
			cop = "NOT LIKE"
		} else {
			cop = "LIKE"
		}
	} else {
		if term.mod == OpUnaryNot {
			cop = "<>"
		} else {
			cop = "="
		}
	}
	return cop
}

func binOp(op Op) string {
	var opStr string
	if op != OpNotAnOp {
		if op == OpBinAnd {
			opStr = " AND "
		} else {
			opStr = " OR "
		}
	}
	return opStr
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"testing"

	"github.com/goby-lang/goby/vm"
)

func init() {
	_, err := os.Stat("./goby")
	if err != nil {
		fmt.Println("Goby binary not found, building")

		cmd := exec.Command("go", "build", ".")
		err = cmd.Run()
		if err != nil {
			fmt.Println("Could not build binary\n", err.Error())
			panic(err)
		}
		fmt.Println("Built. Testing ./goby")
	} else {
		fmt.Println("Using existing Goby binary. Testing ./goby")
	}

}

func execGoby(t *testing.T, args ...string) (in io.WriteCloser, out io.ReadCloser) {
	exec.Command("rm ./goby")
	exec.Command("make build")
	cmd := exec.Command("./goby", args...)

	var err error
	in, err = cmd.StdinPipe()
	if err != nil {
		t.Fatalf("Error getting stdin\n%s", err.Error())
	}

	out, err = cmd.StdoutPipe()
	if err != nil {
		t.Fatalf("Error getting stdout\n%s", err.Error())
	}

	err = cmd.Start()
	if err != nil {
		t.Fatalf("Error running goby\n%s", err.Error())
	}

	return
}

func partialReport() (md string) {

	md += fmt.Sprintf("### Goby version\n%s\n", vm.Version)
	md += fmt.Sprintf("### GOBY_ROOT\n%s\n", os.Getenv("GOBY_ROOT"))
	md += fmt.Sprintf("### Go version\n%s\n", runtime.Version())
	md += fmt.Sprintf("### GOROOT\n%s\n", os.Getenv("GOROOT"))
	md += fmt.Sprintf("### GOPATH\n%s\n", os.Getenv("GOPATH"))
	md += fmt.Sprintf("### Operating system\n%s\n", runtime.GOOS)

	return
}

func TestArgE(t *testing.T) {

	_, out := execGoby(t, "-e", "samples/error-report.gb")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), partialReport()) {
		t.Fatalf("Interpreter -e output incorect:\nExpected \n`%s` in string `\n%s`", partialReport(), string(byt))
	}
}

func TestArgI(t *testing.T) {

	in, out := execGoby(t, "-i")

	fmt.Fprintln(in, `puts "hello world"`)
	fmt.Fprintln(in, `exit`)

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if strings.HasSuffix(string(byt), "hello world\nBye") {
		t.Fatalf("Interpreter output incorect")
	}
}

func TestArgV(t *testing.T) {

	_, out := execGoby(t, "-v")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), vm.Version) {
		t.Fatalf("Interpreter -v output incorect:\nExpected '%s' in string '%s'.", vm.Version, string(byt))
	}
}

func TestArgProfileCPU(t *testing.T) {

	_, out := execGoby(t, "-profile-cpu", "samples/one_thousand_threads.gb")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if string(byt) != "500500\n" {
		t.Fatalf("Test failed, excpected 500500, got %s", string(byt))
	}
}

func TestArgProfileMem(t *testing.T) {

	_, out := execGoby(t, "-profile-mem", "samples/one_thousand_threads.gb")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if string(byt) != "500500\n" {
		t.Fatalf("Test failed, excpected 500500, got %s", string(byt))
	}
}

func TestTestCommand(t *testing.T) {
	// Folder name with slash
	_, out := execGoby(t, "test", "test_fixtures/test_command_test/")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), "Spec test 2") {
		t.Fatalf("Test files by giving folder name with slash failed, got: %s", string(byt))
	}

	// Folder name
	_, out = execGoby(t, "test", "test_fixtures/test_command_test")

	byt, err = ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), "Spec test 2") {
		t.Fatalf("Test files by giving folder name failed, got: %s", string(byt))
	}

	// File name
	_, out = execGoby(t, "test", "test_fixtures/test_command_test/test_spec.gb")

	byt, err = ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), "Spec") {
		t.Fatalf("Test files by giving file name failed, got: %s", string(byt))
	}
}
<commit_msg>Fix goby_test.go<commit_after>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"testing"

	"github.com/goby-lang/goby/vm"
)

func init() {
	_, err := os.Stat("./goby")
	if err == nil {
		err := exec.Command("rm", "./goby").Run()
		if err != nil {
			panic(err)
		}
	}

	fmt.Println("Building Goby binary")
	err = exec.Command("make", "build").Run()
	if err != nil {
		panic(err)
	}
}

func execGoby(t *testing.T, args ...string) (in io.WriteCloser, out io.ReadCloser) {
	p, err := os.Getwd()

	if err != nil {
		panic(err)
	}

	binLocation := fmt.Sprintf("%s/goby", p)
	cmd := exec.Command(binLocation, args...)

	in, err = cmd.StdinPipe()
	if err != nil {
		t.Fatalf("Error getting stdin\n%s", err.Error())
	}

	out, err = cmd.StdoutPipe()
	if err != nil {
		t.Fatalf("Error getting stdout\n%s", err.Error())
	}

	err = cmd.Start()
	if err != nil {
		t.Fatalf("Error running goby\n%s", err.Error())
	}

	return
}

func partialReport() (md string) {

	md += fmt.Sprintf("### Goby version\n%s\n", vm.Version)
	md += fmt.Sprintf("### GOBY_ROOT\n%s\n", os.Getenv("GOBY_ROOT"))
	md += fmt.Sprintf("### Go version\n%s\n", runtime.Version())
	md += fmt.Sprintf("### GOROOT\n%s\n", os.Getenv("GOROOT"))
	md += fmt.Sprintf("### GOPATH\n%s\n", os.Getenv("GOPATH"))
	md += fmt.Sprintf("### Operating system\n%s\n", runtime.GOOS)

	return
}

func TestArgE(t *testing.T) {

	_, out := execGoby(t, "-e", "samples/error-report.gb")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), partialReport()) {
		t.Fatalf("Interpreter -e output incorect:\nExpected \n`%s` in string `\n%s`", partialReport(), string(byt))
	}
}

func TestArgI(t *testing.T) {

	in, out := execGoby(t, "-i")

	fmt.Fprintln(in, `puts "hello world"`)
	fmt.Fprintln(in, `exit`)

	expectedOutput := "hello world\n"

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.HasSuffix(string(byt), expectedOutput) {
		t.Fatalf("Interpreter output incorrect. Expect '%s' to contain '%s'", string(byt), expectedOutput)
	}
}

func TestArgV(t *testing.T) {

	_, out := execGoby(t, "-v")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), vm.Version) {
		t.Fatalf("Interpreter -v output incorect:\nExpected '%s' in string '%s'.", vm.Version, string(byt))
	}
}

func TestArgProfileCPU(t *testing.T) {

	_, out := execGoby(t, "-profile-cpu", "samples/one_thousand_threads.gb")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if string(byt) != "500500\n" {
		t.Fatalf("Test failed, excpected 500500, got %s", string(byt))
	}
}

func TestArgProfileMem(t *testing.T) {

	_, out := execGoby(t, "-profile-mem", "samples/one_thousand_threads.gb")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if string(byt) != "500500\n" {
		t.Fatalf("Test failed, excpected 500500, got %s", string(byt))
	}
}

func TestTestCommand(t *testing.T) {
	// Folder name with slash
	_, out := execGoby(t, "test", "test_fixtures/test_command_test/")

	byt, err := ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), "Spec test 2") {
		t.Fatalf("Test files by giving folder name with slash failed, got: %s", string(byt))
	}

	// Folder name
	_, out = execGoby(t, "test", "test_fixtures/test_command_test")

	byt, err = ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), "Spec test 2") {
		t.Fatalf("Test files by giving folder name failed, got: %s", string(byt))
	}

	// File name
	_, out = execGoby(t, "test", "test_fixtures/test_command_test/test_spec.gb")

	byt, err = ioutil.ReadAll(out)
	if err != nil {
		t.Fatalf("Couldn't read from pipe: %s", err.Error())
	}

	if !strings.Contains(string(byt), "Spec") {
		t.Fatalf("Test files by giving file name failed, got: %s", string(byt))
	}
}
<|endoftext|>


<commit_before>package microcache

import (
	"compress/gzip"
	"io/ioutil"
	"bytes"
)

// CompressorGzip is a gzip compressor
type CompressorGzip struct {
}

// Compress clones the supplied object
// This causes a memcopy of the response body, slowing down cache writes
// Could probably be optimized
func (c CompressorGzip) Compress(res Response) Response {
	newres := res.clone()
	var buf bytes.Buffer
	zw := gzip.NewWriter(&buf)
	zw.Write(res.body)
	zw.Close()
	newres.body = buf.Bytes()
	return newres
}

// Expand modifies the supplied object
// Does not require memcopy, performance on par with no compression
func (c CompressorGzip) Expand(res Response) Response {
	buf := bytes.NewBuffer(res.body)
	zr, _ := gzip.NewReader(buf)
	res.body, _ = ioutil.ReadAll(zr)
	zr.Close()
	return res
}
<commit_msg>go fmt<commit_after>package microcache

import (
	"bytes"
	"compress/gzip"
	"io/ioutil"
)

// CompressorGzip is a gzip compressor
type CompressorGzip struct {
}

// Compress clones the supplied object
// This causes a memcopy of the response body, slowing down cache writes
// Could probably be optimized
func (c CompressorGzip) Compress(res Response) Response {
	newres := res.clone()
	var buf bytes.Buffer
	zw := gzip.NewWriter(&buf)
	zw.Write(res.body)
	zw.Close()
	newres.body = buf.Bytes()
	return newres
}

// Expand modifies the supplied object
// Does not require memcopy, performance on par with no compression
func (c CompressorGzip) Expand(res Response) Response {
	buf := bytes.NewBuffer(res.body)
	zr, _ := gzip.NewReader(buf)
	res.body, _ = ioutil.ReadAll(zr)
	zr.Close()
	return res
}
<|endoftext|>


<commit_before>package gitmediafilters

import (
	".."
	"crypto/sha1"
	"encoding/hex"
	"io"
	"os"
)

type CleanedAsset struct {
	Size          int64
	File          *os.File
	Sha           string
	mediafilepath string
}

func Clean(reader io.Reader) (*CleanedAsset, error) {
	tmp, err := gitmedia.TempFile()
	if err != nil {
		return nil, err
	}

	sha1Hash := sha1.New()
	writer := io.MultiWriter(sha1Hash, tmp)
	written, _ := io.Copy(writer, reader)

	return &CleanedAsset{written, tmp, hex.EncodeToString(sha1Hash.Sum(nil)), ""}, nil
}

func (a *CleanedAsset) Close() error {
	return os.Remove(a.File.Name())
}
<commit_msg>don't ever shadow errors<commit_after>package gitmediafilters

import (
	".."
	"crypto/sha1"
	"encoding/hex"
	"io"
	"os"
)

type CleanedAsset struct {
	Size          int64
	File          *os.File
	Sha           string
	mediafilepath string
}

func Clean(reader io.Reader) (*CleanedAsset, error) {
	tmp, err := gitmedia.TempFile()
	if err != nil {
		return nil, err
	}

	sha1Hash := sha1.New()
	writer := io.MultiWriter(sha1Hash, tmp)
	written, err := io.Copy(writer, reader)

	return &CleanedAsset{written, tmp, hex.EncodeToString(sha1Hash.Sum(nil)), ""}, err
}

func (a *CleanedAsset) Close() error {
	return os.Remove(a.File.Name())
}
<|endoftext|>


<commit_before>package export

import (
	"encoding/json"
	"os"
	"os/exec"
	"time"
)

type ExportedImage struct {
	Path         string
	JsonPath     string
	VersionPath  string
	LayerTarPath string
	LayerDirPath string
	LayerConfig  *LayerConfig
}

func newLayerConfig(id, parent, comment string) *LayerConfig {
	return &LayerConfig{
		Id:            id,
		Parent:        parent,
		Comment:       comment,
		Created:       time.Now().UTC(),
		DockerVersion: "0.1.2",
		Architecture:  "x86_64",
	}
}

func (e *ExportedImage) WriteVersion() error {
	fp := e.VersionPath
	f, err := os.Create(fp)
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.WriteString("1.0")
	if err != nil {
		return err
	}

	return err
}

func (e *ExportedImage) WriteJson() error {
	fp := e.JsonPath
	f, err := os.Create(fp)
	if err != nil {
		return err
	}
	defer f.Close()

	jb, err := json.Marshal(e.LayerConfig)
	if err != nil {
		return err
	}

	_, err = f.WriteString(string(jb))
	if err != nil {
		return err
	}

	return err
}

func (e *ExportedImage) CreateDirs() error {
	return os.MkdirAll(e.Path, 0755)
}

func (e *ExportedImage) TarLayer() error {
	cwd, err := os.Getwd()
	if err != nil {
		return err
	}

	err = os.Chdir(e.LayerDirPath)
	if err != nil {
		return err
	}
	defer os.Chdir(cwd)

	cmd := exec.Command("sudo", "/bin/sh", "-c", "tar cvf ../layer.tar ./")
	out, err := cmd.CombinedOutput()
	if err != nil {
		println(string(out))
		return err
	}
	return nil
}

func (e *ExportedImage) RemoveLayerDir() error {
	return os.RemoveAll(e.LayerDirPath)
}

func (e *ExportedImage) ExtractLayerDir() error {
	err := os.MkdirAll(e.LayerDirPath, 0755)
	if err != nil {
		return err
	}

	out, err := ExtractTar(e.LayerTarPath, e.LayerDirPath)
	if err != nil {
		println(string(out))
		return err
	}
	return nil
}
<commit_msg>Detect if running as superuser, and don't do privilege escalation when tarring files.<commit_after>package export

import (
	"encoding/json"
	"os"
	"os/exec"
	"time"
)

type ExportedImage struct {
	Path         string
	JsonPath     string
	VersionPath  string
	LayerTarPath string
	LayerDirPath string
	LayerConfig  *LayerConfig
}

func newLayerConfig(id, parent, comment string) *LayerConfig {
	return &LayerConfig{
		Id:            id,
		Parent:        parent,
		Comment:       comment,
		Created:       time.Now().UTC(),
		DockerVersion: "0.1.2",
		Architecture:  "x86_64",
	}
}

func (e *ExportedImage) WriteVersion() error {
	fp := e.VersionPath
	f, err := os.Create(fp)
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.WriteString("1.0")
	if err != nil {
		return err
	}

	return err
}

func (e *ExportedImage) WriteJson() error {
	fp := e.JsonPath
	f, err := os.Create(fp)
	if err != nil {
		return err
	}
	defer f.Close()

	jb, err := json.Marshal(e.LayerConfig)
	if err != nil {
		return err
	}

	_, err = f.WriteString(string(jb))
	if err != nil {
		return err
	}

	return err
}

func (e *ExportedImage) CreateDirs() error {
	return os.MkdirAll(e.Path, 0755)
}

func (e *ExportedImage) TarLayer() error {
	cwd, err := os.Getwd()
	if err != nil {
		return err
	}

	err = os.Chdir(e.LayerDirPath)
	if err != nil {
		return err
	}
	defer os.Chdir(cwd)

	var cmd *exec.Cmd
	if os.Geteuid() != 0 {
		cmd = exec.Command("sudo", "/bin/sh", "-c", "tar cvf ../layer.tar ./")
	} else {
		cmd = exec.Command("/bin/sh", "-c", "tar cvf ../layer.tar ./")
	}
	out, err := cmd.CombinedOutput()
	if err != nil {
		println(string(out))
		return err
	}
	return nil
}

func (e *ExportedImage) RemoveLayerDir() error {
	return os.RemoveAll(e.LayerDirPath)
}

func (e *ExportedImage) ExtractLayerDir() error {
	err := os.MkdirAll(e.LayerDirPath, 0755)
	if err != nil {
		return err
	}

	out, err := ExtractTar(e.LayerTarPath, e.LayerDirPath)
	if err != nil {
		println(string(out))
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package fmtutil

import "strconv"

// SI represents an integer which can format itself with SI letters.
type SI uint64

const (
	K = 1024 << (10 * iota)
	M
	G
	T
	P
	E
)

func (x SI) String() string {
	n := 0.0
	s := ""
	switch {
	case x < K:
		return strconv.FormatUint(uint64(x), 10)
	case x < M:
		s = "k"
		n = float64(x) / K
	case x < G:
		s = "M"
		n = float64(x) / M
	case x < T:
		s = "G"
		n = float64(x) / G
	case x < P:
		s = "T"
		n = float64(x) / T
	case x < E:
		s = "P"
		n = float64(x) / P
	default:
		s = "E"
		n = float64(x) / E
	}

	return strconv.FormatFloat(Round(n, 1), 'f', -1, 64) + s
}

type SIUnit struct {
	N uint64
	U string
}

func (x SIUnit) String() string {
	return SI(x.N).String() + x.U
}

// Round rounds a number to the given number of total digits.
func Round(n float64, prec int) float64 {
	n *= float64(prec) * 10
	x := float64(int64(n + 0.5))
	return x / (float64(prec) * 10)
}
<commit_msg>add LongDuration and Bytes<commit_after>// Package fmtutil implements formatting for numbers as common meaningful
// values.
package fmtutil

import (
	"fmt"
	"strconv"
	"time"
)

// SI represents an integer which can format itself with SI prefixes.
type SI uint64

const (
	K = 1024 << (10 * iota)
	M
	G
	T
	P
	E
)

func (x SI) String() string {
	n := 0.0
	s := ""
	switch {
	case x < K:
		return strconv.FormatUint(uint64(x), 10)
	case x < M:
		s = "k"
		n = float64(x) / K
	case x < G:
		s = "M"
		n = float64(x) / M
	case x < T:
		s = "G"
		n = float64(x) / G
	case x < P:
		s = "T"
		n = float64(x) / T
	case x < E:
		s = "P"
		n = float64(x) / P
	default:
		s = "E"
		n = float64(x) / E
	}

	return strconv.FormatFloat(Round(n, 1), 'f', -1, 64) + s
}

type SIUnit struct {
	N uint64
	U string
}

func (x SIUnit) String() string {
	return SI(x.N).String() + x.U
}

// Round rounds a number to the given number of total digits.
func Round(n float64, prec int) float64 {
	n *= float64(prec) * 10
	x := float64(int64(n + 0.5))
	return x / (float64(prec) * 10)
}

// Bytes is a common use case of SI prefix formatting.
type Bytes uint64

func (b Bytes) String() string {
	return SI(b).String() + "B"
}

const (
	Sec   = time.Second
	Min   = Sec * 60
	Hr    = Min * 60
	Day   = Hr * 24
	Week  = Day * 7
	Month = Day * 30
	Year  = Day * 365
)

// LongDuration formats a duration that is most likely much longer than what
// package time will handle. It uses the units seconds, minutes, hours, days,
// weeks, months (30 days), and years (365 days).
func LongDuration(n time.Duration) string {
	p := func(n time.Duration, s string) string {
		return fmt.Sprintf("%d%s", n, s)
	}

	switch {
	case n < Sec:
		return n.String()
	case n < Min:
		return p(n/Sec, "s")
	case n < Hr:
		return p(n/Min, "m")
	case n < Day:
		return p(n/Hr, "h")
	case n < 2*Week:
		return p(n/Day, "d")
	case n < Month:
		return p(n/Week, "w")
	case n < Year:
		return p(n/Month, "mo")
	default:
		return p(n/Year, "y")
	}
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"strconv"
	"strings"

	"github.com/gorilla/mux"
	_ "github.com/lib/pq" // _ means to import only for its side-effects (initialization)
)

// App : struct to expose references to the router and database
type App struct {
	Router *mux.Router
	DB     *sql.DB
}

// Initialize : func to initialize database with connection info, add routes
func (app *App) Initialize(user, password, dbname string) {
	connectionString := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)

	var err error
	app.DB, err = sql.Open("postgres", connectionString)
	if err != nil {
		log.Fatal(err)
	}

	app.Router = mux.NewRouter()
	app.initializeRoutes()
}

// Run : func to start the main application
func (app *App) Run(addr string) {
	log.Fatal(http.ListenAndServe(":8080", app.Router))
}

// initializeRoutes : func to initialize routes
func (app *App) initializeRoutes() {
	app.Router.HandleFunc("/", indexHandler).Methods("GET")
	app.Router.HandleFunc("/hello", helloHandler).Methods("GET")
	app.Router.HandleFunc("/users", app.getUsers).Methods("GET")
	app.Router.HandleFunc("/users", app.createUser).Methods("POST")
	app.Router.HandleFunc("/users/{id:[0-9]+}", app.getUser).Methods("GET")
	app.Router.HandleFunc("/users/{id:[0-9]+}", app.updateUser).Methods("PUT")
	app.Router.HandleFunc("/users/{id:[0-9]+}", app.deleteUser).Methods("DELETE")
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("templates/index.html")
	t.Execute(w, nil)
	log.Println("Log: indexHandler request")
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello Handler") // send data to client side

	// print the parsed form on server side
	r.ParseForm()                       // parse arguments, e.g. /hello/?url_long=111&url_long=222
	fmt.Println("path", r.URL.Path)     // `/hello/`
	fmt.Println("scheme", r.URL.Scheme) // scheme
	fmt.Println("method", r.Method)     // method GET
	fmt.Println(r.Form["url_long"])     // [111 222]
	for k, v := range r.Form {
		fmt.Println("key:", k)                    // key: url_long
		fmt.Println("val:", strings.Join(v, " ")) // val: 111 222
	}
	log.Println("Log: helloHandler request")
}

func (app *App) getUsers(w http.ResponseWriter, r *http.Request) {
	// Get a list of Users
	count, _ := strconv.Atoi(r.FormValue("count"))
	start, _ := strconv.Atoi(r.FormValue("start"))

	if count > 10 || count < 1 {
		count = 10
	}
	if start < 0 {
		start = 0
	}

	users, err := getUsers(app.DB, start, count)
	if err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusOK, users)
}

func (app *App) getUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid User ID")
		return
	}

	u := user{ID: id}
	if err := u.getUser(app.DB); err != nil {
		switch err {
		case sql.ErrNoRows:
			respondWithError(w, http.StatusNotFound, "User not found")
		default:
			respondWithError(w, http.StatusInternalServerError, err.Error())
		}
		return
	}
	respondWithJSON(w, http.StatusOK, u)
}

func (app *App) createUser(w http.ResponseWriter, r *http.Request) {
	var u user
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&u); err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid request payload")
		return
	}
	defer r.Body.Close()

	if err := u.createUser(app.DB); err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusCreated, u)
}

func (app *App) updateUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid User ID")
		return
	}

	var u user
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&u); err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid request payload")
		return
	}
	defer r.Body.Close()
	u.ID = id

	if err := u.updateUser(app.DB); err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusOK, u)
}

func (app *App) deleteUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid User ID")
		return
	}

	u := user{ID: id}
	if err := u.deleteUser(app.DB); err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusOK, map[string]string{"result": "success"})
}

func respondWithError(w http.ResponseWriter, code int, message string) {
	respondWithJSON(w, code, map[string]string{"error": message})
}

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
	response, _ := json.Marshal(payload)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	w.Write(response)
}
<commit_msg>App Run func uses addr given<commit_after>package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"strconv"
	"strings"

	"github.com/gorilla/mux"
	_ "github.com/lib/pq" // _ means to import only for its side-effects (initialization)
)

// App : struct to expose references to the router and database
type App struct {
	Router *mux.Router
	DB     *sql.DB
}

// Initialize : func to initialize database with connection info, add routes
func (app *App) Initialize(user, password, dbname string) {
	connectionString := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)

	var err error
	app.DB, err = sql.Open("postgres", connectionString)
	if err != nil {
		log.Fatal(err)
	}

	app.Router = mux.NewRouter()
	app.initializeRoutes()
}

// Run : func to start the main application at specifc port
func (app *App) Run(addr string) {
	log.Fatal(http.ListenAndServe(addr, app.Router))
}

// initializeRoutes : func to initialize routes
func (app *App) initializeRoutes() {
	app.Router.HandleFunc("/", indexHandler).Methods("GET")
	app.Router.HandleFunc("/hello", helloHandler).Methods("GET")
	app.Router.HandleFunc("/users", app.getUsers).Methods("GET")
	app.Router.HandleFunc("/users", app.createUser).Methods("POST")
	app.Router.HandleFunc("/users/{id:[0-9]+}", app.getUser).Methods("GET")
	app.Router.HandleFunc("/users/{id:[0-9]+}", app.updateUser).Methods("PUT")
	app.Router.HandleFunc("/users/{id:[0-9]+}", app.deleteUser).Methods("DELETE")
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("templates/index.html")
	t.Execute(w, nil)
	log.Println("Log: indexHandler request")
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello Handler") // send data to client side

	// print the parsed form on server side
	r.ParseForm()                       // parse arguments, e.g. /hello/?url_long=111&url_long=222
	fmt.Println("path", r.URL.Path)     // `/hello/`
	fmt.Println("scheme", r.URL.Scheme) // scheme
	fmt.Println("method", r.Method)     // method GET
	fmt.Println(r.Form["url_long"])     // [111 222]
	for k, v := range r.Form {
		fmt.Println("key:", k)                    // key: url_long
		fmt.Println("val:", strings.Join(v, " ")) // val: 111 222
	}
	log.Println("Log: helloHandler request")
}

func (app *App) getUsers(w http.ResponseWriter, r *http.Request) {
	// Get a list of Users
	count, _ := strconv.Atoi(r.FormValue("count"))
	start, _ := strconv.Atoi(r.FormValue("start"))

	if count > 10 || count < 1 {
		count = 10
	}
	if start < 0 {
		start = 0
	}

	users, err := getUsers(app.DB, start, count)
	if err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusOK, users)
}

func (app *App) getUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid User ID")
		return
	}

	u := user{ID: id}
	if err := u.getUser(app.DB); err != nil {
		switch err {
		case sql.ErrNoRows:
			respondWithError(w, http.StatusNotFound, "User not found")
		default:
			respondWithError(w, http.StatusInternalServerError, err.Error())
		}
		return
	}
	respondWithJSON(w, http.StatusOK, u)
}

func (app *App) createUser(w http.ResponseWriter, r *http.Request) {
	var u user
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&u); err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid request payload")
		return
	}
	defer r.Body.Close()

	if err := u.createUser(app.DB); err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusCreated, u)
}

func (app *App) updateUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid User ID")
		return
	}

	var u user
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&u); err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid request payload")
		return
	}
	defer r.Body.Close()
	u.ID = id

	if err := u.updateUser(app.DB); err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusOK, u)
}

func (app *App) deleteUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid User ID")
		return
	}

	u := user{ID: id}
	if err := u.deleteUser(app.DB); err != nil {
		respondWithError(w, http.StatusInternalServerError, err.Error())
		return
	}

	respondWithJSON(w, http.StatusOK, map[string]string{"result": "success"})
}

func respondWithError(w http.ResponseWriter, code int, message string) {
	respondWithJSON(w, code, map[string]string{"error": message})
}

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
	response, _ := json.Marshal(payload)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	w.Write(response)
}
<|endoftext|>


<commit_before>package gridt

import (
	"fmt"
	"testing"
)

const (
	logMsgf   = "\n%s: %s"
	fatalMsgf = logMsgf + "\ncolumns = %v\nlines = %v\nfit = %v"
)

var (
	randomLists = [][]string{
		{"a_value", "another_value", "lots_of_cells", "small_value", "biiiiiiiiiiiig_vaaaaaaaaaalue", "with spaces, it's better to read", "abc", "123", "baby_u_n_me", "bla bla blablablablab", "I see the endings, now", "nothing is broken"},
		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "b", "c"},
		{"a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "c"},
		{"a", "b", "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"},
		{"aaaaa", "b", "c", "d", "e", "f", "g", "h", "i"},
	}
)

func fixedList(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = "1234567890"
	}
	return s
}

func fmtMsg(i, len int, f bool) string {
	var list, fit string
	if !f {
		list = "n empty list"
		fit = "does not fit"
	} else {
		fit = "fits"
		list = fmt.Sprintf(" list with %d column(s)", len)
	}
	return fmt.Sprintf("#%d Should return a%s that %s", i, list, fit)
}

func TestFitIntoWidth(t *testing.T) {
	for i, c := range []struct {
		v []string  //=> cells' values
		m int       //=> maximum width
		d Direction //=> direction
		s string    //=> separator
		c int       //=> columns
		l int       //=> lines
		f bool      //=> if it fits
	}{
		// Empty lists.
		{fixedList(0), 10, TopToBottom, " ", 0, 0, false},
		{fixedList(0), 10, LeftToRight, " ", 0, 0, false},

		// Lists with one cell.
		{fixedList(1), 20, TopToBottom, " ", 1, 1, true},
		{fixedList(1), 5, TopToBottom, " ", 0, 0, false},
		{fixedList(1), 20, LeftToRight, " ", 1, 1, true},
		{fixedList(1), 5, LeftToRight, " ", 0, 0, false},

		// Lists with two cells.
		{fixedList(2), 30, TopToBottom, " ", 2, 1, true},
		{fixedList(2), 15, TopToBottom, " ", 1, 2, true},
		{fixedList(2), 5, TopToBottom, " ", 0, 0, false},
		{fixedList(2), 30, LeftToRight, " ", 2, 1, true},
		{fixedList(2), 15, LeftToRight, " ", 1, 2, true},
		{fixedList(2), 5, LeftToRight, " ", 0, 0, false},

		// Lists with three cells.
		{fixedList(3), 50, TopToBottom, " ", 3, 1, true},
		{fixedList(3), 30, TopToBottom, " ", 2, 2, true},
		{fixedList(3), 15, TopToBottom, " ", 1, 3, true},
		{fixedList(3), 5, TopToBottom, " ", 0, 0, false},
		{fixedList(3), 50, LeftToRight, " ", 3, 1, true},
		{fixedList(3), 30, LeftToRight, " ", 2, 2, true},
		{fixedList(3), 15, LeftToRight, " ", 1, 3, true},
		{fixedList(3), 5, LeftToRight, " ", 0, 0, false},

		// Random-sized lists.
		{randomLists[0], 72, TopToBottom, "  ", 3, 4, true},
		{randomLists[0], 72, LeftToRight, "  ", 2, 6, true},
		{randomLists[1], 100, TopToBottom, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[1], 100, LeftToRight, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[2], 100, TopToBottom, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[2], 100, LeftToRight, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[3], 100, TopToBottom, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[3], 100, LeftToRight, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[4], 9, TopToBottom, " ", 3, 3, true},
		{randomLists[4], 9, LeftToRight, " ", 3, 3, true},
	} {
		msg := fmtMsg(i, c.c, c.f)
		d, f := NewWithCells(c.d, c.s, c.v...).FitIntoWidth(c.m)
		if len(d.ws) != c.c || d.l != c.l || f != c.f {
			t.Fatalf(fatalMsgf, msg, ballotX, d.ws, d.l, f)
		}
		t.Logf(logMsgf, msg, checkMark)
	}
}
<commit_msg>Add tests for every property and method of Grid.<commit_after>package gridt

import (
	"fmt"
	"testing"
)

const (
	logMsgf   = "\n%s: %s"
	fatalMsgf = logMsgf + "\ncolumns = %v\nlines = %v\nfit = %v"
)

var (
	randomLists = [][]string{
		{"a_value", "another_value", "lots_of_cells", "small_value", "biiiiiiiiiiiig_vaaaaaaaaaalue", "with spaces, it's better to read", "abc", "123", "baby_u_n_me", "bla bla blablablablab", "I see the endings, now", "nothing is broken"},
		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "b", "c"},
		{"a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "c"},
		{"a", "b", "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"},
		{"aaaaa", "b", "c", "d", "e", "f", "g", "h", "i"},
	}
)

func fixedList(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = "1234567890"
	}
	return s
}

func fmtMsg(i, ii, len int, f bool) string {
	var list, fit string
	if !f {
		list = "n empty list"
		fit = "does not fit"
	} else {
		fit = "fits"
		list = fmt.Sprintf(" list with %d column(s)", len)
	}
	return fmt.Sprintf("#%d #%d Should return a%s that %s", i, ii, list, fit)
}

func TestFitIntoWidth(t *testing.T) {
	for i, c := range []struct {
		v []string  //=> cells' values
		m int       //=> maximum width
		d Direction //=> direction
		s string    //=> separator
		c int       //=> columns
		l int       //=> lines
		f bool      //=> if it fits
	}{
		// Empty lists.
		{fixedList(0), 10, TopToBottom, " ", 0, 0, false},
		{fixedList(0), 10, LeftToRight, " ", 0, 0, false},

		// Lists with one cell.
		{fixedList(1), 20, TopToBottom, " ", 1, 1, true},
		{fixedList(1), 5, TopToBottom, " ", 0, 0, false},
		{fixedList(1), 20, LeftToRight, " ", 1, 1, true},
		{fixedList(1), 5, LeftToRight, " ", 0, 0, false},

		// Invalid maximum size.
		{fixedList(1), 0, TopToBottom, " ", 0, 0, false},
		{fixedList(1), 0, LeftToRight, " ", 0, 0, false},

		// Lists with two cells.
		{fixedList(2), 30, TopToBottom, " ", 2, 1, true},
		{fixedList(2), 15, TopToBottom, " ", 1, 2, true},
		{fixedList(2), 5, TopToBottom, " ", 0, 0, false},
		{fixedList(2), 30, LeftToRight, " ", 2, 1, true},
		{fixedList(2), 15, LeftToRight, " ", 1, 2, true},
		{fixedList(2), 5, LeftToRight, " ", 0, 0, false},

		// Lists with three cells.
		{fixedList(3), 50, TopToBottom, " ", 3, 1, true},
		{fixedList(3), 30, TopToBottom, " ", 2, 2, true},
		{fixedList(3), 15, TopToBottom, " ", 1, 3, true},
		{fixedList(3), 5, TopToBottom, " ", 0, 0, false},
		{fixedList(3), 50, LeftToRight, " ", 3, 1, true},
		{fixedList(3), 30, LeftToRight, " ", 2, 2, true},
		{fixedList(3), 15, LeftToRight, " ", 1, 3, true},
		{fixedList(3), 5, LeftToRight, " ", 0, 0, false},

		// Random-sized lists.
		{randomLists[0], 72, TopToBottom, "  ", 3, 4, true},
		{randomLists[0], 72, LeftToRight, "  ", 2, 6, true},
		{randomLists[1], 100, TopToBottom, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[1], 100, LeftToRight, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[2], 100, TopToBottom, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[2], 100, LeftToRight, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[3], 100, TopToBottom, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[3], 100, LeftToRight, "^-._,^-._,^-._,^-._,^-._,^-._,^", 1, 3, true},
		{randomLists[4], 9, TopToBottom, " ", 3, 3, true},
		{randomLists[4], 9, LeftToRight, " ", 3, 3, true},
	} {
		gNewWithCells := NewWithCells(c.d, c.s, c.v...)
		gNewAdd := New(c.d, c.s).Add(c.v...)
		gNewInsertDelete := NewWithCells(c.d, c.s, c.v...)
		for i, v := range c.v {
			gNewInsertDelete.Delete(i)
			gNewInsertDelete.Insert(i, v)
		}
		for ii, g := range []*Grid{
			gNewWithCells,
			gNewAdd,
			gNewInsertDelete,
		} {
			msg := fmtMsg(i, ii, c.c, c.f)
			d, f := g.FitIntoWidth(c.m)
			cellsOk := len(c.v) == len(g.Cells())
			for i, cell := range g.Cells() {
				if c.v[i] != cell {
					cellsOk = false
					break
				}
			}
			if g.Direction() != c.d ||
				g.Separator() != c.s ||
				!cellsOk ||
				d.l != c.l ||
				f != c.f {
				t.Fatalf(fatalMsgf, msg, ballotX, d.ws, d.l, f)
			}
			t.Logf(logMsgf, msg, checkMark)
		}
	}
}
<|endoftext|>


<commit_before>
package gspt

import (
  "testing"
  "bytes"
  "strings"
  "time"

  "encoding/binary"
  "encoding/hex"

  "crypto/md5"
  "os/exec"
)

func randomMD5() string {
  str    := md5.New()
  random := new(bytes.Buffer)

  binary.Write(random, binary.LittleEndian, time.Now().UTC().UnixNano())

  str.Write(random.Bytes())

  return hex.EncodeToString(str.Sum(nil))
}

func TestSetProcTitle(t *testing.T) {
  title := randomMD5()

  SetProcTitle(title)

  out, err := exec.Command("/bin/ps", "a").Output()
  if err != nil {
    t.SkipNow()
  } else if !strings.Contains(string(out), title) {
    t.FailNow()
  }
}

<commit_msg>Added comment<commit_after>
package gspt

import (
  "testing"
  "bytes"
  "strings"
  "time"

  "encoding/binary"
  "encoding/hex"

  "crypto/md5"
  "os/exec"
)

func randomMD5() string {
  str    := md5.New()
  random := new(bytes.Buffer)

  binary.Write(random, binary.LittleEndian, time.Now().UTC().UnixNano())

  str.Write(random.Bytes())

  return hex.EncodeToString(str.Sum(nil))
}

func TestSetProcTitle(t *testing.T) {
  title := randomMD5()

  SetProcTitle(title)

  out, err := exec.Command("/bin/ps", "a").Output()
  if err != nil {
    // No ps available on this platform.
    t.SkipNow()
  } else if !strings.Contains(string(out), title) {
    t.FailNow()
  }
}

<|endoftext|>


<commit_before>package gop

import (
	"github.com/cactus/go-statsd-client/statsd"
	"os"
	"strings"
)

type StatsdClient struct {
	client *statsd.Client
	rate   float32
	app    *App
}

func (a *App) initStatsd() {
	statsdHostport, _ := a.Cfg.Get("gop", "statsd_hostport", "localhost:8125")
	hostname, _ := os.Hostname()
	statsdPrefix := strings.Join([]string{a.ProjectName, a.AppName, hostname}, ".")
	client, err := statsd.New(statsdHostport, statsdPrefix)
	if err != nil {
		// App will probably fall over due to nil client. That's OK.
		// We *could* panic below, but lets try and continue at least
		a.Error("Failed to create statsd client: " + err.Error())
		return
	}

	rate, _ := a.Cfg.GetFloat32("gop", "statsd_rate", 1.0)
	a.Stats = StatsdClient{
		client: client,
		rate:   rate,
		app:    a,
	}
}

func (s *StatsdClient) Dec(stat string, value int64) {
	s.app.Debug("STATSD DEC %s %d", stat, value)
	_ = s.client.Dec(stat, value, s.rate)
}

func (s *StatsdClient) Gauge(stat string, value int64) {
	s.app.Debug("STATSD GAUGE %s %d", stat, value)
	_ = s.client.Gauge(stat, value, s.rate)
}

func (s *StatsdClient) GaugeDelta(stat string, value int64) {
	s.app.Debug("STATSD GAUGEDELTA %s %d", stat, value)
	_ = s.client.GaugeDelta(stat, value, s.rate)
}

func (s *StatsdClient) Inc(stat string, value int64) {
	s.app.Debug("STATSD INC %s %d", stat, value)
	_ = s.client.Inc(stat, value, s.rate)
}

func (s *StatsdClient) Timing(stat string, delta int64) {
	s.app.Debug("STATSD TIMING %s %d", stat, delta)
	_ = s.client.Timing(stat, delta, s.rate)
}
<commit_msg>Make sure hostnames are not broken up<commit_after>package gop

import (
	"github.com/cactus/go-statsd-client/statsd"
	"os"
	"strings"
)

type StatsdClient struct {
	client *statsd.Client
	rate   float32
	app    *App
}

func (a *App) initStatsd() {
	statsdHostport, _ := a.Cfg.Get("gop", "statsd_hostport", "localhost:8125")
	hostname, _ := os.Hostname()
	statsdPrefix := strings.Join([]string{a.ProjectName, a.AppName, strings.Replace(hostname, ".", "_", 0)}, ".")
	client, err := statsd.New(statsdHostport, statsdPrefix)
	if err != nil {
		// App will probably fall over due to nil client. That's OK.
		// We *could* panic below, but lets try and continue at least
		a.Error("Failed to create statsd client: " + err.Error())
		return
	}

	rate, _ := a.Cfg.GetFloat32("gop", "statsd_rate", 1.0)
	a.Stats = StatsdClient{
		client: client,
		rate:   rate,
		app:    a,
	}
}

func (s *StatsdClient) Dec(stat string, value int64) {
	s.app.Debug("STATSD DEC %s %d", stat, value)
	_ = s.client.Dec(stat, value, s.rate)
}

func (s *StatsdClient) Gauge(stat string, value int64) {
	s.app.Debug("STATSD GAUGE %s %d", stat, value)
	_ = s.client.Gauge(stat, value, s.rate)
}

func (s *StatsdClient) GaugeDelta(stat string, value int64) {
	s.app.Debug("STATSD GAUGEDELTA %s %d", stat, value)
	_ = s.client.GaugeDelta(stat, value, s.rate)
}

func (s *StatsdClient) Inc(stat string, value int64) {
	s.app.Debug("STATSD INC %s %d", stat, value)
	_ = s.client.Inc(stat, value, s.rate)
}

func (s *StatsdClient) Timing(stat string, delta int64) {
	s.app.Debug("STATSD TIMING %s %d", stat, delta)
	_ = s.client.Timing(stat, delta, s.rate)
}
<|endoftext|>


<commit_before><commit_msg>refactor<commit_after><|endoftext|>


<commit_before><commit_msg>Add documentation of func<commit_after><|endoftext|>


<commit_before>package pagerduty

import (
	"fmt"
	"testing"

	"github.com/PagerDuty/go-pagerduty"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccPagerDutyUser_Basic(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckPagerDutyUserDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccCheckPagerDutyUserConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "foo@bar.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "color", "green"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "role", "user"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "job_title", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "description", "foo"),
				),
			},
			resource.TestStep{
				Config: testAccCheckPagerDutyUserConfigUpdated,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "bar"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "bar@foo.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "color", "red"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "role", "user"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "job_title", "bar"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "description", "bar"),
				),
			},
		},
	})
}

func testAccCheckPagerDutyUserDestroy(s *terraform.State) error {
	client := testAccProvider.Meta().(*pagerduty.Client)
	for _, r := range s.RootModule().Resources {
		if r.Type != "pagerduty_user" {
			continue
		}

		opts := pagerduty.GetUserOptions{}

		_, err := client.GetUser(r.Primary.ID, opts)

		if err == nil {
			return fmt.Errorf("User still exists")
		}

	}
	return nil
}

func testAccCheckPagerDutyUserExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		client := testAccProvider.Meta().(*pagerduty.Client)
		for _, r := range s.RootModule().Resources {
			opts := pagerduty.GetUserOptions{}
			if _, err := client.GetUser(r.Primary.ID, opts); err != nil {
				return fmt.Errorf("Received an error retrieving user %s ID: %s", err, r.Primary.ID)
			}
		}
		return nil
	}
}

const testAccCheckPagerDutyUserConfig = `
resource "pagerduty_user" "foo" {
  name        = "foo"
  email       = "foo@bar.com"
  color       = "green"
  role        = "user"
  job_title   = "foo"
  description = "foo"
}
`

const testAccCheckPagerDutyUserConfigUpdated = `
resource "pagerduty_user" "foo" {
  name        = "bar"
  email       = "bar@foo.com"
  color       = "red"
  role        = "user"
  job_title   = "bar"
  description = "bar"
}
`
<commit_msg>Add test case for user with teams<commit_after>package pagerduty

import (
	"fmt"
	"testing"

	"github.com/PagerDuty/go-pagerduty"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccPagerDutyUser_Basic(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckPagerDutyUserDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccCheckPagerDutyUserConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "foo@bar.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "color", "green"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "role", "user"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "job_title", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "description", "foo"),
				),
			},
			resource.TestStep{
				Config: testAccCheckPagerDutyUserConfigUpdated,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "bar"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "bar@foo.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "color", "red"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "role", "user"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "job_title", "bar"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "description", "bar"),
				),
			},
		},
	})
}

func TestAccPagerDutyUserWithTeams_Basic(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckPagerDutyUserDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccCheckPagerDutyUserWithTeamsConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "foo@bar.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "teams.#", "1"),
				),
			},
			resource.TestStep{
				Config: testAccCheckPagerDutyUserWithTeamsConfigUpdated,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "foo@bar.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "teams.#", "2"),
				),
			},
			resource.TestStep{
				Config: testAccCheckPagerDutyUserWithNoTeamsConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPagerDutyUserExists("pagerduty_user.foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "name", "foo"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "email", "foo@bar.com"),
					resource.TestCheckResourceAttr(
						"pagerduty_user.foo", "teams.#", "0"),
				),
			},
		},
	})
}

func testAccCheckPagerDutyUserDestroy(s *terraform.State) error {
	client := testAccProvider.Meta().(*pagerduty.Client)
	for _, r := range s.RootModule().Resources {
		if r.Type != "pagerduty_user" {
			continue
		}

		opts := pagerduty.GetUserOptions{}

		_, err := client.GetUser(r.Primary.ID, opts)

		if err == nil {
			return fmt.Errorf("User still exists")
		}

	}
	return nil
}

func testAccCheckPagerDutyUserExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}
		if rs.Primary.ID == "" {
			return fmt.Errorf("No user ID is set")
		}

		client := testAccProvider.Meta().(*pagerduty.Client)

		found, err := client.GetUser(rs.Primary.ID, pagerduty.GetUserOptions{})
		if err != nil {
			return err
		}

		if found.ID != rs.Primary.ID {
			return fmt.Errorf("User not found: %v - %v", rs.Primary.ID, found)
		}

		return nil
	}
}

const testAccCheckPagerDutyUserConfig = `
resource "pagerduty_user" "foo" {
  name        = "foo"
  email       = "foo@bar.com"
  color       = "green"
  role        = "user"
  job_title   = "foo"
  description = "foo"
}
`

const testAccCheckPagerDutyUserConfigUpdated = `
resource "pagerduty_user" "foo" {
  name        = "bar"
  email       = "bar@foo.com"
  color       = "red"
  role        = "user"
  job_title   = "bar"
  description = "bar"
}
`

const testAccCheckPagerDutyUserWithTeamsConfig = `
resource "pagerduty_team" "foo" {
  name = "Foo team"
}

resource "pagerduty_user" "foo" {
  name  = "foo"
  email = "foo@bar.com"
	teams = ["${pagerduty_team.foo.id}"]
}
`
const testAccCheckPagerDutyUserWithTeamsConfigUpdated = `
resource "pagerduty_team" "foo" {
  name = "Foo team"
}

resource "pagerduty_team" "bar" {
  name = "Bar team"
}

resource "pagerduty_user" "foo" {
  name  = "foo"
  email = "foo@bar.com"
	teams = ["${pagerduty_team.foo.id}", "${pagerduty_team.bar.id}"]
}
`

const testAccCheckPagerDutyUserWithNoTeamsConfig = `
resource "pagerduty_team" "foo" {
  name = "Foo team"
}

resource "pagerduty_team" "bar" {
  name = "Bar team"
}

resource "pagerduty_user" "foo" {
  name  = "foo"
  email = "foo@bar.com"
}
`
<|endoftext|>