<commit_before>package durafmt

import (
	"fmt"
	"time"
)

func HMS(duration time.Duration) string {

	h, m, s := extractValues(duration)

	hours := fmt.Sprintf("%0.2d", h)
	minutes := fmt.Sprintf("%0.2d", m)
	seconds := fmt.Sprintf("%0.2d", s)

	d := fmt.Sprintf("%s:%s:%s", hours, minutes, seconds)

	return d
}

func Words(duration time.Duration) string {

	h, m, s := extractValues(duration)

	hours := fmt.Sprintf("%d", h)
	minutes := fmt.Sprintf("%d", m)
	seconds := fmt.Sprintf("%d", s)

	d := fmt.Sprintf("%s hours %s minutes %s seconds", hours, minutes, seconds)

	return d
}

func extractValues(duration time.Duration) (int64, int64, int64) {
	var hours, minutes, seconds int64
	if duration.Hours() < 1 {
		hours = 0
	} else {
		hours = int64(duration.Hours())
	}

	if duration.Minutes() < 1 {
		minutes = 0
	} else {
		minutes = int64(duration.Minutes()) - hours*60
	}

	if duration.Seconds() < 1 {
		seconds = 0
	} else {
		seconds = int64(duration.Seconds()) - hours*60*60 - minutes*60
	}

	return hours, minutes, seconds
}
<commit_msg>only return non-zero segments<commit_after>package durafmt

import (
	"fmt"
	"time"
)

func HMS(duration time.Duration) string {

	h, m, s := extractValues(duration)

	hours := fmt.Sprintf("%0.2d", h)
	minutes := fmt.Sprintf("%0.2d", m)
	seconds := fmt.Sprintf("%0.2d", s)

	d := fmt.Sprintf("%s:%s:%s", hours, minutes, seconds)

	return d
}

func Words(duration time.Duration) string {

	var d string
	h, m, s := extractValues(duration)

	seconds := fmt.Sprintf("%d", s)
	d = fmt.Sprintf("%s seconds", seconds)

	if duration.Minutes() < 1 {
		return d
	}

	minutes := fmt.Sprintf("%d", m)
	d = fmt.Sprintf("%s minutes %s seconds", minutes, seconds)

	if duration.Hours() < 1 {
		return d
	}

	hours := fmt.Sprintf("%d", h)
	d = fmt.Sprintf("%s hours %s minutes %s seconds", hours, minutes, seconds)

	return d
}

func extractValues(duration time.Duration) (int64, int64, int64) {
	var hours, minutes, seconds int64
	if duration.Hours() < 1 {
		hours = 0
	} else {
		hours = int64(duration.Hours())
	}

	if duration.Minutes() < 1 {
		minutes = 0
	} else {
		minutes = int64(duration.Minutes()) - hours*60
	}

	if duration.Seconds() < 1 {
		seconds = 0
	} else {
		seconds = int64(duration.Seconds()) - hours*60*60 - minutes*60
	}

	return hours, minutes, seconds
}
<|endoftext|>


<commit_before>// Copyright 2017, TCN Inc.
// All rights reserved.

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:

//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of TCN Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package templates

const SpannerUnaryMethodTemplate = `{{define "spanner_unary_method"}}// spanner unary select {{.GetName}}
func (s* {{.GetServiceName}}Impl) {{.GetName}} (ctx context.Context, req *{{.GetInputType}}) (*{{.GetOutputType}}, error) {
{{if .Spanner.IsSelect}}{{template "spanner_unary_select" .}}{{end}}
{{if .Spanner.IsUpdate}}{{template "spanner_unary_update" .}}{{end}}
{{if .Spanner.IsInsert}}{{template "spanner_unary_insert" .}}{{end}}
{{if .Spanner.IsDelete}}{{template "spanner_unary_delete" .}}{{end}}
{{end}}`

const SpannerHelperTemplates = `
{{define "type_desc_to_def"}}
{{if .IsMapped}}
	//is mapped
	conv, err = {{.GoName}}{}.ToSpanner(req.{{.Name}}).Value()
	if err != nil {
		return nil, grpc.Errorf(codes.Unknown, err.Error())
	}
{{else}}
	// is not mapped
	conv = req.{{.Name}}
{{end}}{{end}}
`
const SpannerUnarySelectTemplate = `{{define "spanner_unary_select"}}
	var (
{{range $field, $type := .GetFieldsWithLocalTypesFor .GetOutputTypeStruct}}
		{{$field}} {{$type}}{{end}}
	)
	params := make(map[string]interface{})

	var conv string
	var err error
	//.GetSpannerSelectArgs
{{range $key, $val := .GetSpannerSelectArgs}}
{{if $val.IsFieldValue}}
	//if is.IsFieldValue
	{{template "type_desc_to_def" $val.Field}}
	params["{{$val.Name}}"] = conv
{{else}}
	//else
	//conv = { {$val.Value} }
	conv = {{$val.Value}}
	//params[{ {$val.Name} }] = conv
	params["{{$val.Name}}"] = conv
	{{end}}{{end}}

	//stmt := spanner.Statement{SQL: "{ {.Spanner.Query} }", Params: params}
	stmt := spanner.Statement{SQL: "{{.Spanner.Query}}", Params: params}
	tx := s.Client.Single()
	defer tx.Close()
	iter := tx.Query(ctx, stmt)
	rows := s.SRH.NewRowsFromIter(iter)
	rows.Next()
	if err = rows.Err(); err != nil {
		return nil, grpc.Errorf(codes.Unknown, err.Error())
	}

	//err = rows.Scan({ {range $index, $t := .GetTypeDescArrayForStruct .GetOutputTypeStruct} } &{ {$t.Name} },{ {end} })
	err = rows.Scan({{range $index, $t := .GetTypeDescArrayForStruct .GetOutputTypeStruct}} &{{$t.Name}},{{end}})
	if err == sql.ErrNoRows {
		return nil, grpc.Errorf(codes.NotFound, "%+v doesn't exist", req)
	} else if err != nil {
		return nil, grpc.Errorf(codes.Unknown, err.Error())
	}

	//res := &{ {.GetOutputType} }{
	res := &{{.GetOutputType}}{
	{{range $field, $type := .GetTypeDescForFieldsInStruct .GetOutputTypeStruct}}
	{{$field}}: {{template "addr" $type}}{{template "base" $type}}{{template "mapping" $type}},{{end}}
	}
	return res, nil
}
{{end}}`

const SpannerUnaryInsertTemplate = `{{define "spanner_unary_insert"}}
	params := []interface{}{
		{{range $index, $val := .GetSpannerInsertArgs}}
		{{$val}},{{end}}
	}
	muts := make([]*spanner.Mutation, 1)
	muts[0] = spanner.Insert("{{.Spanner.TableName}}", {{.Spanner.InsertColsAsString}}, params)
	_, err := s.SpannerDB.Apply(ctx, muts)
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			return nil, grpc.Errorf(codes.AlreadyExists, err.Error())
		} else {
			return nil, grpc.Errorf(codes.Unknown, err.Error())
		}
	}
	res := &{{.GetOutputType}}{}

	return res, nil
}
{{end}}`

const SpannerUnaryUpdateTemplate = `{{define "spanner_unary_update"}}
	params := map[string]interface{}{
		{{range $key, $val := .GetSpannerUpdateArgs "req"}}
		{{$key}}: {{$val}},\n{{end}}
	}
	muts := make([]*spanner.Mutation, 1)
	muts[0] = spanner.UpdateMap("{{.Spanner.TableName}}", params)
	_, err := s.SpannerDB.Apply(muts)
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			return grpc.Errorf(codes.AlreadyExists, err.Error())
		} else {
			return nil, grpc.Errorf(codes.Unknown, err.Error())
		}
	}
	res := &{{.GetOutputType}}{}

	return res, nil
}
{{end}}`

const SpannerUnaryDeleteTemplate = `{{define "spanner_unary_delete"}}
	key := {{.GetDeleteKeyRange}}
	muts := make([]*spanner.Mutation, 1)
	muts[0] = spanner.DeleteKeyRange("{{.Spanner.TableName}}", key)
	_, err := s.SpannerDB.Apply(muts)
	if err != nil {
		if strings.Contains(err.Error(), "does not exist") {
			return grpc.Errorf(codes.NotFound, err.Error())
		}
	}
{{end}}`

const SpannerClientStreamingMethodTemplate = `{{define "spanner_client_streaming_method"}}// spanner client streaming {{.GetName}}
func (s *{{.GetServiceName}}Impl) {{.GetName}}(stream {{.GetServiceName}}_{{.GetName}}Server) error {
	var totalAffected int64
	muts := make([]*spanner.Mutation, 0)
	for {
		req, err := stream.Recv()
		if err == io.EOF {
			break
		} else if err != nil {
			return grpc.Errorf(codes.Unknown, err.Error())
		}
		totalAffected += 1

		{{if .Spanner.IsInsert}}{{template "spanner_client_streaming_insert" .}}{{end}}
		{{if .Spanner.IsUpdate}}{{template "spanner_client_streaming_update" .}}{{end}}
		{{if .Spanner.IsDelete}}{{template "spanner_client_streaming_delete" .}}{{end}}
		//In the future, we might do apply if muts gets really big,  but for now,
		// we only do one apply on the database with all the records stored in muts
	}
	_, err := s.SpannerDB.Apply(muts)
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			return grpc.Errorf(codes.AlreadyExists, err.Error())
		} else {
			return nil, grpc.Errorf(codes.Unknown, err.Error())
		}
	}
	stream.SendAndClose(&{{.GetOutputType}}{Count: totalAffeted})
}
{{end}}`

const SpannerClientStreamingUpdateTemplate = `{{define "spanner_client_streaming_update"}}//spanner client streaming update
params := map[string]interface{}{
{{range $key, $val := .GetSpannerUpdateArgs}}
	{{$key}}: {{$val}},\n{{end}}
}
muts = append(muts, spanner.UpdateMap("{{.Spanner.TableName}}", params))
{{end}}`

const SpannerClientStreamingInsertTemplate = `{{define "spanner_client_streaming_insert"}}//spanner client streaming update
params := []interface{}{
{{range $index, $val := .GetSpannerInsertArgs}}
	{{$val}},\n{{end}}
}
muts = append(muts, spanner.Insert("{{.Spanner.TableName}}", {{.Spanner.InsertColsAsString}}, params))
{{end}}`

const SpannerClientStreamingDeleteTemplate = `{{define "spanner_client_streaming_delete"}}//spanner client streaming update
key := {{.GetDeleteKeyRange "req"}}
muts = append(muts, spanner.DeleteKeyRange("{{.Spanner.TableName}}", key)
{{end}}`

const SpannerServerStreamingMethodTemplate = `{{define "spanner_server_streaming_method"}}// spanner server streaming {{.GetName}}
func (s *{{.GetServiceName}}Impl) {{.GetName}}(req *{{.GetInputType}}, stream {{.GetServiceName}}_{{.GetName}}Server) error {
	var (
	{{range $field, $type := .GetFieldsWithLocalTypesFor .GetOutputTypeStruct}}
		{{$field}} {{$type}}{{end}}
	)
	params := make(map[string]interface{})

	var conv string
	var err error
	//.GetSpannerSelectArgs
{{range $key, $val := .GetSpannerSelectArgs}}
{{if $val.IsFieldValue}}
	//if is.IsFieldValue
	{{template "type_desc_to_def" $val.Field}}
	params["{{$val.Name}}"] = conv
{{else}}
	//else
	//conv = { {$val.Value} }
	conv = {{$val.Value}}
	//params["{ {$val.Name} }"] = conv
	params["{{$val.Name}}"] = conv
{{end}}{{end}}

	stmt := spanner.Statement{SQL: "{{.Spanner.Query}}", Params: params}
	tx := s.Client.Single()
	defer tx.Close()
	iter := tx.Query(context.Background(), stmt)
	rows := s.SRH.NewRowsFromIter(iter)
	for rows.Next() {
		if err := rows.Err(); err != nil {
			if err == sql.ErrNowRows {
				return grpc.Errorf(codes.NotFound, "%+v doesn't exist", req)
			}
			return grpc.Errorf(codes.Unknown, err.Error())
		}
		err := rows.Scan({{range $index, $t := .GetTypeDescArrayForStruct .GetOutputTypeStruct}} &{{$t.Name}},{{end}})
		if err != nil {
			return grpc.Errorf(codes.Unknown, err.Error())
		}
		res := &{{.GetOutputType}}{
		{{range $field, $type := .GetTypeDescForFieldsInStruct .GetOutputTypeStruct}}
		{{$field}}: {{template "addr" $type}}{{template "base" $type}}{{template "mapping" $type}},{{end}}
		}
		stream.Send(res)
	}
	return  nil
}
{{end}}`

const SpannerBidiStreamingMethodTemplate = `{{define "spanner_bidi_streaming_method"}}// spanner bidi streaming {{.GetName}} unimplemented{{end}}`
<commit_msg>use interface for interface value,  added return to template<commit_after>// Copyright 2017, TCN Inc.
// All rights reserved.

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:

//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of TCN Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package templates

const SpannerUnaryMethodTemplate = `{{define "spanner_unary_method"}}// spanner unary select {{.GetName}}
func (s* {{.GetServiceName}}Impl) {{.GetName}} (ctx context.Context, req *{{.GetInputType}}) (*{{.GetOutputType}}, error) {
{{if .Spanner.IsSelect}}{{template "spanner_unary_select" .}}{{end}}
{{if .Spanner.IsUpdate}}{{template "spanner_unary_update" .}}{{end}}
{{if .Spanner.IsInsert}}{{template "spanner_unary_insert" .}}{{end}}
{{if .Spanner.IsDelete}}{{template "spanner_unary_delete" .}}{{end}}
{{end}}`

const SpannerHelperTemplates = `
{{define "type_desc_to_def"}}
{{if .IsMapped}}
	//is mapped
	conv, err = {{.GoName}}{}.ToSpanner(req.{{.Name}}).Value()
	if err != nil {
		return nil, grpc.Errorf(codes.Unknown, err.Error())
	}
{{else}}
	// is not mapped
	conv = req.{{.Name}}
{{end}}{{end}}
`
const SpannerUnarySelectTemplate = `{{define "spanner_unary_select"}}
	var (
{{range $field, $type := .GetFieldsWithLocalTypesFor .GetOutputTypeStruct}}
		{{$field}} {{$type}}{{end}}
	)
	params := make(map[string]interface{})

	var conv interface{}
	var err error
	//.GetSpannerSelectArgs
{{range $key, $val := .GetSpannerSelectArgs}}
{{if $val.IsFieldValue}}
	//if is.IsFieldValue
	{{template "type_desc_to_def" $val.Field}}
	params["{{$val.Name}}"] = conv
{{else}}
	//else
	//conv = { {$val.Value} }
	conv = {{$val.Value}}
	//params[{ {$val.Name} }] = conv
	params["{{$val.Name}}"] = conv
	{{end}}{{end}}

	//stmt := spanner.Statement{SQL: "{ {.Spanner.Query} }", Params: params}
	stmt := spanner.Statement{SQL: "{{.Spanner.Query}}", Params: params}
	tx := s.Client.Single()
	defer tx.Close()
	iter := tx.Query(ctx, stmt)
	rows := s.SRH.NewRowsFromIter(iter)
	rows.Next()
	if err = rows.Err(); err != nil {
		return nil, grpc.Errorf(codes.Unknown, err.Error())
	}

	//err = rows.Scan({ {range $index, $t := .GetTypeDescArrayForStruct .GetOutputTypeStruct} } &{ {$t.Name} },{ {end} })
	err = rows.Scan({{range $index, $t := .GetTypeDescArrayForStruct .GetOutputTypeStruct}} &{{$t.Name}},{{end}})
	if err == sql.ErrNoRows {
		return nil, grpc.Errorf(codes.NotFound, "%+v doesn't exist", req)
	} else if err != nil {
		return nil, grpc.Errorf(codes.Unknown, err.Error())
	}

	//res := &{ {.GetOutputType} }{
	res := &{{.GetOutputType}}{
	{{range $field, $type := .GetTypeDescForFieldsInStruct .GetOutputTypeStruct}}
	{{$field}}: {{template "addr" $type}}{{template "base" $type}}{{template "mapping" $type}},{{end}}
	}
	return res, nil
}
{{end}}`

const SpannerUnaryInsertTemplate = `{{define "spanner_unary_insert"}}
	params := []interface{}{
		{{range $index, $val := .GetSpannerInsertArgs}}
		{{$val}},{{end}}
	}
	muts := make([]*spanner.Mutation, 1)
	muts[0] = spanner.Insert("{{.Spanner.TableName}}", {{.Spanner.InsertColsAsString}}, params)
	_, err := s.SpannerDB.Apply(ctx, muts)
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			return nil, grpc.Errorf(codes.AlreadyExists, err.Error())
		} else {
			return nil, grpc.Errorf(codes.Unknown, err.Error())
		}
	}
	res := &{{.GetOutputType}}{}

	return res, nil
}
{{end}}`

const SpannerUnaryUpdateTemplate = `{{define "spanner_unary_update"}}
	params := map[string]interface{}{
		{{range $key, $val := .GetSpannerUpdateArgs "req"}}
		{{$key}}: {{$val}},\n{{end}}
	}
	muts := make([]*spanner.Mutation, 1)
	muts[0] = spanner.UpdateMap("{{.Spanner.TableName}}", params)
	_, err := s.SpannerDB.Apply(muts)
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			return grpc.Errorf(codes.AlreadyExists, err.Error())
		} else {
			return nil, grpc.Errorf(codes.Unknown, err.Error())
		}
	}
	res := &{{.GetOutputType}}{}

	return res, nil
}
{{end}}`

const SpannerUnaryDeleteTemplate = `{{define "spanner_unary_delete"}}
	key := {{.GetDeleteKeyRange}}
	muts := make([]*spanner.Mutation, 1)
	muts[0] = spanner.DeleteKeyRange("{{.Spanner.TableName}}", key)
	_, err := s.SpannerDB.Apply(muts)
	if err != nil {
		if strings.Contains(err.Error(), "does not exist") {
			return grpc.Errorf(codes.NotFound, err.Error())
		}
	}
{{end}}`

const SpannerClientStreamingMethodTemplate = `{{define "spanner_client_streaming_method"}}// spanner client streaming {{.GetName}}
func (s *{{.GetServiceName}}Impl) {{.GetName}}(stream {{.GetServiceName}}_{{.GetName}}Server) error {
	var totalAffected int64
	muts := make([]*spanner.Mutation, 0)
	for {
		req, err := stream.Recv()
		if err == io.EOF {
			break
		} else if err != nil {
			return grpc.Errorf(codes.Unknown, err.Error())
		}
		totalAffected += 1

		{{if .Spanner.IsInsert}}{{template "spanner_client_streaming_insert" .}}{{end}}
		{{if .Spanner.IsUpdate}}{{template "spanner_client_streaming_update" .}}{{end}}
		{{if .Spanner.IsDelete}}{{template "spanner_client_streaming_delete" .}}{{end}}
		//In the future, we might do apply if muts gets really big,  but for now,
		// we only do one apply on the database with all the records stored in muts
	}
	_, err := s.SpannerDB.Apply(muts)
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			return grpc.Errorf(codes.AlreadyExists, err.Error())
		} else {
			return nil, grpc.Errorf(codes.Unknown, err.Error())
		}
	}
	stream.SendAndClose(&{{.GetOutputType}}{Count: totalAffeted})
	return nil
}
{{end}}`

const SpannerClientStreamingUpdateTemplate = `{{define "spanner_client_streaming_update"}}//spanner client streaming update
params := map[string]interface{}{
{{range $key, $val := .GetSpannerUpdateArgs}}
	{{$key}}: {{$val}},\n{{end}}
}
muts = append(muts, spanner.UpdateMap("{{.Spanner.TableName}}", params))
{{end}}`

const SpannerClientStreamingInsertTemplate = `{{define "spanner_client_streaming_insert"}}//spanner client streaming update
params := []interface{}{
{{range $index, $val := .GetSpannerInsertArgs}}
	{{$val}},\n{{end}}
}
muts = append(muts, spanner.Insert("{{.Spanner.TableName}}", {{.Spanner.InsertColsAsString}}, params))
{{end}}`

const SpannerClientStreamingDeleteTemplate = `{{define "spanner_client_streaming_delete"}}//spanner client streaming update
key := {{.GetDeleteKeyRange "req"}}
muts = append(muts, spanner.DeleteKeyRange("{{.Spanner.TableName}}", key)
{{end}}`

const SpannerServerStreamingMethodTemplate = `{{define "spanner_server_streaming_method"}}// spanner server streaming {{.GetName}}
func (s *{{.GetServiceName}}Impl) {{.GetName}}(req *{{.GetInputType}}, stream {{.GetServiceName}}_{{.GetName}}Server) error {
	var (
	{{range $field, $type := .GetFieldsWithLocalTypesFor .GetOutputTypeStruct}}
		{{$field}} {{$type}}{{end}}
	)
	params := make(map[string]interface{})

	var conv interface{}
	var err error
	//.GetSpannerSelectArgs
{{range $key, $val := .GetSpannerSelectArgs}}
{{if $val.IsFieldValue}}
	//if is.IsFieldValue
	{{template "type_desc_to_def" $val.Field}}
	params["{{$val.Name}}"] = conv
{{else}}
	//else
	//conv = { {$val.Value} }
	conv = {{$val.Value}}
	//params["{ {$val.Name} }"] = conv
	params["{{$val.Name}}"] = conv
{{end}}{{end}}

	stmt := spanner.Statement{SQL: "{{.Spanner.Query}}", Params: params}
	tx := s.Client.Single()
	defer tx.Close()
	iter := tx.Query(context.Background(), stmt)
	rows := s.SRH.NewRowsFromIter(iter)
	for rows.Next() {
		if err := rows.Err(); err != nil {
			if err == sql.ErrNowRows {
				return grpc.Errorf(codes.NotFound, "%+v doesn't exist", req)
			}
			return grpc.Errorf(codes.Unknown, err.Error())
		}
		err := rows.Scan({{range $index, $t := .GetTypeDescArrayForStruct .GetOutputTypeStruct}} &{{$t.Name}},{{end}})
		if err != nil {
			return grpc.Errorf(codes.Unknown, err.Error())
		}
		res := &{{.GetOutputType}}{
		{{range $field, $type := .GetTypeDescForFieldsInStruct .GetOutputTypeStruct}}
		{{$field}}: {{template "addr" $type}}{{template "base" $type}}{{template "mapping" $type}},{{end}}
		}
		stream.Send(res)
	}
	return  nil
}
{{end}}`

const SpannerBidiStreamingMethodTemplate = `{{define "spanner_bidi_streaming_method"}}// spanner bidi streaming {{.GetName}} unimplemented{{end}}`
<|endoftext|>


<commit_before>package common

import "github.com/cihangir/schema"

type Req struct {
	Schema  *schema.Schema
	Context *Context
}

type Res struct {
	Output []Output
}

type Generator interface {
	Generate(*Req, *Res) error
}
<commit_msg>gene/common: add string version of schema<commit_after>package common

import "github.com/cihangir/schema"

type Req struct {
	Schema    *schema.Schema
	SchemaStr string
	Context   *Context
}

type Res struct {
	Output []Output
}

type Generator interface {
	Generate(*Req, *Res) error
}
<|endoftext|>


<commit_before>package main

import (
	cnt "github.com/orian/counters/global"
	"net/http"
	"time"
)

func main() {
	cnt.GetCounter("start").Increment()
	http.Handle("/status", cnt.CreateHttpHandler())
	go func() {
		c := time.Tick(1 * time.Second)
		for range c {
			cnt.GetCounter("ticker").Increment()
		}
	}()
	cnt.GetMax("monist").Set(128)
	http.ListenAndServe(":8080", nil)
}
<commit_msg>Add signal handling to example<commit_after>package main

import (
	cnt "github.com/orian/counters/global"
	"flag"
	"net/http"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	FLAG_signal := flag.Bool("signal", false, "handle SIGINT and SIGTERM")
	flag.Parse()
	if *FLAG_signal {
		sigs := make(chan os.Signal, 1)
		signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
		go func() {
			lastInt := time.Now()
			for sig := range sigs {
				fmt.Printf("Got signal: %s(%d)", sig, sig)
				fmt.Printf("I am: %d", os.Getpid())
				fmt.Printf(counters.String())
				l := time.Now()
				if sig == syscall.SIGTERM || l.Sub(lastInt).Seconds() < 1. {
					os.Exit(0)
				}
				lastInt = l
			}
		}()
	}

	cnt.GetCounter("start").Increment()
	http.Handle("/status", cnt.CreateHttpHandler())
	go func() {
		c := time.Tick(1 * time.Second)
		for range c {
			cnt.GetCounter("ticker").Increment()
		}
	}()
	cnt.GetMax("monist").Set(128)
	http.ListenAndServe(":8080", nil)
}
<|endoftext|>


<commit_before>package "example"

import (
  "fmt"
  "github.com/adabei/goldenbot/rcon"
)

type Example struct {
  requests chan rcon.RCONRequest
}

func NewExample(requests chan rcon.RCONRequest) *Example {
  e := new(Example)
  e.requests = requests
  return e
}

func (e *Example) Start (next, prev chan string) {
  for {
    // Every plugin has to pass on messages to the next
    in := <-prev
    next <- in

    // Here we will print all received messages to Stdout
    fmt.Println(in)
  }
}
<commit_msg>adds example plugin<commit_after>package example

import (
  "fmt"
  "github.com/adabei/goldenbot/rcon"
)

type Example struct {
  requests chan rcon.RCONRequest
}

func NewExample(requests chan rcon.RCONRequest) *Example {
  e := new(Example)
  e.requests = requests
  return e
}

func (e *Example) Start (next, prev chan string) {
  for {
    // Every plugin has to pass on messages to the next
    in := <-prev
    next <- in

    // Here we will print all received messages to Stdout
    fmt.Println(in)
  }
}
<|endoftext|>


<commit_before>/*                          _       _
 *__      _____  __ ___   ___  __ _| |_ ___
 *\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
 * \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
 *  \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
 *
 * Copyright © 2016 Weaviate. All rights reserved.
 * LICENSE: https://github.com/weaviate/weaviate/blob/master/LICENSE
 * AUTHOR: Bob van Luijt (bob@weaviate.com)
 * See www.weaviate.com for details
 * Contact: @weaviate_iot / yourfriends@weaviate.com
 */

package datastore

import (
	"context"
	"errors"
	"fmt"
	"log"
	"net"

	"cloud.google.com/go/datastore"
	gouuid "github.com/satori/go.uuid"

	"encoding/json"

	"github.com/weaviate/weaviate/connectors/utils"
)

// Datastore has some basic variables.
type Datastore struct {
	client *datastore.Client
}

// SetConfig is used to fill in a struct with config variables
func (f *Datastore) SetConfig(configInput interface{}) {
	// NOTHING HERE
}

// GetName returns a unique connector name
func (f *Datastore) GetName() string {
	return "datastore"
}

// Connect to datastore
func (f *Datastore) Connect() error {
	// Set ctx, your Google Cloud Platform project ID and kind.
	ctx := context.Background()
	projectID := "weaviate-dev-001"

	// Create new client
	client, err := datastore.NewClient(ctx, projectID)

	// If error, return it. Otherwise set client.
	if err != nil {
		return err
	}

	f.client = client
	return nil
}

// Creates a root key and tables if not already available
func (f *Datastore) Init() error {

	ctx := context.Background()

	kind := "weaviate_users"

	// create query to check for root key
	query := datastore.NewQuery(kind).Filter("Parent =", "*").Limit(1)

	dbKeyObjects := []connector_utils.DatabaseUsersObject{}

	_, err := f.client.GetAll(ctx, query, &dbKeyObjects)

	if err != nil {
		panic("ERROR INITIALIZING SERVER")
	}

	// No key was found, create one
	if len(dbKeyObjects) == 0 {

		dbObject := connector_utils.DatabaseUsersObject{}

		// Create key token
		dbObject.KeyToken = fmt.Sprintf("%v", gouuid.NewV4())

		// Uuid + name
		uuid := fmt.Sprintf("%v", gouuid.NewV4())

		// Creates a Key instance.
		taskKey := datastore.NameKey(kind, uuid, nil)

		// Auto set the parent ID to root *
		dbObject.Parent = "*"

		// Set Uuid
		dbObject.Uuid = uuid

		// Set chmod variables
		dbObjectObject := connector_utils.DatabaseUsersObjectsObject{}
		dbObjectObject.Read = true
		dbObjectObject.Write = true
		dbObjectObject.Delete = true

		// Get ips as v6
		var ips []string
		ifaces, _ := net.Interfaces()
		for _, i := range ifaces {
			addrs, _ := i.Addrs()
			for _, addr := range addrs {
				var ip net.IP
				switch v := addr.(type) {
				case *net.IPNet:
					ip = v.IP
				case *net.IPAddr:
					ip = v.IP
				}

				ipv6 := ip.To16()
				ips = append(ips, ipv6.String())
			}
		}

		dbObjectObject.IpOrigin = ips

		// Marshall and add to object
		dbObjectObjectJson, _ := json.Marshal(dbObjectObject)
		dbObject.Object = string(dbObjectObjectJson)

		// Saves the new entity.
		if _, err := f.client.Put(ctx, taskKey, &dbObject); err != nil {
			log.Fatalf("Failed to save task: %v", err)
		}

		// Print the key
		log.Println("INFO: No root key was found, a new root key is created. More info: https://github.com/weaviate/weaviate/blob/develop/README.md#authentication")
		log.Println("INFO: Auto set allowed IPs to: ", ips)
		log.Println("ROOTKEY=" + dbObject.KeyToken)
	}

	return nil
}

// Add item to DB
func (f *Datastore) Add(dbObject connector_utils.DatabaseObject) (string, error) {
	// Move all other objects to history
	f.MoveToHistory(dbObject.Uuid)

	// Add item to Datastore
	newUUID, _ := f.AddByKind(dbObject, "weaviate")

	// Return the ID that is used to create.
	return newUUID, nil
}

// AddHistory adds an item to the history kind
func (f *Datastore) MoveToHistory(UUIDToMove string) (bool, error) {
	// Set ctx and kind.
	ctx := context.Background()

	// Make list query with all items
	query := datastore.NewQuery("weaviate").Filter("Uuid =", UUIDToMove)

	// Fill object with results
	dbObjectsToMove := connector_utils.DatabaseObjects{}
	keys, err := f.client.GetAll(ctx, query, &dbObjectsToMove)

	for index, dbObjectToMove := range dbObjectsToMove {
		// Add item to Datastore
		if _, errAdd := f.AddByKind(dbObjectToMove, "weaviate_history"); errAdd != nil {
			log.Fatalf("Failed to add history task: %v", errAdd)
		}

		// Deletes the old entity.
		if err := f.client.Delete(ctx, keys[index]); err != nil {
			log.Fatalf("Failed to delete task: %v", err)
		}
	}

	// Return true
	return true, err
}

// AddByKind adds using a kind
func (f *Datastore) AddByKind(dbObject connector_utils.DatabaseObject, kind string) (string, error) {
	// Set ctx and kind.
	ctx := context.Background()

	// Generate an UUID
	nameUUID := fmt.Sprintf("%v", gouuid.NewV4())

	// Creates a Key instance.
	taskKey := datastore.NameKey(kind, nameUUID, nil)

	// Saves the new entity.
	if _, err := f.client.Put(ctx, taskKey, &dbObject); err != nil {
		log.Fatalf("Failed to save task: %v", err)
		return "Error", err
	}

	// Return the ID that is used to create.
	return dbObject.Uuid, nil
}

// Get DatabaseObject from DB by uuid
func (f *Datastore) Get(uuid string) (connector_utils.DatabaseObject, error) {
	// Set ctx and kind.
	ctx := context.Background()
	kind := "weaviate"

	// Make get Query
	query := datastore.NewQuery(kind).Filter("Uuid =", uuid).Order("-CreateTimeMs").Limit(1)

	// Fill object
	object := connector_utils.DatabaseObjects{}
	keys, err := f.client.GetAll(ctx, query, &object)

	// Return error
	if err != nil {
		log.Fatalf("Failed to load task: %v", err)
		return connector_utils.DatabaseObject{}, err
	}

	// Return error 'not found'
	if len(keys) == 0 {
		notFoundErr := errors.New("no object with such UUID found")
		return connector_utils.DatabaseObject{}, notFoundErr
	}

	// Return found object
	return object[0], nil
}

// List lists the items from Datastore by refType and limit
func (f *Datastore) List(refType string, limit int, page int, referenceFilter *connector_utils.ObjectReferences) (connector_utils.DatabaseObjects, int64, error) {
	// Set ctx and kind.
	ctx := context.Background()
	kind := "weaviate"

	// Calculate offset
	offset := (page - 1) * limit

	// Make list queries
	query := datastore.NewQuery(kind).Filter("RefType =", refType).Filter("Deleted =", false).Order("-CreateTimeMs")

	// Add more to queries for reference filters
	if referenceFilter != nil {
		if referenceFilter.ThingID != "" {
			query = query.Filter("RelatedObjects.ThingID = ", string(referenceFilter.ThingID))
		}
	}

	// Make total results query
	totalResultsQuery := query

	// finish query
	query = query.Limit(limit).Offset(offset)

	// Fill object with results
	dbObjects := connector_utils.DatabaseObjects{}
	_, err := f.client.GetAll(ctx, query, &dbObjects)
	totalResults, errTotal := f.client.Count(ctx, totalResultsQuery)

	// Return error and empty object
	if err != nil || errTotal != nil {
		log.Fatalf("Failed to load task: %v", err)

		return connector_utils.DatabaseObjects{}, 0, err
	}

	// Return list with objects
	return dbObjects, int64(totalResults), nil
}

// Validate if a user has access, returns permissions object
func (f *Datastore) ValidateKey(token string) ([]connector_utils.DatabaseUsersObject, error) {

	ctx := context.Background()

	kind := "weaviate_users"

	query := datastore.NewQuery(kind).Filter("KeyToken =", token).Limit(1)

	dbUsersObjects := []connector_utils.DatabaseUsersObject{}

	_, err := f.client.GetAll(ctx, query, &dbUsersObjects)

	if err != nil {
		return dbUsersObjects, err
	}

	// keys are found, return them
	return dbUsersObjects, nil
}

// AddUser to DB
func (f *Datastore) AddKey(parentUuid string, dbObject connector_utils.DatabaseUsersObject) (connector_utils.DatabaseUsersObject, error) {
	ctx := context.Background()

	kind := "weaviate_users"

	nameUUID := fmt.Sprintf("%v", gouuid.NewV4())

	// Create key token
	dbObject.KeyToken = fmt.Sprintf("%v", gouuid.NewV4())

	// Creates a Key instance.
	taskKey := datastore.NameKey(kind, nameUUID, nil)

	// Auto set the parent ID
	dbObject.Parent = parentUuid

	// Saves the new entity.
	if _, err := f.client.Put(ctx, taskKey, &dbObject); err != nil {
		log.Fatalf("Failed to save task: %v", err)
		return dbObject, err
	}

	// Return the ID that is used to create.
	return dbObject, nil
}
<commit_msg>gh-109: Show error when connection init wont work.<commit_after>/*                          _       _
 *__      _____  __ ___   ___  __ _| |_ ___
 *\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
 * \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
 *  \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
 *
 * Copyright © 2016 Weaviate. All rights reserved.
 * LICENSE: https://github.com/weaviate/weaviate/blob/master/LICENSE
 * AUTHOR: Bob van Luijt (bob@weaviate.com)
 * See www.weaviate.com for details
 * Contact: @weaviate_iot / yourfriends@weaviate.com
 */

package datastore

import (
	"context"
	"errors"
	"fmt"
	"log"
	"net"

	"cloud.google.com/go/datastore"
	gouuid "github.com/satori/go.uuid"

	"encoding/json"

	"github.com/weaviate/weaviate/connectors/utils"
)

// Datastore has some basic variables.
type Datastore struct {
	client *datastore.Client
}

// SetConfig is used to fill in a struct with config variables
func (f *Datastore) SetConfig(configInput interface{}) {
	// NOTHING HERE
}

// GetName returns a unique connector name
func (f *Datastore) GetName() string {
	return "datastore"
}

// Connect to datastore
func (f *Datastore) Connect() error {
	// Set ctx, your Google Cloud Platform project ID and kind.
	ctx := context.Background()
	projectID := "weaviate-dev-001"

	// Create new client
	client, err := datastore.NewClient(ctx, projectID)

	// If error, return it. Otherwise set client.
	if err != nil {
		return err
	}

	f.client = client
	return nil
}

// Creates a root key and tables if not already available
func (f *Datastore) Init() error {

	ctx := context.Background()

	kind := "weaviate_users"

	// create query to check for root key
	query := datastore.NewQuery(kind).Filter("Parent =", "*").Limit(1)

	dbKeyObjects := []connector_utils.DatabaseUsersObject{}

	_, err := f.client.GetAll(ctx, query, &dbKeyObjects)

	if err != nil {
		panic("ERROR INITIALIZING SERVER: " + err.Error())
	}

	// No key was found, create one
	if len(dbKeyObjects) == 0 {

		dbObject := connector_utils.DatabaseUsersObject{}

		// Create key token
		dbObject.KeyToken = fmt.Sprintf("%v", gouuid.NewV4())

		// Uuid + name
		uuid := fmt.Sprintf("%v", gouuid.NewV4())

		// Creates a Key instance.
		taskKey := datastore.NameKey(kind, uuid, nil)

		// Auto set the parent ID to root *
		dbObject.Parent = "*"

		// Set Uuid
		dbObject.Uuid = uuid

		// Set chmod variables
		dbObjectObject := connector_utils.DatabaseUsersObjectsObject{}
		dbObjectObject.Read = true
		dbObjectObject.Write = true
		dbObjectObject.Delete = true

		// Get ips as v6
		var ips []string
		ifaces, _ := net.Interfaces()
		for _, i := range ifaces {
			addrs, _ := i.Addrs()
			for _, addr := range addrs {
				var ip net.IP
				switch v := addr.(type) {
				case *net.IPNet:
					ip = v.IP
				case *net.IPAddr:
					ip = v.IP
				}

				ipv6 := ip.To16()
				ips = append(ips, ipv6.String())
			}
		}

		dbObjectObject.IpOrigin = ips

		// Marshall and add to object
		dbObjectObjectJson, _ := json.Marshal(dbObjectObject)
		dbObject.Object = string(dbObjectObjectJson)

		// Saves the new entity.
		if _, err := f.client.Put(ctx, taskKey, &dbObject); err != nil {
			log.Fatalf("Failed to save task: %v", err)
		}

		// Print the key
		log.Println("INFO: No root key was found, a new root key is created. More info: https://github.com/weaviate/weaviate/blob/develop/README.md#authentication")
		log.Println("INFO: Auto set allowed IPs to: ", ips)
		log.Println("ROOTKEY=" + dbObject.KeyToken)
	}

	return nil
}

// Add item to DB
func (f *Datastore) Add(dbObject connector_utils.DatabaseObject) (string, error) {
	// Move all other objects to history
	f.MoveToHistory(dbObject.Uuid)

	// Add item to Datastore
	newUUID, _ := f.AddByKind(dbObject, "weaviate")

	// Return the ID that is used to create.
	return newUUID, nil
}

// AddHistory adds an item to the history kind
func (f *Datastore) MoveToHistory(UUIDToMove string) (bool, error) {
	// Set ctx and kind.
	ctx := context.Background()

	// Make list query with all items
	query := datastore.NewQuery("weaviate").Filter("Uuid =", UUIDToMove)

	// Fill object with results
	dbObjectsToMove := connector_utils.DatabaseObjects{}
	keys, err := f.client.GetAll(ctx, query, &dbObjectsToMove)

	for index, dbObjectToMove := range dbObjectsToMove {
		// Add item to Datastore
		if _, errAdd := f.AddByKind(dbObjectToMove, "weaviate_history"); errAdd != nil {
			log.Fatalf("Failed to add history task: %v", errAdd)
		}

		// Deletes the old entity.
		if err := f.client.Delete(ctx, keys[index]); err != nil {
			log.Fatalf("Failed to delete task: %v", err)
		}
	}

	// Return true
	return true, err
}

// AddByKind adds using a kind
func (f *Datastore) AddByKind(dbObject connector_utils.DatabaseObject, kind string) (string, error) {
	// Set ctx and kind.
	ctx := context.Background()

	// Generate an UUID
	nameUUID := fmt.Sprintf("%v", gouuid.NewV4())

	// Creates a Key instance.
	taskKey := datastore.NameKey(kind, nameUUID, nil)

	// Saves the new entity.
	if _, err := f.client.Put(ctx, taskKey, &dbObject); err != nil {
		log.Fatalf("Failed to save task: %v", err)
		return "Error", err
	}

	// Return the ID that is used to create.
	return dbObject.Uuid, nil
}

// Get DatabaseObject from DB by uuid
func (f *Datastore) Get(uuid string) (connector_utils.DatabaseObject, error) {
	// Set ctx and kind.
	ctx := context.Background()
	kind := "weaviate"

	// Make get Query
	query := datastore.NewQuery(kind).Filter("Uuid =", uuid).Order("-CreateTimeMs").Limit(1)

	// Fill object
	object := connector_utils.DatabaseObjects{}
	keys, err := f.client.GetAll(ctx, query, &object)

	// Return error
	if err != nil {
		log.Fatalf("Failed to load task: %v", err)
		return connector_utils.DatabaseObject{}, err
	}

	// Return error 'not found'
	if len(keys) == 0 {
		notFoundErr := errors.New("no object with such UUID found")
		return connector_utils.DatabaseObject{}, notFoundErr
	}

	// Return found object
	return object[0], nil
}

// List lists the items from Datastore by refType and limit
func (f *Datastore) List(refType string, limit int, page int, referenceFilter *connector_utils.ObjectReferences) (connector_utils.DatabaseObjects, int64, error) {
	// Set ctx and kind.
	ctx := context.Background()
	kind := "weaviate"

	// Calculate offset
	offset := (page - 1) * limit

	// Make list queries
	query := datastore.NewQuery(kind).Filter("RefType =", refType).Filter("Deleted =", false).Order("-CreateTimeMs")

	// Add more to queries for reference filters
	if referenceFilter != nil {
		if referenceFilter.ThingID != "" {
			query = query.Filter("RelatedObjects.ThingID = ", string(referenceFilter.ThingID))
		}
	}

	// Make total results query
	totalResultsQuery := query

	// finish query
	query = query.Limit(limit).Offset(offset)

	// Fill object with results
	dbObjects := connector_utils.DatabaseObjects{}
	_, err := f.client.GetAll(ctx, query, &dbObjects)
	totalResults, errTotal := f.client.Count(ctx, totalResultsQuery)

	// Return error and empty object
	if err != nil || errTotal != nil {
		log.Fatalf("Failed to load task: %v", err)

		return connector_utils.DatabaseObjects{}, 0, err
	}

	// Return list with objects
	return dbObjects, int64(totalResults), nil
}

// Validate if a user has access, returns permissions object
func (f *Datastore) ValidateKey(token string) ([]connector_utils.DatabaseUsersObject, error) {

	ctx := context.Background()

	kind := "weaviate_users"

	query := datastore.NewQuery(kind).Filter("KeyToken =", token).Limit(1)

	dbUsersObjects := []connector_utils.DatabaseUsersObject{}

	_, err := f.client.GetAll(ctx, query, &dbUsersObjects)

	if err != nil {
		return dbUsersObjects, err
	}

	// keys are found, return them
	return dbUsersObjects, nil
}

// AddUser to DB
func (f *Datastore) AddKey(parentUuid string, dbObject connector_utils.DatabaseUsersObject) (connector_utils.DatabaseUsersObject, error) {
	ctx := context.Background()

	kind := "weaviate_users"

	nameUUID := fmt.Sprintf("%v", gouuid.NewV4())

	// Create key token
	dbObject.KeyToken = fmt.Sprintf("%v", gouuid.NewV4())

	// Creates a Key instance.
	taskKey := datastore.NameKey(kind, nameUUID, nil)

	// Auto set the parent ID
	dbObject.Parent = parentUuid

	// Saves the new entity.
	if _, err := f.client.Put(ctx, taskKey, &dbObject); err != nil {
		log.Fatalf("Failed to save task: %v", err)
		return dbObject, err
	}

	// Return the ID that is used to create.
	return dbObject, nil
}
<|endoftext|>


<commit_before>// Package twse - Fetch stock data from http://www.twse.com.tw/
// 擷取台灣股市上市股票資訊
//
package twse

import (
	"encoding/csv"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/toomore/gogrs/utils"
)

type unixMapData map[int64][][]string

// Data start with stock no, date.
type Data struct {
	No          string
	Date        time.Time
	RawData     [][]string
	UnixMapData unixMapData
	openList    []float64
	priceList   []float64
	rangeList   []float64
	volumeList  []uint64
}

// URL return stock csv url path.
func (d Data) URL() string {
	path := fmt.Sprintf(utils.TWSECSV, d.Date.Year(), d.Date.Month(), d.Date.Year(), d.Date.Month(), d.No, utils.RandInt())
	return fmt.Sprintf("%s%s", utils.TWSEHOST, path)
}

// Round will do sub one month.
func (d *Data) Round() {
	year, month, _ := d.Date.Date()
	d.Date = time.Date(year, month-1, 1, 0, 0, 0, 0, time.UTC)
}

// PlusData will do Round() and Get().
func (d *Data) PlusData() {
	d.Round()
	d.Get()
}

func (d *Data) clearCache() {
	d.rangeList = nil
	d.openList = nil
	d.priceList = nil
	d.volumeList = nil
}

// Get return csv data in array.
func (d *Data) Get() ([][]string, error) {
	if d.UnixMapData == nil {
		d.UnixMapData = make(unixMapData)
	}
	if len(d.UnixMapData[d.Date.Unix()]) == 0 {
		csvFiles, err := http.Get(d.URL())
		if err != nil {
			return nil, fmt.Errorf("Network fail: %s", err)
		}
		defer csvFiles.Body.Close()
		data, _ := ioutil.ReadAll(csvFiles.Body)
		csvArrayContent := strings.Split(string(data), "\n")
		for i := range csvArrayContent {
			csvArrayContent[i] = strings.TrimSpace(csvArrayContent[i])
		}
		if len(csvArrayContent) > 2 {
			csvReader := csv.NewReader(strings.NewReader(strings.Join(csvArrayContent[2:], "\n")))
			allData, err := csvReader.ReadAll()
			d.RawData = append(allData, d.RawData...)
			d.UnixMapData[d.Date.Unix()] = allData
			d.clearCache()
			return allData, err
		}
		return nil, errors.New("Not enough data.")
	}
	return d.UnixMapData[d.Date.Unix()], nil
}

// GetByTimeMap return a map by key of time.Time
func (d Data) GetByTimeMap() map[time.Time]interface{} {
	data := make(map[time.Time]interface{})
	dailyData, _ := d.Get()
	for _, v := range dailyData {
		data[utils.ParseDate(v[0])] = v
	}
	return data
}

func (d Data) getColsList(colsNo int) []string {
	var result []string
	result = make([]string, len(d.RawData))
	for i, value := range d.RawData {
		result[i] = value[colsNo]
	}
	return result
}

func (d Data) getColsListFloat64(colsNo int) []float64 {
	var result []float64
	result = make([]float64, len(d.RawData))
	for i, v := range d.getColsList(colsNo) {
		result[i], _ = strconv.ParseFloat(v, 64)
	}
	return result
}

// GetVolumeList 取得 成交股數 序列
func (d *Data) GetVolumeList() []uint64 {
	if d.volumeList == nil {
		var result []uint64
		result = make([]uint64, len(d.RawData))
		for i, v := range d.getColsList(1) {
			result[i], _ = strconv.ParseUint(strings.Replace(v, ",", "", -1), 10, 64)
		}
		d.volumeList = result
	}
	return d.volumeList
}

// GetOpenList 取得 開盤價 序列
func (d *Data) GetOpenList() []float64 {
	if d.openList == nil {
		d.openList = d.getColsListFloat64(3)
	}
	return d.openList
}

// GetPriceList 取得 收盤價 序列
func (d *Data) GetPriceList() []float64 {
	if d.priceList == nil {
		d.priceList = d.getColsListFloat64(6)
	}
	return d.priceList
}

// GetRangeList 取得 漲跌價差 序列
func (d *Data) GetRangeList() []float64 {
	if d.rangeList == nil {
		d.rangeList = d.getColsListFloat64(7)
	}
	return d.rangeList
}

// MA 計算 收盤價 的移動平均
func (d Data) MA(days int) []float64 {
	var result []float64
	var priceList = d.GetPriceList()
	result = make([]float64, len(priceList)-days+1)
	for i := range priceList[days-1:] {
		result[i] = utils.AvgFlast64(priceList[i : i+days])
	}
	return result
}

// MAV 計算 成交股數 的移動平均
func (d Data) MAV(days int) []uint64 {
	var result []uint64
	var volumeList = d.GetVolumeList()
	result = make([]uint64, len(volumeList)-days+1)
	for i := range volumeList[days-1:] {
		result[i] = utils.AvgUint64(volumeList[i : i+days])
	}
	return result
}

// IsRed 計算是否收紅 K
func (d Data) IsRed() bool {
	var rangeList = d.GetRangeList()
	return rangeList[len(rangeList)-1] > 0
}

// FmtData is struct for daily data format.
type FmtData struct {
	Date       time.Time
	Volume     uint64  //成交股數
	TotalPrice uint64  //成交金額
	Open       float64 //開盤價
	High       float64 //最高價
	Low        float64 //最低價
	Price      float64 //收盤價
	Range      float64 //漲跌價差
	Totalsale  uint64  //成交筆數
}

// FormatData is format daily data.
func (d Data) FormatData() []FmtData {
	result := make([]FmtData, len(d.RawData))
	var loopd FmtData
	for i, v := range d.RawData {
		loopd.Date = utils.ParseDate(v[0])

		volume, _ := strconv.ParseUint(strings.Replace(v[1], ",", "", -1), 10, 32)
		loopd.Volume = volume

		totalprice, _ := strconv.ParseUint(strings.Replace(v[2], ",", "", -1), 10, 32)
		loopd.TotalPrice = totalprice

		open, _ := strconv.ParseFloat(v[3], 64)
		loopd.Open = open

		high, _ := strconv.ParseFloat(v[4], 64)
		loopd.High = high

		low, _ := strconv.ParseFloat(v[5], 64)
		loopd.Low = low

		price, _ := strconv.ParseFloat(v[6], 64)
		loopd.Price = price

		rangeData, _ := strconv.ParseFloat(v[7], 64)
		loopd.Range = rangeData

		totalsale, _ := strconv.ParseUint(strings.Replace(v[8], ",", "", -1), 10, 64)
		loopd.Totalsale = totalsale

		result[i] = loopd
	}
	return result
}
<commit_msg>Add otc into URL.<commit_after>// Package twse - Fetch stock data from http://www.twse.com.tw/
// 擷取台灣股市上市股票資訊
//
package twse

import (
	"encoding/csv"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/toomore/gogrs/utils"
)

type unixMapData map[int64][][]string

// Data start with stock no, date.
type Data struct {
	No          string
	Date        time.Time
	RawData     [][]string
	UnixMapData unixMapData
	exchange    string
	openList    []float64
	priceList   []float64
	rangeList   []float64
	volumeList  []uint64
}

// URL return stock csv url path.
func (d Data) URL() string {
	var path string
	var host string

	if d.exchange == "tse" {
		path = fmt.Sprintf(utils.TWSECSV, d.Date.Year(), d.Date.Month(), d.Date.Year(), d.Date.Month(), d.No, utils.RandInt())
		host = utils.TWSEHOST
	} else if d.exchange == "otc" {
		path = fmt.Sprintf(utils.OTCCSV, d.Date.Year(), d.Date.Month(), d.No, utils.RandInt())
		host = utils.OTCHOST
	}

	return fmt.Sprintf("%s%s", host, path)
}

// Round will do sub one month.
func (d *Data) Round() {
	year, month, _ := d.Date.Date()
	d.Date = time.Date(year, month-1, 1, 0, 0, 0, 0, time.UTC)
}

// PlusData will do Round() and Get().
func (d *Data) PlusData() {
	d.Round()
	d.Get()
}

func (d *Data) clearCache() {
	d.rangeList = nil
	d.openList = nil
	d.priceList = nil
	d.volumeList = nil
}

// Get return csv data in array.
func (d *Data) Get() ([][]string, error) {
	if d.UnixMapData == nil {
		d.UnixMapData = make(unixMapData)
	}
	if len(d.UnixMapData[d.Date.Unix()]) == 0 {
		csvFiles, err := http.Get(d.URL())
		if err != nil {
			return nil, fmt.Errorf("Network fail: %s", err)
		}
		defer csvFiles.Body.Close()
		data, _ := ioutil.ReadAll(csvFiles.Body)
		csvArrayContent := strings.Split(string(data), "\n")
		for i := range csvArrayContent {
			csvArrayContent[i] = strings.TrimSpace(csvArrayContent[i])
		}
		if len(csvArrayContent) > 2 {
			csvReader := csv.NewReader(strings.NewReader(strings.Join(csvArrayContent[2:], "\n")))
			allData, err := csvReader.ReadAll()
			d.RawData = append(allData, d.RawData...)
			d.UnixMapData[d.Date.Unix()] = allData
			d.clearCache()
			return allData, err
		}
		return nil, errors.New("Not enough data.")
	}
	return d.UnixMapData[d.Date.Unix()], nil
}

// GetByTimeMap return a map by key of time.Time
func (d Data) GetByTimeMap() map[time.Time]interface{} {
	data := make(map[time.Time]interface{})
	dailyData, _ := d.Get()
	for _, v := range dailyData {
		data[utils.ParseDate(v[0])] = v
	}
	return data
}

func (d Data) getColsList(colsNo int) []string {
	var result []string
	result = make([]string, len(d.RawData))
	for i, value := range d.RawData {
		result[i] = value[colsNo]
	}
	return result
}

func (d Data) getColsListFloat64(colsNo int) []float64 {
	var result []float64
	result = make([]float64, len(d.RawData))
	for i, v := range d.getColsList(colsNo) {
		result[i], _ = strconv.ParseFloat(v, 64)
	}
	return result
}

// GetVolumeList 取得 成交股數 序列
func (d *Data) GetVolumeList() []uint64 {
	if d.volumeList == nil {
		var result []uint64
		result = make([]uint64, len(d.RawData))
		for i, v := range d.getColsList(1) {
			result[i], _ = strconv.ParseUint(strings.Replace(v, ",", "", -1), 10, 64)
		}
		d.volumeList = result
	}
	return d.volumeList
}

// GetOpenList 取得 開盤價 序列
func (d *Data) GetOpenList() []float64 {
	if d.openList == nil {
		d.openList = d.getColsListFloat64(3)
	}
	return d.openList
}

// GetPriceList 取得 收盤價 序列
func (d *Data) GetPriceList() []float64 {
	if d.priceList == nil {
		d.priceList = d.getColsListFloat64(6)
	}
	return d.priceList
}

// GetRangeList 取得 漲跌價差 序列
func (d *Data) GetRangeList() []float64 {
	if d.rangeList == nil {
		d.rangeList = d.getColsListFloat64(7)
	}
	return d.rangeList
}

// MA 計算 收盤價 的移動平均
func (d Data) MA(days int) []float64 {
	var result []float64
	var priceList = d.GetPriceList()
	result = make([]float64, len(priceList)-days+1)
	for i := range priceList[days-1:] {
		result[i] = utils.AvgFlast64(priceList[i : i+days])
	}
	return result
}

// MAV 計算 成交股數 的移動平均
func (d Data) MAV(days int) []uint64 {
	var result []uint64
	var volumeList = d.GetVolumeList()
	result = make([]uint64, len(volumeList)-days+1)
	for i := range volumeList[days-1:] {
		result[i] = utils.AvgUint64(volumeList[i : i+days])
	}
	return result
}

// IsRed 計算是否收紅 K
func (d Data) IsRed() bool {
	var rangeList = d.GetRangeList()
	return rangeList[len(rangeList)-1] > 0
}

// FmtData is struct for daily data format.
type FmtData struct {
	Date       time.Time
	Volume     uint64  //成交股數
	TotalPrice uint64  //成交金額
	Open       float64 //開盤價
	High       float64 //最高價
	Low        float64 //最低價
	Price      float64 //收盤價
	Range      float64 //漲跌價差
	Totalsale  uint64  //成交筆數
}

// FormatData is format daily data.
func (d Data) FormatData() []FmtData {
	result := make([]FmtData, len(d.RawData))
	var loopd FmtData
	for i, v := range d.RawData {
		loopd.Date = utils.ParseDate(v[0])

		volume, _ := strconv.ParseUint(strings.Replace(v[1], ",", "", -1), 10, 32)
		loopd.Volume = volume

		totalprice, _ := strconv.ParseUint(strings.Replace(v[2], ",", "", -1), 10, 32)
		loopd.TotalPrice = totalprice

		open, _ := strconv.ParseFloat(v[3], 64)
		loopd.Open = open

		high, _ := strconv.ParseFloat(v[4], 64)
		loopd.High = high

		low, _ := strconv.ParseFloat(v[5], 64)
		loopd.Low = low

		price, _ := strconv.ParseFloat(v[6], 64)
		loopd.Price = price

		rangeData, _ := strconv.ParseFloat(v[7], 64)
		loopd.Range = rangeData

		totalsale, _ := strconv.ParseUint(strings.Replace(v[8], ",", "", -1), 10, 64)
		loopd.Totalsale = totalsale

		result[i] = loopd
	}
	return result
}
<|endoftext|>


<commit_before>package docker

import "fmt"

// list of valid os/arch values (see "Optional Environment Variables" section
// of https://golang.org/doc/install/source
// Added linux/s390x as we know System z support already exists

var validOSArch = map[string]bool{
	"darwin/386":      true,
	"darwin/amd64":    true,
	"darwin/arm":      true,
	"darwin/arm64":    true,
	"dragonfly/amd64": true,
	"freebsd/386":     true,
	"freebsd/amd64":   true,
	"freebsd/arm":     true,
	"linux/386":       true,
	"linux/amd64":     true,
	"linux/arm":       true,
	"linux/arm64":     true,
	"linux/ppc64":     true,
	"linux/ppc64le":   true,
	"linux/mips64":    true,
	"linux/mips64le":  true,
	"linux/s390x":     true,
	"netbsd/386":      true,
	"netbsd/amd64":    true,
	"netbsd/arm":      true,
	"openbsd/386":     true,
	"openbsd/amd64":   true,
	"openbsd/arm":     true,
	"plan9/386":       true,
	"plan9/amd64":     true,
	"solaris/amd64":   true,
	"windows/386":     true,
	"windows/amd64":   true,
}

func isValidOSArch(os string, arch string) bool {
	osarch := fmt.Sprintf("%s/%s", os, arch)
	_, ok := validOSArch[osarch]
	return ok
}
<commit_msg>Add windows/arm to the validOSArch map<commit_after>package docker

import "fmt"

// list of valid os/arch values (see "Optional Environment Variables" section
// of https://golang.org/doc/install/source
// Added linux/s390x as we know System z support already exists

var validOSArch = map[string]bool{
	"darwin/386":      true,
	"darwin/amd64":    true,
	"darwin/arm":      true,
	"darwin/arm64":    true,
	"dragonfly/amd64": true,
	"freebsd/386":     true,
	"freebsd/amd64":   true,
	"freebsd/arm":     true,
	"linux/386":       true,
	"linux/amd64":     true,
	"linux/arm":       true,
	"linux/arm64":     true,
	"linux/ppc64":     true,
	"linux/ppc64le":   true,
	"linux/mips64":    true,
	"linux/mips64le":  true,
	"linux/s390x":     true,
	"netbsd/386":      true,
	"netbsd/amd64":    true,
	"netbsd/arm":      true,
	"openbsd/386":     true,
	"openbsd/amd64":   true,
	"openbsd/arm":     true,
	"plan9/386":       true,
	"plan9/amd64":     true,
	"solaris/amd64":   true,
	"windows/386":     true,
	"windows/amd64":   true,
	"windows/arm":     true,
}

func isValidOSArch(os string, arch string) bool {
	osarch := fmt.Sprintf("%s/%s", os, arch)
	_, ok := validOSArch[osarch]
	return ok
}
<|endoftext|>


<commit_before>// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zk

import (
	"fmt"
	"log"
	"math/rand"
	"os"
	"path"
	"sort"
	"strings"
	"sync"
	"time"

	"launchpad.net/gozk/zookeeper"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

// Create a path and any pieces required, think mkdir -p.
// Intermediate znodes are always created empty.
func CreateRecursive(zconn Conn, zkPath, value string, flags int, aclv []zookeeper.ACL) (pathCreated string, err error) {
	parts := strings.Split(zkPath, "/")
	if parts[1] != "zk" {
		return "", fmt.Errorf("zkutil: non zk path: %v", zkPath)
	}

	pathCreated, err = zconn.Create(zkPath, value, flags, aclv)
	if zookeeper.IsError(err, zookeeper.ZNONODE) {
		_, err = CreateRecursive(zconn, path.Dir(zkPath), "", flags, aclv)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			return "", err
		}
		pathCreated, err = zconn.Create(zkPath, value, flags, aclv)
	}
	return
}

func CreateOrUpdate(zconn Conn, zkPath, value string, flags int, aclv []zookeeper.ACL, recursive bool) (pathCreated string, err error) {
	if recursive {
		pathCreated, err = CreateRecursive(zconn, zkPath, value, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	} else {
		pathCreated, err = zconn.Create(zkPath, value, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	}
	if err != nil && zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
		pathCreated = ""
		_, err = zconn.Set(zkPath, value, -1)
	}
	return
}

type pathItem struct {
	path string
	err  error
}

func ChildrenRecursive(zconn Conn, zkPath string) ([]string, error) {
	var err error
	mutex := sync.Mutex{}
	wg := sync.WaitGroup{}
	pathList := make([]string, 0, 32)
	children, _, err := zconn.Children(zkPath)
	if err != nil {
		return nil, err
	}

	for _, child := range children {
		wg.Add(1)
		go func(child string) {
			childPath := path.Join(zkPath, child)
			rChildren, zkErr := ChildrenRecursive(zconn, childPath)
			if zkErr != nil {
				// If other processes are deleting nodes, we need to ignore
				// the missing nodes.
				if !zookeeper.IsError(zkErr, zookeeper.ZNONODE) {
					mutex.Lock()
					err = zkErr
					mutex.Unlock()
				}
			} else {
				mutex.Lock()
				pathList = append(pathList, child)
				for _, rChild := range rChildren {
					pathList = append(pathList, path.Join(child, rChild))
				}
				mutex.Unlock()
			}
			wg.Done()
		}(child)
	}

	wg.Wait()

	mutex.Lock()
	defer mutex.Unlock()
	if err != nil {
		return nil, err
	}
	return pathList, nil
}

// resolve paths like:
// /zk/nyc/vt/tablets/*/action
// /zk/global/vt/keyspaces/*/shards/*/action
// /zk/*/vt/tablets/*/action
// into real existing paths
//
// If you send paths that don't contain any wildcard and
// don't exist, this function will return an empty array.
func ResolveWildcards(zconn Conn, zkPaths []string) ([]string, error) {
	// check all the paths start with /zk/ before doing anything
	// time consuming
	for _, zkPath := range zkPaths {

		if _, err := ZkCellFromZkPath(zkPath); err != nil {
			return nil, err
		}
	}

	result := make([]string, 0, 32)

	for _, zkPath := range zkPaths {
		parts := strings.Split(zkPath, "/")
		subResult, err := resolveRecursive(zconn, parts, true)
		if err != nil {
			return nil, err
		}
		result = append(result, subResult...)
	}

	return result, nil
}

// checks if a string has a wildcard in it. In the cases we detect a bad
// pattern, we return 'true', and let the path.Match function find it.
func hasWildcard(path string) bool {
	for i := 0; i < len(path); i++ {
		switch path[i] {
		case '\\':
			if i+1 >= len(path) {
				return true
			} else {
				i++
			}
		case '*', '?', '[':
			return true
		}
	}
	return false
}

func resolveRecursive(zconn Conn, parts []string, toplevel bool) (result []string, err error) {
	for i, part := range parts {
		if hasWildcard(part) {
			var children []string
			if i == 2 {
				children = ZkKnownCells(false)
			} else {
				zkParentPath := strings.Join(parts[:i], "/")
				children, _, err = zconn.Children(zkParentPath)
				if err != nil {
					// we asked for something like
					// /zk/cell/aaa/* and
					// /zk/cell/aaa doesn't exist
					// -> return empty list, no error
					// (note we check both a regular zk
					// error and the error the test
					// produces)
					if zookeeper.IsError(err, zookeeper.ZNONODE) {
						return nil, nil
					}
					// otherwise we return the error
					return
				}
			}
			sort.Strings(children)

			result = make([]string, 0, 32)
			for _, child := range children {
				matched, err := path.Match(part, child)
				if err != nil {
					return nil, err
				}
				if matched {
					// we have a match!
					newParts := make([]string, len(parts))
					copy(newParts, parts)
					newParts[i] = child
					subResult, err := resolveRecursive(zconn, newParts, false)
					if err != nil {
						return nil, err
					}
					result = append(result, subResult...)
				}
			}

			// we found a part that is a wildcard, we
			// added the children already, we're done
			return result, nil
		}
	}

	// no part contains a wildcard, add the path if it exists, and done
	path := strings.Join(parts, "/")
	if toplevel {
		// for whatever the user typed at the toplevel, we don't
		// check it exists or not, we just return it
		return []string{path}, nil
	}

	// this is an expanded path, we need to check if it exists
	stat, err := zconn.Exists(path)
	if err != nil {
		return nil, err
	}
	if stat != nil {
		return []string{path}, nil
	}
	return nil, nil
}

func DeleteRecursive(zconn Conn, zkPath string, version int) error {
	// version: -1 delete any version of the node at path - only applies to the top node
	err := zconn.Delete(zkPath, version)
	if err == nil {
		return nil
	}
	if !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
		return err
	}
	// Remove the ability for other nodes to get created while we are trying to delete.
	// Otherwise, you can enter a race condition, or get starved out from deleting.
	err = zconn.SetACL(zkPath, zookeeper.WorldACL(zookeeper.PERM_ADMIN|zookeeper.PERM_DELETE|zookeeper.PERM_READ), version)
	if err != nil {
		return err
	}
	children, _, err := zconn.Children(zkPath)
	if err != nil {
		return err
	}
	for _, child := range children {
		err := DeleteRecursive(zconn, path.Join(zkPath, child), -1)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return fmt.Errorf("zkutil: recursive delete failed: %v", err)
		}
	}

	err = zconn.Delete(zkPath, version)
	if err != nil && !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
		err = fmt.Errorf("zkutil: nodes getting recreated underneath delete (app race condition): %v", zkPath)
	}
	return err
}

// The lexically lowest node is the lock holder - verify that this
// path holds the lock.  Call this queue-lock because the semantics are
// a hybrid.  Normal zookeeper locks make assumptions about sequential
// numbering that don't hold when the data in a lock is modified.
// if the provided 'interrupted' chan is closed, we'll just stop waiting
// and return an interruption error
func ObtainQueueLock(zconn Conn, zkPath string, wait time.Duration, interrupted chan struct{}) error {
	queueNode := path.Dir(zkPath)
	lockNode := path.Base(zkPath)

	timer := time.NewTimer(wait)
trylock:
	children, _, err := zconn.Children(queueNode)
	if err != nil {
		return fmt.Errorf("zkutil: trylock failed %v", err)
	}
	sort.Strings(children)
	if len(children) > 0 {
		if children[0] == lockNode {
			return nil
		}
		if wait > 0 {
			prevLock := ""
			for i := 1; i < len(children); i++ {
				if children[i] == lockNode {
					prevLock = children[i-1]
					break
				}
			}
			if prevLock == "" {
				return fmt.Errorf("zkutil: no previous queue node found: %v", zkPath)
			}

			zkPrevLock := path.Join(queueNode, prevLock)
			stat, watch, err := zconn.ExistsW(zkPrevLock)
			if err != nil {
				return fmt.Errorf("zkutil: unable to watch queued node %v %v", zkPrevLock, err)
			}
			if stat == nil {
				goto trylock
			}
			select {
			case <-timer.C:
				break
			case <-interrupted:
				return fmt.Errorf("zkutil: obtaining lock was interrupted %v", zkPath)
			case <-watch:
				// The precise event doesn't matter - try to read again regardless.
				goto trylock
			}
		}
		return fmt.Errorf("zkutil: obtaining lock timed out %v", zkPath)
	}
	return fmt.Errorf("zkutil: empty queue node: %v", queueNode)
}

// Close done when you want to exit cleanly.
func CreatePidNode(zconn Conn, zkPath string, done chan struct{}) error {
	hostname, err := os.Hostname()
	if err != nil {
		return fmt.Errorf("zkutil: failed creating pid node %v: %v", zkPath, err)
	}
	data := fmt.Sprintf("host:%v\npid:%v\n", hostname, os.Getpid())

	// On the first try, assume the cluster is up and running, that will
	// help hunt down any config issues present at startup
	_, err = zconn.Create(zkPath, data, zookeeper.EPHEMERAL, zookeeper.WorldACL(zookeeper.PERM_ALL))
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			err = zconn.Delete(zkPath, -1)
		}
		if err != nil {
			return fmt.Errorf("zkutil: failed deleting pid node: %v: %v", zkPath, err)
		}
		_, err = zconn.Create(zkPath, data, zookeeper.EPHEMERAL, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil {
			return fmt.Errorf("zkutil: failed creating pid node: %v: %v", zkPath, err)
		}
	}

	go func() {
		for {
			_, _, watch, err := zconn.GetW(zkPath)
			if err != nil {
				if zookeeper.IsError(err, zookeeper.ZNONODE) {
					_, err = zconn.Create(zkPath, data, zookeeper.EPHEMERAL, zookeeper.WorldACL(zookeeper.PERM_ALL))
					if err == nil {
						log.Printf("WARNING: failed recreating pid node: %v: %v", zkPath, err)
					} else {
						log.Printf("INFO: recreated pid node: %v", zkPath)
						continue
					}
				} else {
					log.Printf("WARNING: failed reading pid node: %v", err)
				}
			} else {
				select {
				case event := <-watch:
					if event.Ok() && event.Type == zookeeper.EVENT_DELETED {
						// Most likely another process has started up. However,
						// there is a chance that an ephemeral node is deleted by
						// the session expiring, yet that same session gets a watch
						// notification. This seems like buggy behavior, but rather
						// than race too hard on the node, just wait a bit and see
						// if the situation resolves itself.
						log.Printf("WARNING: pid deleted: %v", zkPath)
					} else {
						log.Printf("INFO: pid node event: %v", event)
					}
					// break here and wait for a bit before attempting
				case <-done:
					log.Printf("INFO: pid watcher stopped on done: %v", zkPath)
					return
				}
			}
			select {
			// No one likes a thundering herd, least of all zookeeper.
			case <-time.After(5*time.Second + time.Duration(rand.Int63n(55e9))):
			case <-done:
				log.Printf("INFO: pid watcher stopped on done: %v", zkPath)
				return
			}
		}
	}()

	return nil
}
<commit_msg>Parallelizing wildcards. 20x faster.<commit_after>// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zk

import (
	"fmt"
	"log"
	"math/rand"
	"os"
	"path"
	"sort"
	"strings"
	"sync"
	"time"

	"launchpad.net/gozk/zookeeper"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

// Create a path and any pieces required, think mkdir -p.
// Intermediate znodes are always created empty.
func CreateRecursive(zconn Conn, zkPath, value string, flags int, aclv []zookeeper.ACL) (pathCreated string, err error) {
	parts := strings.Split(zkPath, "/")
	if parts[1] != "zk" {
		return "", fmt.Errorf("zkutil: non zk path: %v", zkPath)
	}

	pathCreated, err = zconn.Create(zkPath, value, flags, aclv)
	if zookeeper.IsError(err, zookeeper.ZNONODE) {
		_, err = CreateRecursive(zconn, path.Dir(zkPath), "", flags, aclv)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			return "", err
		}
		pathCreated, err = zconn.Create(zkPath, value, flags, aclv)
	}
	return
}

func CreateOrUpdate(zconn Conn, zkPath, value string, flags int, aclv []zookeeper.ACL, recursive bool) (pathCreated string, err error) {
	if recursive {
		pathCreated, err = CreateRecursive(zconn, zkPath, value, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	} else {
		pathCreated, err = zconn.Create(zkPath, value, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	}
	if err != nil && zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
		pathCreated = ""
		_, err = zconn.Set(zkPath, value, -1)
	}
	return
}

type pathItem struct {
	path string
	err  error
}

func ChildrenRecursive(zconn Conn, zkPath string) ([]string, error) {
	var err error
	mutex := sync.Mutex{}
	wg := sync.WaitGroup{}
	pathList := make([]string, 0, 32)
	children, _, err := zconn.Children(zkPath)
	if err != nil {
		return nil, err
	}

	for _, child := range children {
		wg.Add(1)
		go func(child string) {
			childPath := path.Join(zkPath, child)
			rChildren, zkErr := ChildrenRecursive(zconn, childPath)
			if zkErr != nil {
				// If other processes are deleting nodes, we need to ignore
				// the missing nodes.
				if !zookeeper.IsError(zkErr, zookeeper.ZNONODE) {
					mutex.Lock()
					err = zkErr
					mutex.Unlock()
				}
			} else {
				mutex.Lock()
				pathList = append(pathList, child)
				for _, rChild := range rChildren {
					pathList = append(pathList, path.Join(child, rChild))
				}
				mutex.Unlock()
			}
			wg.Done()
		}(child)
	}

	wg.Wait()

	mutex.Lock()
	defer mutex.Unlock()
	if err != nil {
		return nil, err
	}
	return pathList, nil
}

// resolve paths like:
// /zk/nyc/vt/tablets/*/action
// /zk/global/vt/keyspaces/*/shards/*/action
// /zk/*/vt/tablets/*/action
// into real existing paths
//
// If you send paths that don't contain any wildcard and
// don't exist, this function will return an empty array.
func ResolveWildcards(zconn Conn, zkPaths []string) ([]string, error) {
	// check all the paths start with /zk/ before doing anything
	// time consuming
	for _, zkPath := range zkPaths {

		if _, err := ZkCellFromZkPath(zkPath); err != nil {
			return nil, err
		}
	}

	results := make([][]string, len(zkPaths))
	wg := &sync.WaitGroup{}
	mu := &sync.Mutex{}
	var firstError error

	for i, zkPath := range zkPaths {
		wg.Add(1)
		parts := strings.Split(zkPath, "/")
		go func(i int) {
			defer wg.Done()
			subResult, err := resolveRecursive(zconn, parts, true)
			if err != nil {
				mu.Lock()
				if firstError != nil {
					log.Printf("Multiple error: %v", err)
				} else {
					firstError = err
				}
				mu.Unlock()
			} else {
				results[i] = subResult
			}
		}(i)
	}

	wg.Wait()
	if firstError != nil {
		return nil, firstError
	}

	result := make([]string, 0, 32)
	for i := 0; i < len(zkPaths); i++ {
		subResult := results[i]
		if subResult != nil {
			result = append(result, subResult...)
		}
	}

	return result, nil
}

// checks if a string has a wildcard in it. In the cases we detect a bad
// pattern, we return 'true', and let the path.Match function find it.
func hasWildcard(path string) bool {
	for i := 0; i < len(path); i++ {
		switch path[i] {
		case '\\':
			if i+1 >= len(path) {
				return true
			} else {
				i++
			}
		case '*', '?', '[':
			return true
		}
	}
	return false
}

func resolveRecursive(zconn Conn, parts []string, toplevel bool) ([]string, error) {
	for i, part := range parts {
		if hasWildcard(part) {
			var children []string
			if i == 2 {
				children = ZkKnownCells(false)
			} else {
				zkParentPath := strings.Join(parts[:i], "/")
				var err error
				children, _, err = zconn.Children(zkParentPath)
				if err != nil {
					// we asked for something like
					// /zk/cell/aaa/* and
					// /zk/cell/aaa doesn't exist
					// -> return empty list, no error
					// (note we check both a regular zk
					// error and the error the test
					// produces)
					if zookeeper.IsError(err, zookeeper.ZNONODE) {
						return nil, nil
					}
					// otherwise we return the error
					return nil, err
				}
			}
			sort.Strings(children)

			results := make([][]string, len(children))
			wg := &sync.WaitGroup{}
			mu := &sync.Mutex{}
			var firstError error

			for j, child := range children {
				matched, err := path.Match(part, child)
				if err != nil {
					return nil, err
				}
				if matched {
					// we have a match!
					wg.Add(1)
					newParts := make([]string, len(parts))
					copy(newParts, parts)
					newParts[i] = child
					go func(j int) {
						defer wg.Done()
						subResult, err := resolveRecursive(zconn, newParts, false)
						if err != nil {
							mu.Lock()
							if firstError != nil {
								log.Printf("Multiple error: %v", err)
							} else {
								firstError = err
							}
							mu.Unlock()
						} else {
							results[j] = subResult
						}
					}(j)
				}
			}

			wg.Wait()
			if firstError != nil {
				return nil, firstError
			}

			result := make([]string, 0, 32)
			for j := 0; j < len(children); j++ {
				subResult := results[j]
				if subResult != nil {
					result = append(result, subResult...)
				}
			}

			// we found a part that is a wildcard, we
			// added the children already, we're done
			return result, nil
		}
	}

	// no part contains a wildcard, add the path if it exists, and done
	path := strings.Join(parts, "/")
	if toplevel {
		// for whatever the user typed at the toplevel, we don't
		// check it exists or not, we just return it
		return []string{path}, nil
	}

	// this is an expanded path, we need to check if it exists
	stat, err := zconn.Exists(path)
	if err != nil {
		return nil, err
	}
	if stat != nil {
		return []string{path}, nil
	}
	return nil, nil
}

func DeleteRecursive(zconn Conn, zkPath string, version int) error {
	// version: -1 delete any version of the node at path - only applies to the top node
	err := zconn.Delete(zkPath, version)
	if err == nil {
		return nil
	}
	if !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
		return err
	}
	// Remove the ability for other nodes to get created while we are trying to delete.
	// Otherwise, you can enter a race condition, or get starved out from deleting.
	err = zconn.SetACL(zkPath, zookeeper.WorldACL(zookeeper.PERM_ADMIN|zookeeper.PERM_DELETE|zookeeper.PERM_READ), version)
	if err != nil {
		return err
	}
	children, _, err := zconn.Children(zkPath)
	if err != nil {
		return err
	}
	for _, child := range children {
		err := DeleteRecursive(zconn, path.Join(zkPath, child), -1)
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
			return fmt.Errorf("zkutil: recursive delete failed: %v", err)
		}
	}

	err = zconn.Delete(zkPath, version)
	if err != nil && !zookeeper.IsError(err, zookeeper.ZNOTEMPTY) {
		err = fmt.Errorf("zkutil: nodes getting recreated underneath delete (app race condition): %v", zkPath)
	}
	return err
}

// The lexically lowest node is the lock holder - verify that this
// path holds the lock.  Call this queue-lock because the semantics are
// a hybrid.  Normal zookeeper locks make assumptions about sequential
// numbering that don't hold when the data in a lock is modified.
// if the provided 'interrupted' chan is closed, we'll just stop waiting
// and return an interruption error
func ObtainQueueLock(zconn Conn, zkPath string, wait time.Duration, interrupted chan struct{}) error {
	queueNode := path.Dir(zkPath)
	lockNode := path.Base(zkPath)

	timer := time.NewTimer(wait)
trylock:
	children, _, err := zconn.Children(queueNode)
	if err != nil {
		return fmt.Errorf("zkutil: trylock failed %v", err)
	}
	sort.Strings(children)
	if len(children) > 0 {
		if children[0] == lockNode {
			return nil
		}
		if wait > 0 {
			prevLock := ""
			for i := 1; i < len(children); i++ {
				if children[i] == lockNode {
					prevLock = children[i-1]
					break
				}
			}
			if prevLock == "" {
				return fmt.Errorf("zkutil: no previous queue node found: %v", zkPath)
			}

			zkPrevLock := path.Join(queueNode, prevLock)
			stat, watch, err := zconn.ExistsW(zkPrevLock)
			if err != nil {
				return fmt.Errorf("zkutil: unable to watch queued node %v %v", zkPrevLock, err)
			}
			if stat == nil {
				goto trylock
			}
			select {
			case <-timer.C:
				break
			case <-interrupted:
				return fmt.Errorf("zkutil: obtaining lock was interrupted %v", zkPath)
			case <-watch:
				// The precise event doesn't matter - try to read again regardless.
				goto trylock
			}
		}
		return fmt.Errorf("zkutil: obtaining lock timed out %v", zkPath)
	}
	return fmt.Errorf("zkutil: empty queue node: %v", queueNode)
}

// Close done when you want to exit cleanly.
func CreatePidNode(zconn Conn, zkPath string, done chan struct{}) error {
	hostname, err := os.Hostname()
	if err != nil {
		return fmt.Errorf("zkutil: failed creating pid node %v: %v", zkPath, err)
	}
	data := fmt.Sprintf("host:%v\npid:%v\n", hostname, os.Getpid())

	// On the first try, assume the cluster is up and running, that will
	// help hunt down any config issues present at startup
	_, err = zconn.Create(zkPath, data, zookeeper.EPHEMERAL, zookeeper.WorldACL(zookeeper.PERM_ALL))
	if err != nil {
		if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			err = zconn.Delete(zkPath, -1)
		}
		if err != nil {
			return fmt.Errorf("zkutil: failed deleting pid node: %v: %v", zkPath, err)
		}
		_, err = zconn.Create(zkPath, data, zookeeper.EPHEMERAL, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil {
			return fmt.Errorf("zkutil: failed creating pid node: %v: %v", zkPath, err)
		}
	}

	go func() {
		for {
			_, _, watch, err := zconn.GetW(zkPath)
			if err != nil {
				if zookeeper.IsError(err, zookeeper.ZNONODE) {
					_, err = zconn.Create(zkPath, data, zookeeper.EPHEMERAL, zookeeper.WorldACL(zookeeper.PERM_ALL))
					if err == nil {
						log.Printf("WARNING: failed recreating pid node: %v: %v", zkPath, err)
					} else {
						log.Printf("INFO: recreated pid node: %v", zkPath)
						continue
					}
				} else {
					log.Printf("WARNING: failed reading pid node: %v", err)
				}
			} else {
				select {
				case event := <-watch:
					if event.Ok() && event.Type == zookeeper.EVENT_DELETED {
						// Most likely another process has started up. However,
						// there is a chance that an ephemeral node is deleted by
						// the session expiring, yet that same session gets a watch
						// notification. This seems like buggy behavior, but rather
						// than race too hard on the node, just wait a bit and see
						// if the situation resolves itself.
						log.Printf("WARNING: pid deleted: %v", zkPath)
					} else {
						log.Printf("INFO: pid node event: %v", event)
					}
					// break here and wait for a bit before attempting
				case <-done:
					log.Printf("INFO: pid watcher stopped on done: %v", zkPath)
					return
				}
			}
			select {
			// No one likes a thundering herd, least of all zookeeper.
			case <-time.After(5*time.Second + time.Duration(rand.Int63n(55e9))):
			case <-done:
				log.Printf("INFO: pid watcher stopped on done: %v", zkPath)
				return
			}
		}
	}()

	return nil
}
<|endoftext|>


<commit_before>package fonet

import "testing"

var samples = [][][]float64{
	{
		{
			0,
			0,
		},
		{
			0,
		},
	},
	{
		{
			0,
			1,
		},
		{
			1,
		},
	},
	{
		{
			1,
			0,
		},
		{
			1,
		},
	},
	{
		{
			1,
			1,
		},
		{
			0,
		},
	},
}

func TestNetwork(t *testing.T) {
	n, err := NewNetwork([]int{2, 3, 1})
	if err != nil {
		t.FailNow()
	}

	n.Train(samples, 10000, 1.01, false)

	a := n.Predict([]float64{0, 0})[0]
	b := n.Predict([]float64{0, 1})[0]
	c := n.Predict([]float64{1, 0})[0]
	d := n.Predict([]float64{1, 1})[0]
	if int(a+0.5) != 0 || int(b+0.5) != 1 || int(c+0.5) != 1 || int(d+0.5) != 0 {
		t.Fail()
	}
}
<commit_msg>cleaned up tests<commit_after>package fonet

import (
	"math"
	"testing"
)

var samples = [][][]float64{
	{
		{
			0,
			0,
		},
		{
			0,
		},
	},
	{
		{
			0,
			1,
		},
		{
			1,
		},
	},
	{
		{
			1,
			0,
		},
		{
			1,
		},
	},
	{
		{
			1,
			1,
		},
		{
			0,
		},
	},
}

var samples2 = [][][]float64{
	{
		{
			0,
			0,
		},
		{
			10,
		},
	},
	{
		{
			0,
			1,
		},
		{
			20,
		},
	},
	{
		{
			1,
			0,
		},
		{
			20,
		},
	},
	{
		{
			1,
			1,
		},
		{
			10,
		},
	},
}

func TestNetwork(t *testing.T) {
	tests := []struct {
		name    string
		samples [][][]float64
		inps    [][]float64
		res     []float64
	}{
		{
			name:    "Output range 0-1",
			samples: samples,
			inps: [][]float64{
				{0, 0},
				{0, 1},
				{1, 0},
				{1, 1},
			},
			res: []float64{
				0,
				1,
				1,
				0,
			},
		},
		{
			name:    "Output range 10-20",
			samples: samples,
			inps: [][]float64{
				{0, 0},
				{0, 1},
				{1, 0},
				{1, 1},
			},
			res: []float64{
				10,
				20,
				20,
				10,
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t1 *testing.T) {
			if len(tt.inps) != len(tt.res) {
				t1.Fatalf("Different amount of test data [%d] vs results [%d]", len(tt.inps), len(tt.res))
			}

			n, err := NewNetwork([]int{2, 3, 1})
			if err != nil {
				t1.Fatalf("Could not create network: %+v", err)
			}

			n.Train(tt.samples, 10000, 1.01, false)

			for i, testData := range tt.inps {
				if res := n.Predict(testData)[0]; !percDiffLessThan(res, tt.res[i], 2) {
					t1.Errorf("Result is too different to be accurate; Got: %.2f, expected: %.2f", res, tt.res[i])
				}
			}
		})
	}
}

// percDiffLessThan returns whether v1 and v2 differ by the percentage.
func percDiffLessThan(v1, v2, perc float64) bool {
	absDiff := math.Abs(v1 - v2)
	// Prevent issues with divide by zero
	if absDiff == 0 || v1 == 0 || v2 == 0 {
		return true
	}

	decDiff := absDiff / math.Max(v1, v2)
	return decDiff*100.0 < perc
}
<|endoftext|>


<commit_before>package main

import(
    "os"
    "log"
    "bytes"
    "net/url"
    "net/http"
    "net/http/httputil"
    "net/http/httptest"
    //"encoding/json"
    "crypto/sha1"
)

func main() {
    // The remote URL has to be provided as the first command line argument
    remote_host := os.Args[1]
    log.Println("Initializing proxy for: " + remote_host)

    remote, err := url.Parse(remote_host)
    if err != nil {
        panic(err)
    }

    proxy := httputil.NewSingleHostReverseProxy(remote)
    http.HandleFunc("/", handler(proxy))
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(err)
    }
}

func hashKey(r *http.Request) [20]byte {
    url := []byte(r.URL.String())

    buf := new(bytes.Buffer)
    buf.ReadFrom(r.Body)
    body := buf.Bytes()

    return sha1.Sum(append(url,body...))
}

func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {
        rec := httptest.NewRecorder()

        hash := hashKey(r)
        log.Println(hash)

        // Perform the real request with a recorder
        p.ServeHTTP(rec, r)

        // Copy the recorded data to the actual ResponseWriter
        for k, v := range rec.Header() {
            w.Header()[k] = v
        }
        w.Header().Set("X-Eidetic", "Live request")
        w.Write(rec.Body.Bytes())
        // log.Println(rec.Body)
    }
}<commit_msg>First apparently working version<commit_after>package main

import (
	"os"
	"log"
	"bytes"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
	"net/url"
	"encoding/json"
	"crypto/sha1"
	"menteslibres.net/gosexy/redis"
	"encoding/base64"
)

type SerializableResponse struct {
    Header    map[string][]string   // the HTTP response headers
    Body      []byte                // the body string
}

func main() {
	// The remote URL has to be provided as the first command line argument
	remote_host := os.Args[1]
	log.Println("Initializing proxy for: " + remote_host)

	remote, err := url.Parse(remote_host)
	if err != nil {
		panic(err)
	}

	proxy := httputil.NewSingleHostReverseProxy(remote)

	redisClient := redis.New()
	err = redisClient.Connect("127.0.0.1", uint(6379))
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/", handler(proxy, redisClient))
	err = http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}
}

func hashKey(r *http.Request) string {
	url := []byte(r.URL.String())
	// log.Println("\n\n\nRequest URL: " + r.URL.String())

	// Read the body
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(err)
	}
	// log.Println("\nRequest Body:\n" + string(body))

	// Hack to pretend the reader has ben resetted
	reader := bytes.NewReader(body)
	r.Body = ioutil.NopCloser(reader)

	hash := sha1.Sum(append(url,body...))
	return "eidetic#" + base64.URLEncoding.EncodeToString(hash[:])
}

func handler(p *httputil.ReverseProxy, redisClient *redis.Client) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {

		hash := hashKey(r)

		serializableResponse := SerializableResponse{make(map[string][]string), nil}

		s, err := redisClient.Get(hash)
		if err != nil { // The request is not cached
			rec := httptest.NewRecorder()
			log.Println("Non cached: " + hash)

			// Perform the real request and cache it
			p.ServeHTTP(rec, r)

			for k, v := range rec.Header() {
				serializableResponse.Header[k] = v
			}
			serializableResponse.Body = rec.Body.Bytes()

			jsonResponse, err := json.Marshal(serializableResponse)
			if err != nil {
				panic(err)
			}
			redisClient.Set(hash, jsonResponse)
			w.Header().Set("X-Eidetic", "Live request")

		} else { // The request is cached
			log.Println("Cached!: " + hash)

			// Load the cached request
			err = json.Unmarshal([]byte(s), &serializableResponse)
			if err != nil {
				panic(err)
			}

			w.Header().Set("X-Eidetic", "Cached request")

		}

		//Copy the data to the actual ResponseWriter
		// log.Println("\n\n\nResponse Headers:")
		for k, v := range serializableResponse.Header {
			w.Header()[k] = v
			// log.Println(k + ": ")
			// for _, str := range v {
			// 	log.Println("  " + str)
			// }
		}
		w.Write([]byte(serializableResponse.Body))
		// log.Println("\nResponse body:\n" + string(serializableResponse.Body))

	}
}
<|endoftext|>


<commit_before>package nhc

import (
	"encoding/hex"
	"fmt"
	"net"
	"time"

	"github.com/mch1307/gomotics/log"
)

// Discover discover NHC controller by sending UDP pkg on port 10000
// return NHC IP address and boolean
func Discover() net.IP {
	//	var err error
	var nhcConnectString net.IP
	var targetAddr *net.UDPAddr
	data, _ := hex.DecodeString("44")
	addr := net.UDPAddr{IP: net.ParseIP("255.255.255.255"), Port: 10000}

	conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: 18043})
	if err != nil {
		fmt.Println("err connect: ", err)
	}

	_, err = conn.WriteToUDP(data, &addr)

	b := make([]byte, 1024)
	// goroutine for reading broadcast result
	go func() {
		for {
			defer conn.Close()
			_, targetAddr, err = conn.ReadFromUDP(b)
			if err != nil {
				log.Warnf("Error: UDP read error: %v", err)
				continue
			}
			// test "nhc" connection to replying IP to make sure targetAddr is a NHC controller
			connectString := net.TCPAddr{IP: targetAddr.IP, Port: 8000}
			if err == nil {
				_, err := net.DialTCP("tcp", nil, &connectString)
				//defer nhConn.Close()
				if err == nil {
					nhcConnectString = connectString.IP
					log.Debug("return IP: ", string(nhcConnectString))
					return
				}
			}
			//			return
		}
	}()
	time.Sleep(time.Second * 3)
	defer conn.Close()
	return nhcConnectString
}
<commit_msg>conn close<commit_after>package nhc

import (
	"encoding/hex"
	"fmt"
	"net"
	"time"

	"github.com/mch1307/gomotics/log"
)

// Discover discover NHC controller by sending UDP pkg on port 10000
// return NHC IP address and boolean
func Discover() net.IP {
	//	var err error
	var nhcConnectString net.IP
	var targetAddr *net.UDPAddr
	data, _ := hex.DecodeString("44")
	addr := net.UDPAddr{IP: net.ParseIP("255.255.255.255"), Port: 10000}

	conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: 18043})
	if err != nil {
		fmt.Println("err connect: ", err)
	}
	defer conn.Close()
	_, err = conn.WriteToUDP(data, &addr)

	b := make([]byte, 1024)
	// goroutine for reading broadcast result
	go func() {
		for {
			defer conn.Close()
			_, targetAddr, err = conn.ReadFromUDP(b)
			if err != nil {
				log.Warnf("Error: UDP read error: %v", err)
				continue
			}
			// test "nhc" connection to replying IP to make sure targetAddr is a NHC controller
			connectString := net.TCPAddr{IP: targetAddr.IP, Port: 8000}
			if err == nil {
				_, err := net.DialTCP("tcp", nil, &connectString)
				//defer nhConn.Close()
				if err == nil {
					nhcConnectString = connectString.IP
					log.Debug("return IP: ", string(nhcConnectString))
					return
				}
			}
			//			return
		}
	}()
	time.Sleep(time.Second * 3)
	return nhcConnectString
}
<|endoftext|>


<commit_before>package version

import "fmt"

const (
	// VersionMajor is for an API incompatible changes
	VersionMajor = 5
	// VersionMinor is for functionality in a backwards-compatible manner
	VersionMinor = 22
	// VersionPatch is for backwards-compatible bug fixes
	VersionPatch = 0

	// VersionDev indicates development branch. Releases will be empty string.
	VersionDev = ""
)

// Version is the specification version that the package types support.
var Version = fmt.Sprintf("%d.%d.%d%s", VersionMajor, VersionMinor, VersionPatch, VersionDev)
<commit_msg>Bump to v5.22.1-dev<commit_after>package version

import "fmt"

const (
	// VersionMajor is for an API incompatible changes
	VersionMajor = 5
	// VersionMinor is for functionality in a backwards-compatible manner
	VersionMinor = 22
	// VersionPatch is for backwards-compatible bug fixes
	VersionPatch = 1

	// VersionDev indicates development branch. Releases will be empty string.
	VersionDev = "-dev"
)

// Version is the specification version that the package types support.
var Version = fmt.Sprintf("%d.%d.%d%s", VersionMajor, VersionMinor, VersionPatch, VersionDev)
<|endoftext|>


<commit_before>// The version package provides a location to set the release versions for all
// packages to consume, without creating import cycles.
//
// This pckage should not import any other terraform packages.
package version

import (
	"fmt"

	version "github.com/hashicorp/go-version"
)

// The main version number that is being run at the moment.
const Version = "0.10.8"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
var Prerelease = "dev"

// SemVer is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
// proper semantic version, which should always be the case.
var SemVer = version.Must(version.NewVersion(Version))

// Header is the header name used to send the current terraform version
// in http requests.
const Header = "Terraform-Version"

// String returns the complete version string, including prerelease
func String() string {
	if Prerelease != "" {
		return fmt.Sprintf("%s-%s", Version, Prerelease)
	}
	return Version
}
<commit_msg>release: clean up after v0.10.8<commit_after>// The version package provides a location to set the release versions for all
// packages to consume, without creating import cycles.
//
// This pckage should not import any other terraform packages.
package version

import (
	"fmt"

	version "github.com/hashicorp/go-version"
)

// The main version number that is being run at the moment.
const Version = "0.10.9"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
var Prerelease = "dev"

// SemVer is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
// proper semantic version, which should always be the case.
var SemVer = version.Must(version.NewVersion(Version))

// Header is the header name used to send the current terraform version
// in http requests.
const Header = "Terraform-Version"

// String returns the complete version string, including prerelease
func String() string {
	if Prerelease != "" {
		return fmt.Sprintf("%s-%s", Version, Prerelease)
	}
	return Version
}
<|endoftext|>


<commit_before>package version

import "fmt"

const (
	// VersionMajor is for an API incompatible changes
	VersionMajor = 5
	// VersionMinor is for functionality in a backwards-compatible manner
	VersionMinor = 0
	// VersionPatch is for backwards-compatible bug fixes
	VersionPatch = 0

	// VersionDev indicates development branch. Releases will be empty string.
	VersionDev = ""
)

// Version is the specification version that the package types support.
var Version = fmt.Sprintf("%d.%d.%d%s", VersionMajor, VersionMinor, VersionPatch, VersionDev)
<commit_msg>Bump to v5.0.1-dev<commit_after>package version

import "fmt"

const (
	// VersionMajor is for an API incompatible changes
	VersionMajor = 5
	// VersionMinor is for functionality in a backwards-compatible manner
	VersionMinor = 0
	// VersionPatch is for backwards-compatible bug fixes
	VersionPatch = 1

	// VersionDev indicates development branch. Releases will be empty string.
	VersionDev = "-dev"
)

// Version is the specification version that the package types support.
var Version = fmt.Sprintf("%d.%d.%d%s", VersionMajor, VersionMinor, VersionPatch, VersionDev)
<|endoftext|>


<commit_before>// Copyright 2014 CoreOS, 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 version

import (
	"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
)

const Version = "0.11.4+git"

var SemVersion semver.Version

func init() {
	sv, err := semver.NewVersion(Version)
	if err != nil {
		panic("bad version string!")
	}
	SemVersion = *sv
}
<commit_msg>version: bump to v0.11.5+git<commit_after>// Copyright 2014 CoreOS, 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 version

import (
	"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
)

const Version = "0.11.5+git"

var SemVersion semver.Version

func init() {
	sv, err := semver.NewVersion(Version)
	if err != nil {
		panic("bad version string!")
	}
	SemVersion = *sv
}
<|endoftext|>


<commit_before>package version

// Version is the version of the build.
const Version = "0.1.12-dev"
<commit_msg>bump to v0.1.12<commit_after>package version

// Version is the version of the build.
const Version = "0.1.12"
<|endoftext|>


<commit_before>package version

// Package is the overall, canonical project import path under which the
// package was built.
var Package = "github.com/docker/distribution"

// Version indicates which version of the binary is running. This is set to
// the latest release tag by hand, always suffixed by "+unknown". During
// build, it will be replaced by the actual version. The value here will be
// used if the registry is run after a go get based install.
var Version = "v2.3.0+unknown"
<commit_msg>Rev base version to 2.4.0<commit_after>package version

// Package is the overall, canonical project import path under which the
// package was built.
var Package = "github.com/docker/distribution"

// Version indicates which version of the binary is running. This is set to
// the latest release tag by hand, always suffixed by "+unknown". During
// build, it will be replaced by the actual version. The value here will be
// used if the registry is run after a go get based install.
var Version = "v2.4.0+unknown"
<|endoftext|>


<commit_before>package version

import (
	"fmt"
	"strings"
)

var (
	Version = "dev"

	// GitCommit will be overwritten automatically by the build system
	GitCommit = "HEAD"
)

// FullVersion formats the version to be printed
func FullVersion() string {
	return fmt.Sprintf("%s, build %s", Version, GitCommit)
}

// RC checks if the Machine version is a release candidate or not
func RC() bool {
	return strings.Contains(Version, "rc")
}
<commit_msg>Bump version to 0.15.0<commit_after>package version

import (
	"fmt"
	"strings"
)

var (
	Version = "0.15.0"

	// GitCommit will be overwritten automatically by the build system
	GitCommit = "HEAD"
)

// FullVersion formats the version to be printed
func FullVersion() string {
	return fmt.Sprintf("%s, build %s", Version, GitCommit)
}

// RC checks if the Machine version is a release candidate or not
func RC() bool {
	return strings.Contains(Version, "rc")
}
<|endoftext|>


<commit_before>package version

const Maj = "0"
const Min = "10"
const Fix = "0"

var (
	// The full version string
	Version = "0.10.0"

	// GitCommit is set with --ldflags "-X main.gitCommit=$(git rev-parse HEAD)"
	GitCommit string
)

func init() {
	if GitCommit != "" {
		Version += "-" + GitCommit[:8]
	}
}
<commit_msg>version bump<commit_after>package version

const Maj = "0"
const Min = "10"
const Fix = "1"

var (
	// The full version string
	Version = "0.10.1"

	// GitCommit is set with --ldflags "-X main.gitCommit=$(git rev-parse HEAD)"
	GitCommit string
)

func init() {
	if GitCommit != "" {
		Version += "-" + GitCommit[:8]
	}
}
<|endoftext|>