<commit_before>package css // import "github.com/tdewolff/parse/css"

import (
	"io"
)

// Node is an interface that all nodes implement.
type Node interface {
	WriteTo(io.Writer) (int64, error)
}

////////////////////////////////////////////////////////////////

// TokenNode is a leaf node of a single token.
type TokenNode struct {
	TokenType
	Data []byte
}

// WriteTo writes the string representation of the node to the writer.
func (token TokenNode) WriteTo(w io.Writer) (int64, error) {
	n, err := w.Write(token.Data)
	return int64(n), err
}

////////////////////////////////////////////////////////////////

// StylesheetNode is the apex node of the whole stylesheet.
// Nodes contains TokenNode, AtRuleNode, DeclarationNode and RulesetNode nodes.
type StylesheetNode struct {
	Nodes []Node
}

// WriteTo writes the string representation of the node to the writer.
func (stylesheet StylesheetNode) WriteTo(w io.Writer) (size int64, err error) {
	var m int64
	for _, node := range stylesheet.Nodes {
		m, err = node.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}

////////////////////////////////////////////////////////////////

// AtRuleNode contains several nodes and/or a block node.
type AtRuleNode struct {
	Name  *TokenNode
	Nodes []Node
	Rules []Node
}

// WriteTo writes the string representation of the node to the writer.
func (atrule AtRuleNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	m, err = atrule.Name.WriteTo(w)
	if err != nil {
		return
	}
	size += m
	for i, node := range atrule.Nodes {
		if i != 0 {
			var t *TokenNode
			if k, ok := atrule.Nodes[i-1].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			} else if k, ok := atrule.Nodes[i].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			}
			if t == nil || t.Data[0] != ',' {
				n, err = w.Write([]byte(" "))
				if err != nil {
					return
				}
				size += int64(n)
			}
		} else {
			n, err = w.Write([]byte(" "))
			if err != nil {
				return
			}
			size += int64(n)
		}
		m, err = node.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	if len(atrule.Rules) > 0 {
		n, err = w.Write([]byte("{"))
		if err != nil {
			return
		}
		size += int64(n)
		for _, rule := range atrule.Rules {
			m, err = rule.WriteTo(w)
			if err != nil {
				return
			}
			size += m
		}
		n, err = w.Write([]byte("}"))
		if err != nil {
			return
		}
		size += int64(n)
	} else {
		n, err = w.Write([]byte(";"))
		if err != nil {
			return
		}
		size += int64(n)
	}
	return
}

////////////////////////////////////////////////////////////////

// RulesetNode consists of selector groups (separated by commas) and a declaration list.
type RulesetNode struct {
	Selectors []*SelectorNode
	Decls     []*DeclarationNode
}

// WriteTo writes the string representation of the node to the writer.
func (ruleset RulesetNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	for i, sel := range ruleset.Selectors {
		if i != 0 {
			n, err = w.Write([]byte(","))
			if err != nil {
				return
			}
			size += int64(n)
		}
		m, err = sel.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write([]byte("{"))
	if err != nil {
		return
	}
	size += int64(n)
	for _, decl := range ruleset.Decls {
		m, err = decl.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write([]byte("}"))
	if err != nil {
		return
	}
	size += int64(n)
	return
}

////////////////////////////////////////////////////////////////

// SelectorNode contains the tokens of a single selector.
type SelectorNode struct {
	Elems []*TokenNode
}

// WriteTo writes the string representation of the node to the writer.
func (sel SelectorNode) WriteTo(w io.Writer) (size int64, err error) {
	var m int64
	for _, elem := range sel.Elems {
		m, err = elem.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}

////////////////////////////////////////////////////////////////

// DeclarationNode represents a property declaration.
// Vals contains FunctionNode and TokenNode nodes.
type DeclarationNode struct {
	Prop *TokenNode
	Vals []Node
}

// WriteTo writes the string representation of the node to the writer.
func (decl DeclarationNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	m, err = decl.Prop.WriteTo(w)
	if err != nil {
		return
	}
	size += m
	n, err = w.Write([]byte(":"))
	if err != nil {
		return
	}
	size += int64(n)
	for i, val := range decl.Vals {
		if i != 0 {
			var t *TokenNode
			if k, ok := decl.Vals[i-1].(*TokenNode); ok && len(k.Data) == 1 && k.TokenType != IdentToken {
				t = k
			} else if k, ok := decl.Vals[i].(*TokenNode); ok && len(k.Data) == 1 && k.TokenType != IdentToken {
				t = k
			}
			if t == nil || (t.Data[0] != ',' && t.Data[0] != '/' && t.Data[0] != ':' && t.Data[0] != '.' && t.Data[0] != '!') {
				n, err = w.Write([]byte(" "))
				if err != nil {
					return
				}
				size += int64(n)
			}
		}
		m, err = val.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write([]byte(";"))
	if err != nil {
		return
	}
	size += int64(n)
	return
}

////////////////////////////////////////////////////////////////

// FunctionNode represents a function and its arguments (separated by commas).
type FunctionNode struct {
	Name *TokenNode
	Args []*ArgumentNode
}

// WriteTo writes the string representation of the node to the writer.
func (fun FunctionNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	m, err = fun.Name.WriteTo(w)
	if err != nil {
		return
	}
	size += m
	n, err = w.Write([]byte("("))
	if err != nil {
		return
	}
	size += int64(n)
	for i, arg := range fun.Args {
		if i != 0 {
			n, err = w.Write([]byte(","))
			if err != nil {
				return
			}
			size += int64(n)
		}
		m, err = arg.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write([]byte(")"))
	if err != nil {
		return
	}
	size += int64(n)
	return
}

////////////////////////////////////////////////////////////////

// ArgumentNode represents the key and parts of an argument separated by spaces.
type ArgumentNode struct {
	Vals []Node
}

// WriteTo writes the string representation of the node to the writer.
func (arg ArgumentNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	for i, val := range arg.Vals {
		if i != 0 {
			var t *TokenNode
			if k, ok := arg.Vals[i-1].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			} else if k, ok := arg.Vals[i].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			}
			if t == nil || (t.Data[0] != '=' && t.Data[0] != '*' && t.Data[0] != '/') {
				n, err = w.Write([]byte(" "))
				if err != nil {
					return
				}
				size += int64(n)
			}
		}
		m, err = val.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}

////////////////////////////////////////////////////////////////

// BlockNode contains the contents of a block (brace, bracket or parenthesis blocks).
// Nodes contains AtRuleNode, DeclarationNode, RulesetNode and BlockNode nodes.
type BlockNode struct {
	Open  *TokenNode
	Nodes []Node
	Close *TokenNode
}

// WriteTo writes the string representation of the node to the writer.
func (block BlockNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	if block.Open != nil && block.Close != nil && len(block.Nodes) > 0 {
		m, err = block.Open.WriteTo(w)
		if err != nil {
			return
		}
		size += m
		for i, node := range block.Nodes {
			if i != 0 {
				var t *TokenNode
				if k, ok := block.Nodes[i-1].(*TokenNode); ok && len(k.Data) == 1 {
					t = k
				} else if k, ok := block.Nodes[i].(*TokenNode); ok && len(k.Data) == 1 {
					t = k
				}
				if t == nil || t.Data[0] != ':' {
					n, err = w.Write([]byte(" "))
					if err != nil {
						return
					}
					size += int64(n)
				}
			}
			m, err = node.WriteTo(w)
			if err != nil {
				return
			}
			size += m
		}
		m, err = block.Close.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}
<commit_msg>Replace const byte slices and superfluous pointers<commit_after>package css // import "github.com/tdewolff/parse/css"

import (
	"io"
)

var (
	commaBytes            = []byte(",")
	spaceBytes            = []byte(" ")
	colonBytes            = []byte(":")
	semicolonBytes        = []byte(";")
	leftParenthesisBytes  = []byte("(")
	rightParenthesisBytes = []byte(")")
	leftBracketBytes      = []byte("{")
	rightBracketBytes     = []byte("}")
)

// Node is an interface that all nodes implement.
type Node interface {
	WriteTo(io.Writer) (int64, error)
}

////////////////////////////////////////////////////////////////

// TokenNode is a leaf node of a single token.
type TokenNode struct {
	TokenType
	Data []byte
}

// WriteTo writes the string representation of the node to the writer.
func (token TokenNode) WriteTo(w io.Writer) (int64, error) {
	n, err := w.Write(token.Data)
	return int64(n), err
}

////////////////////////////////////////////////////////////////

// StylesheetNode is the apex node of the whole stylesheet.
// Nodes contains TokenNode, AtRuleNode, DeclarationNode and RulesetNode nodes.
type StylesheetNode struct {
	Nodes []Node
}

// WriteTo writes the string representation of the node to the writer.
func (stylesheet StylesheetNode) WriteTo(w io.Writer) (size int64, err error) {
	var m int64
	for _, node := range stylesheet.Nodes {
		m, err = node.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}

////////////////////////////////////////////////////////////////

// AtRuleNode contains several nodes and/or a block node.
type AtRuleNode struct {
	Name  *TokenNode
	Nodes []Node
	Rules []Node
}

// WriteTo writes the string representation of the node to the writer.
func (atrule AtRuleNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	m, err = atrule.Name.WriteTo(w)
	if err != nil {
		return
	}
	size += m
	for i, node := range atrule.Nodes {
		if i != 0 {
			var t *TokenNode
			if k, ok := atrule.Nodes[i-1].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			} else if k, ok := atrule.Nodes[i].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			}
			if t == nil || t.Data[0] != ',' {
				n, err = w.Write(spaceBytes)
				if err != nil {
					return
				}
				size += int64(n)
			}
		} else {
			n, err = w.Write(spaceBytes)
			if err != nil {
				return
			}
			size += int64(n)
		}
		m, err = node.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	if len(atrule.Rules) > 0 {
		n, err = w.Write(leftBracketBytes)
		if err != nil {
			return
		}
		size += int64(n)
		for _, rule := range atrule.Rules {
			m, err = rule.WriteTo(w)
			if err != nil {
				return
			}
			size += m
		}
		n, err = w.Write(rightBracketBytes)
		if err != nil {
			return
		}
		size += int64(n)
	} else {
		n, err = w.Write(semicolonBytes)
		if err != nil {
			return
		}
		size += int64(n)
	}
	return
}

////////////////////////////////////////////////////////////////

// RulesetNode consists of selector groups (separated by commas) and a declaration list.
type RulesetNode struct {
	Selectors []SelectorNode
	Decls     []*DeclarationNode
}

// WriteTo writes the string representation of the node to the writer.
func (ruleset RulesetNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	for i, sel := range ruleset.Selectors {
		if i != 0 {
			n, err = w.Write(commaBytes)
			if err != nil {
				return
			}
			size += int64(n)
		}
		m, err = sel.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write(leftBracketBytes)
	if err != nil {
		return
	}
	size += int64(n)
	for _, decl := range ruleset.Decls {
		m, err = decl.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write(rightBracketBytes)
	if err != nil {
		return
	}
	size += int64(n)
	return
}

////////////////////////////////////////////////////////////////

// SelectorNode contains the tokens of a single selector.
type SelectorNode struct {
	Elems []*TokenNode
}

// WriteTo writes the string representation of the node to the writer.
func (sel SelectorNode) WriteTo(w io.Writer) (size int64, err error) {
	var m int64
	for _, elem := range sel.Elems {
		m, err = elem.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}

////////////////////////////////////////////////////////////////

// DeclarationNode represents a property declaration.
// Vals contains FunctionNode and TokenNode nodes.
type DeclarationNode struct {
	Prop *TokenNode
	Vals []Node
}

// WriteTo writes the string representation of the node to the writer.
func (decl DeclarationNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	m, err = decl.Prop.WriteTo(w)
	if err != nil {
		return
	}
	size += m
	n, err = w.Write(colonBytes)
	if err != nil {
		return
	}
	size += int64(n)
	for i, val := range decl.Vals {
		if i != 0 {
			var t *TokenNode
			if k, ok := decl.Vals[i-1].(*TokenNode); ok && len(k.Data) == 1 && k.TokenType != IdentToken {
				t = k
			} else if k, ok := decl.Vals[i].(*TokenNode); ok && len(k.Data) == 1 && k.TokenType != IdentToken {
				t = k
			}
			if t == nil || (t.Data[0] != ',' && t.Data[0] != '/' && t.Data[0] != ':' && t.Data[0] != '.' && t.Data[0] != '!') {
				n, err = w.Write(spaceBytes)
				if err != nil {
					return
				}
				size += int64(n)
			}
		}
		m, err = val.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write(semicolonBytes)
	if err != nil {
		return
	}
	size += int64(n)
	return
}

////////////////////////////////////////////////////////////////

// FunctionNode represents a function and its arguments (separated by commas).
type FunctionNode struct {
	Name *TokenNode
	Args []ArgumentNode
}

// WriteTo writes the string representation of the node to the writer.
func (fun FunctionNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	m, err = fun.Name.WriteTo(w)
	if err != nil {
		return
	}
	size += m
	n, err = w.Write(leftParenthesisBytes)
	if err != nil {
		return
	}
	size += int64(n)
	for i, arg := range fun.Args {
		if i != 0 {
			n, err = w.Write(commaBytes)
			if err != nil {
				return
			}
			size += int64(n)
		}
		m, err = arg.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	n, err = w.Write(rightParenthesisBytes)
	if err != nil {
		return
	}
	size += int64(n)
	return
}

////////////////////////////////////////////////////////////////

// ArgumentNode represents the key and parts of an argument separated by spaces.
type ArgumentNode struct {
	Vals []Node
}

// WriteTo writes the string representation of the node to the writer.
func (arg ArgumentNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	for i, val := range arg.Vals {
		if i != 0 {
			var t *TokenNode
			if k, ok := arg.Vals[i-1].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			} else if k, ok := arg.Vals[i].(*TokenNode); ok && len(k.Data) == 1 {
				t = k
			}
			if t == nil || (t.Data[0] != '=' && t.Data[0] != '*' && t.Data[0] != '/') {
				n, err = w.Write(spaceBytes)
				if err != nil {
					return
				}
				size += int64(n)
			}
		}
		m, err = val.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}

////////////////////////////////////////////////////////////////

// BlockNode contains the contents of a block (brace, bracket or parenthesis blocks).
// Nodes contains AtRuleNode, DeclarationNode, RulesetNode and BlockNode nodes.
type BlockNode struct {
	Open  *TokenNode
	Nodes []Node
	Close *TokenNode
}

// WriteTo writes the string representation of the node to the writer.
func (block BlockNode) WriteTo(w io.Writer) (size int64, err error) {
	var n int
	var m int64
	if block.Open != nil && block.Close != nil && len(block.Nodes) > 0 {
		m, err = block.Open.WriteTo(w)
		if err != nil {
			return
		}
		size += m
		for i, node := range block.Nodes {
			if i != 0 {
				var t *TokenNode
				if k, ok := block.Nodes[i-1].(*TokenNode); ok && len(k.Data) == 1 {
					t = k
				} else if k, ok := block.Nodes[i].(*TokenNode); ok && len(k.Data) == 1 {
					t = k
				}
				if t == nil || t.Data[0] != ':' {
					n, err = w.Write(spaceBytes)
					if err != nil {
						return
					}
					size += int64(n)
				}
			}
			m, err = node.WriteTo(w)
			if err != nil {
				return
			}
			size += m
		}
		m, err = block.Close.WriteTo(w)
		if err != nil {
			return
		}
		size += m
	}
	return
}
<|endoftext|>


<commit_before>// Copyright 2019 Google LLC
//
// 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
//
//     https://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.

// [START profiler_setup_go_compute_engine]

// snippets is an example of starting cloud.google.com/go/profiler.
package main

import (
	"cloud.google.com/go/profiler"
)

func main() {
	// Profiler initialization, best done as early as possible.
	if err := profiler.Start(profiler.Config{
		Service:        "myservice",
		ServiceVersion: "1.0.0",
		// ProjectID must be set if not running on GCP.
		// ProjectID: "my-project",
	}); err != nil {
		// TODO: Handle error.
	}
}

// [END profiler_setup_go_compute_engine]
<commit_msg>profiler: add WithTelemetryDisabled for OpenCensus users (#1543)<commit_after>// Copyright 2019 Google LLC
//
// 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
//
//     https://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.

// [START profiler_setup_go_compute_engine]

// snippets is an example of starting cloud.google.com/go/profiler.
package main

import (
	"cloud.google.com/go/profiler"
)

func main() {
	cfg := profiler.Config{
		Service:        "myservice",
		ServiceVersion: "1.0.0",
		// ProjectID must be set if not running on GCP.
		// ProjectID: "my-project",

		// For OpenCensus users:
		// To see Profiler agent spans in APM backend,
		// set EnableOCTelemetry to true
		// EnableOCTelemetry: true,
	}

	// Profiler initialization, best done as early as possible.
	if err := profiler.Start(cfg); err != nil {
		// TODO: Handle error.
	}
}

// [END profiler_setup_go_compute_engine]
<|endoftext|>


<commit_before>package neoutils

import (
	"fmt"

	"github.com/jmcvetta/neoism"
)

type NeoConnectionLegacy struct {
	NeoConnection
	db *neoism.Database
}

func (ncl *NeoConnectionLegacy) NeoDB() *neoism.Database {
	return ncl.db
}

func ConnectLegacy(neoURL string, conf *ConnectionConfig) (*NeoConnectionLegacy, error) {
	con, err := Connect(neoURL, conf)
	if err != nil {
		return nil, err
	}

	var db *neoism.Database

	switch c := con.(type) {
	case *DefaultNeoConnection:
		db = c.db
	default:
		return nil, fmt.Errorf("Unable to create NeoConnectionLegacy for underlying type %T", con)
	}

	return &NeoConnectionLegacy{con, db}, nil
}

/*
type legacyIndexManager struct {
	db *neoism.Database
}

func (lim *legacyIndexManager) CreateIndex(label, property string) (*neoism.Index, error) {
	return lim.db.CreateIndex(label, property)
}

func (lim *legacyIndexManager) CreateUniqueConstraint(label, property string) (*neoism.UniqueConstraint, error) {
	return lim.db.CreateUniqueConstraint(
	panic("write me")
}

func (lim *legacyIndexManager) Indexes(label string) ([]*neoism.Index, error) {
	panic("write me")
}

func (lim *legacyIndexManager) UniqueConstraints(label, property string) ([]*neoism.UniqueConstraint, error) {
	panic("write me")
}

type warnIndexManager struct {
}

func (lim *warnIndexManager) CreateIndex(label, property string) (*neoism.Index, error) {
	lim.warn()
	return nil, nil
}

func (lim *warnIndexManager) CreateUniqueConstraint(label, property string) (*neoism.UniqueConstraint, error) {
	lim.warn()
	return nil, nil
}

func (lim *warnIndexManager) Indexes(label string) ([]*neoism.Index, error) {
	lim.warn()
	return []*neoism.Index{}, nil
}

func (lim *warnIndexManager) UniqueConstraints(label, property string) ([]*neoism.UniqueConstraint, error) {
	lim.warn()
	return []*neoism.UniqueConstraint{}, nil
}

func (lim *warnIndexManager) warn() {
	log.Print("WARN : Not handling indexes or unique constraints.  Please update app to use neoutils.Connect()")
}
*/
<commit_msg>Remove NeoConnectionLegacy<commit_after>package neoutils

import (
	"log"

	"github.com/jmcvetta/neoism"
)

func UnderlyingDB(con NeoConnection) *neoism.Database {
	log.Println("App is using neoism connection directly. Please update when possible to avoid this.")

	switch c := con.(type) {
	case *DefaultNeoConnection:
		return c.db
	default:
		panic("unhandled NeoConnection type")
	}

}
<|endoftext|>


<commit_before>package QesyDb

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"reflect"
	"strconv"
	"strings"
	"time"
)

var Db *sql.DB

type Model struct {
	Cond   interface{}
	Insert map[string]string
	Update map[string]string
	Field  string
	Table  string
	Index  string
	Limit  interface{}
	Sort   string
	Tx     *sql.Tx
}

// Connect  is a method with a sql.
func Connect(connStr string) {
	sqlDb, err := sql.Open("mysql", connStr)
	if err != nil {
		log.Fatal("mysql connect error")
	}
	err = sqlDb.Ping()
	if err != nil {
		log.Fatal("mysql ping error")
	}
	fmt.Println("mysql connect sueccss")
	Db = sqlDb
}

func Begin() (*sql.Tx, error) {
	return Db.Begin()
}

func Rollback(tx *sql.Tx) error {
	return tx.Rollback()
}

func Commit(tx *sql.Tx) error {
	return tx.Commit()
}

// ExecSelectIndex  is a method with a sql.
func (m *Model) ExecSelectIndex() (map[string]map[string]string, error) {
	resultsSlice, _ := m.execSelect()
	retArr := map[string]map[string]string{}
	for _, v := range resultsSlice {
		if v[m.Index] == "" {
			continue
		}
		retArr[v[m.Index]] = v
	}
	m.Clean()
	return retArr, nil
}

func (m *Model) ExecSelect() ([]map[string]string, error) {
	m.Clean()
	return m.execSelect()
}

// ExecSelect is a method with a sql.
func (m *Model) execSelect() ([]map[string]string, error) {
	cond := m.getSQLCond()
	field := m.getSQLField()
	sort := m.getSort()
	limit := m.getSQLLimite()
	sqlStr := "SELECT " + field + " FROM " + m.Table + cond + sort + limit + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}

	if err != nil {
		return nil, err
	}
	rows, err := stmt.Query()
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	fields, err := rows.Columns()
	if err != nil {
		return nil, err
	}
	var resultsSlice []map[string]string
	for rows.Next() {
		result := make(map[string]string)
		var scanResultContainers []interface{}
		for i := 0; i < len(fields); i++ {
			var scanResultContainer interface{}
			scanResultContainers = append(scanResultContainers, &scanResultContainer)
		}
		if err := rows.Scan(scanResultContainers...); err != nil {
			return nil, err
		}
		for k, v := range fields {
			rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[k]))
			if rawValue.Interface() == nil {
				continue
			}
			rawType := reflect.TypeOf(rawValue.Interface())
			rawVal := reflect.ValueOf(rawValue.Interface())
			var str string
			switch rawType.Kind() {
			case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				str = strconv.FormatInt(rawVal.Int(), 10)
				result[v] = str
			case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				str = strconv.FormatUint(rawVal.Uint(), 10)
				result[v] = str
			case reflect.Float32, reflect.Float64:
				str = strconv.FormatFloat(rawVal.Float(), 'f', -1, 64)
				result[v] = str
			case reflect.Slice:
				if rawType.Elem().Kind() == reflect.Uint8 {
					result[v] = string(rawVal.Interface().([]byte))
					break
				}
			case reflect.String:
				str = rawVal.String()
				result[v] = str
			case reflect.Struct:
				str = rawVal.Interface().(time.Time).Format("2006-01-02 15:04:05.000 -0700")
				result[v] = str
			case reflect.Bool:
				if rawVal.Bool() {
					result[v] = "1"
				} else {
					result[v] = "0"
				}
			}
		}
		resultsSlice = append(resultsSlice, result)
	}
	return resultsSlice, nil
}

func (m *Model) ExecSelectOne() (map[string]string, error) {
	resultsSlice, err := m.ExecSelect()
	if len(resultsSlice) == 0 {
		return nil, err
	}
	return resultsSlice[0], nil
}

func (m *Model) ExecUpdate() (sql.Result, error) {
	updateStr := m.getSQLUpdate()
	condStr := m.getSQLCond()
	sqlStr := "UPDATE " + m.Table + " SET " + updateStr + condStr + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}
	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func (m *Model) ExecInsert() (sql.Result, error) {
	insert := m.getSQLInsert()
	sqlStr := "INSERT INTO " + m.Table + " " + insert + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}

	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func (m *Model) ExecReplace() (sql.Result, error) {
	insert := m.getSQLInsert()
	sqlStr := "REPLACE INTO " + m.Table + " " + insert + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}

	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func (m *Model) ExecDelete() (sql.Result, error) {
	condStr := m.getSQLCond()
	sqlStr := "DELETE FROM " + m.Table + condStr + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}
	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func GetLastInsertId(result sql.Result) (int64, error) {
	return result.LastInsertId()
}

func GetRowsAffected(result sql.Result) (int64, error) {
	return result.RowsAffected()
}

func (m *Model) getSQLCond() string {
	if str, ok := m.Cond.(string); ok {
		return str
	}
	var strArr []string
	if arr, ok := m.Cond.(map[string]string); ok {
		var strArr []string
		for k, v := range arr {
			strArr = append(strArr, k+"='"+v+"'")
		}
		return " WHERE " + strings.Join(strArr, " && ")
	}
	if arr, ok := m.Cond.(map[string]interface{}); ok {
		for k, v := range arr {
			if isStr, ok := v.(string); ok {
				strArr = append(strArr, k+"='"+isStr+"'")
			}
			if isStrArr, ok := v.([]string); ok {
				for k, v := range isStrArr {
					isStrArr[k] = "'" + v + "'"
				}
				strArr = append(strArr, k+" in ("+strings.Join(isStrArr, ",")+")")
			}
		}
		return " WHERE " + strings.Join(strArr, " && ")
	}
	return ""
}

func (m *Model) getSQLField() string {
	if m.Field != "" {
		return m.Field
	}
	return "*"
}

func (m *Model) getSort() string {
	if m.Sort != "" {
		return " ORDER BY " + m.Sort + " "
	}
	return ""
}

func (m *Model) getSQLUpdate() string {
	var strArr []string
	for k, v := range m.Update {
		strArr = append(strArr, k+"='"+v+"'")
	}
	return strings.Join(strArr, ",")
}

func (m *Model) getSQLInsert() string {
	var fieldArr, valueArr []string
	for k, v := range m.Insert {
		fieldArr = append(fieldArr, k)
		valueArr = append(valueArr, "'"+v+"'")
	}
	return "(" + strings.Join(fieldArr, ",") + ") values (" + strings.Join(valueArr, ",") + ")"
}

func (m *Model) getSQLLimite() string {
	if strArr, ok := m.Limit.([2]int); ok {
		return " LIMIT " + fmt.Sprintf("%d", strArr[0]) + ", " + fmt.Sprintf("%d", strArr[1])
	}
	return ""
}

func (m *Model) Clean() {
	m.Cond = nil
	m.Insert = nil
	m.Update = nil
	m.Field = ""
	m.Table = ""
	m.Index = ""
	m.Limit = nil
	m.Sort = ""
}
<commit_msg>fix bug<commit_after>package QesyDb

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"reflect"
	"strconv"
	"strings"
	"time"
)

var Db *sql.DB

type Model struct {
	Cond   interface{}
	Insert map[string]string
	Update map[string]string
	Field  string
	Table  string
	Index  string
	Limit  interface{}
	Sort   string
	Tx     *sql.Tx
}

// Connect  is a method with a sql.
func Connect(connStr string) {
	sqlDb, err := sql.Open("mysql", connStr)
	if err != nil {
		log.Fatal("mysql connect error")
	}
	err = sqlDb.Ping()
	if err != nil {
		log.Fatal("mysql ping error")
	}
	fmt.Println("mysql connect sueccss")
	Db = sqlDb
}

func Begin() (*sql.Tx, error) {
	return Db.Begin()
}

func Rollback(tx *sql.Tx) error {
	return tx.Rollback()
}

func Commit(tx *sql.Tx) error {
	return tx.Commit()
}

// ExecSelectIndex  is a method with a sql.
func (m *Model) ExecSelectIndex() (map[string]map[string]string, error) {
	resultsSlice, _ := m.execSelect()
	retArr := map[string]map[string]string{}
	for _, v := range resultsSlice {
		if v[m.Index] == "" {
			continue
		}
		retArr[v[m.Index]] = v
	}
	m.Clean()
	return retArr, nil
}

func (m *Model) ExecSelect() ([]map[string]string, error) {
	ret, err := m.execSelect()
	m.Clean()
	return ret, err
}

// ExecSelect is a method with a sql.
func (m *Model) execSelect() ([]map[string]string, error) {
	cond := m.getSQLCond()
	field := m.getSQLField()
	sort := m.getSort()
	limit := m.getSQLLimite()
	sqlStr := "SELECT " + field + " FROM " + m.Table + cond + sort + limit + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}

	if err != nil {
		return nil, err
	}
	rows, err := stmt.Query()
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	fields, err := rows.Columns()
	if err != nil {
		return nil, err
	}
	var resultsSlice []map[string]string
	for rows.Next() {
		result := make(map[string]string)
		var scanResultContainers []interface{}
		for i := 0; i < len(fields); i++ {
			var scanResultContainer interface{}
			scanResultContainers = append(scanResultContainers, &scanResultContainer)
		}
		if err := rows.Scan(scanResultContainers...); err != nil {
			return nil, err
		}
		for k, v := range fields {
			rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[k]))
			if rawValue.Interface() == nil {
				continue
			}
			rawType := reflect.TypeOf(rawValue.Interface())
			rawVal := reflect.ValueOf(rawValue.Interface())
			var str string
			switch rawType.Kind() {
			case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				str = strconv.FormatInt(rawVal.Int(), 10)
				result[v] = str
			case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				str = strconv.FormatUint(rawVal.Uint(), 10)
				result[v] = str
			case reflect.Float32, reflect.Float64:
				str = strconv.FormatFloat(rawVal.Float(), 'f', -1, 64)
				result[v] = str
			case reflect.Slice:
				if rawType.Elem().Kind() == reflect.Uint8 {
					result[v] = string(rawVal.Interface().([]byte))
					break
				}
			case reflect.String:
				str = rawVal.String()
				result[v] = str
			case reflect.Struct:
				str = rawVal.Interface().(time.Time).Format("2006-01-02 15:04:05.000 -0700")
				result[v] = str
			case reflect.Bool:
				if rawVal.Bool() {
					result[v] = "1"
				} else {
					result[v] = "0"
				}
			}
		}
		resultsSlice = append(resultsSlice, result)
	}
	return resultsSlice, nil
}

func (m *Model) ExecSelectOne() (map[string]string, error) {
	resultsSlice, err := m.ExecSelect()
	if len(resultsSlice) == 0 {
		return nil, err
	}
	return resultsSlice[0], nil
}

func (m *Model) ExecUpdate() (sql.Result, error) {
	updateStr := m.getSQLUpdate()
	condStr := m.getSQLCond()
	sqlStr := "UPDATE " + m.Table + " SET " + updateStr + condStr + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}
	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func (m *Model) ExecInsert() (sql.Result, error) {
	insert := m.getSQLInsert()
	sqlStr := "INSERT INTO " + m.Table + " " + insert + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}

	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func (m *Model) ExecReplace() (sql.Result, error) {
	insert := m.getSQLInsert()
	sqlStr := "REPLACE INTO " + m.Table + " " + insert + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}

	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func (m *Model) ExecDelete() (sql.Result, error) {
	condStr := m.getSQLCond()
	sqlStr := "DELETE FROM " + m.Table + condStr + ";"
	var err error
	var stmt *sql.Stmt
	if m.Tx == nil {
		stmt, err = Db.Prepare(sqlStr)
	} else {
		stmt, err = m.Tx.Prepare(sqlStr)
	}
	if err != nil {
		return nil, err
	}
	result, err := stmt.Exec()
	m.Clean()
	return result, err
}

func GetLastInsertId(result sql.Result) (int64, error) {
	return result.LastInsertId()
}

func GetRowsAffected(result sql.Result) (int64, error) {
	return result.RowsAffected()
}

func (m *Model) getSQLCond() string {
	if str, ok := m.Cond.(string); ok {
		return str
	}
	var strArr []string
	if arr, ok := m.Cond.(map[string]string); ok {
		var strArr []string
		for k, v := range arr {
			strArr = append(strArr, k+"='"+v+"'")
		}
		return " WHERE " + strings.Join(strArr, " && ")
	}
	if arr, ok := m.Cond.(map[string]interface{}); ok {
		for k, v := range arr {
			if isStr, ok := v.(string); ok {
				strArr = append(strArr, k+"='"+isStr+"'")
			}
			if isStrArr, ok := v.([]string); ok {
				for k, v := range isStrArr {
					isStrArr[k] = "'" + v + "'"
				}
				strArr = append(strArr, k+" in ("+strings.Join(isStrArr, ",")+")")
			}
		}
		return " WHERE " + strings.Join(strArr, " && ")
	}
	return ""
}

func (m *Model) getSQLField() string {
	if m.Field != "" {
		return m.Field
	}
	return "*"
}

func (m *Model) getSort() string {
	if m.Sort != "" {
		return " ORDER BY " + m.Sort + " "
	}
	return ""
}

func (m *Model) getSQLUpdate() string {
	var strArr []string
	for k, v := range m.Update {
		strArr = append(strArr, k+"='"+v+"'")
	}
	return strings.Join(strArr, ",")
}

func (m *Model) getSQLInsert() string {
	var fieldArr, valueArr []string
	for k, v := range m.Insert {
		fieldArr = append(fieldArr, k)
		valueArr = append(valueArr, "'"+v+"'")
	}
	return "(" + strings.Join(fieldArr, ",") + ") values (" + strings.Join(valueArr, ",") + ")"
}

func (m *Model) getSQLLimite() string {
	if strArr, ok := m.Limit.([2]int); ok {
		return " LIMIT " + fmt.Sprintf("%d", strArr[0]) + ", " + fmt.Sprintf("%d", strArr[1])
	}
	return ""
}

func (m *Model) Clean() {
	m.Cond = nil
	m.Insert = nil
	m.Update = nil
	m.Field = ""
	m.Table = ""
	m.Index = ""
	m.Limit = nil
	m.Sort = ""
}
<|endoftext|>


<commit_before>package quandl

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
)

// ApiKey is used to set your api key before you make any call
var ApiKey = ""

// LastUrl will hold the last requested URL after each call
var LastUrl = ""

// CacheHandler is a reference to a struct that implements the Cacher interface.
// If set, it will use it to get documents from the cache or set to it.
// See the example cache handler in the quandl/cache package.
var CacheHandler Cacher

var urlTemplates map[string]string = map[string]string{
	"symbol":  "https://www.quandl.com/api/v1/datasets/%s.%s?%s",
	"symbols": "https://www.quandl.com/api/v1/multisets.%s?columns=%s&%s",
	"search":  "https://www.quandl.com/api/v1/datasets.%s?%s",
	"list":    "http://www.quandl.com/api/v2/datasets.%s?%s",
}

// Type Options is used to send additional parameters in the Quandl request
type Options url.Values

// Set registers a key=value pair to be sent in the Quandl request
func (o Options) Set(key, value string) {
	o[key] = []string{value}
}

type Cacher interface {
	Get(key string) []byte
	Set(key string, data []byte) error
}

// GetSymbol returns data for a given symbol
func GetSymbol(symbol string, params Options) (SymbolResponse, error) {
	raw, err := GetSymbolRaw(symbol, "json", params)
	var response SymbolResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetSymbols returns data for a symbols array
func GetSymbols(symbols []string, params Options) (SymbolsResponse, error) {
	raw, err := GetSymbolsRaw(symbols, "json", params)
	var response SymbolsResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetList returns a list of symbols for a source
func GetList(source string, page int, perPage int) (ListResponse, error) {
	raw, err := GetListRaw(source, "json", page, perPage)
	var response ListResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetSearch returns search results
func GetSearch(query string, page int, perPage int) (SearchResponse, error) {
	raw, err := GetSearchRaw(query, "json", page, perPage)
	var response SearchResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetSymbolRaw returns CSV, JSON or XML data for a given symbol
func GetSymbolRaw(symbol string, format string, params Options) ([]byte, error) {
	url := getUrl("symbol", symbol, format, arrangeParams(params))
	return getData(url)
}

// GetSymbolsRaw returns CSV, JSON or XML data for multiple symbols
func GetSymbolsRaw(symbols []string, format string, params Options) ([]byte, error) {
	url := getUrl("symbols", "json", symbolsToString(symbols), arrangeParams(params))
	return getData(url)
}

// GetListRaw returns a list of symbols for a source as CSV, JSON or XML
func GetListRaw(source string, format string, page int, perPage int) ([]byte, error) {
	params := Options{}

	params.Set("query", "*")
	params.Set("source_code", source)
	params.Set("per_page", strconv.Itoa(perPage))
	params.Set("page", strconv.Itoa(page))

	url := getUrl("list", "json", arrangeParams(params))
	return getData(url)
}

// GetSearchRaw returns search results as JSON or XML
func GetSearchRaw(query string, format string, page int, perPage int) ([]byte, error) {
	params := Options{}

	params.Set("query", query)
	params.Set("per_page", strconv.Itoa(perPage))
	params.Set("page", strconv.Itoa(page))

	url := getUrl("search", "json", arrangeParams(params))
	return getData(url)
}

// getData requests a URL from Quandl and returns the raw response string
func getData(url string) ([]byte, error) {

	if CacheHandler != nil {
		if response := CacheHandler.Get(url); response != nil {
			return response, nil
		}
	}

	response, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()
	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	if CacheHandler != nil {
		if err := CacheHandler.Set(url, contents); err != nil {
			return nil, err
		}
	}

	return contents, nil
}

// getUrl receives a kind that points to a URL template and
// a variable number of strings, which will be replaced
// in the template.
func getUrl(kind string, args ...interface{}) string {
	template := urlTemplates[kind]
	LastUrl = strings.Trim(fmt.Sprintf(template, args...), "&?")
	return LastUrl
}

// arrangeParams takes an Options map and converts it to
// a query string. It will also append the api key as needed.
func arrangeParams(qs Options) string {
	if qs == nil {
		if ApiKey == "" {
			return ""
		}
		qs = Options{}
	}
	qs.Set("auth_token", ApiKey)
	return url.Values(qs).Encode()
}

// symbolsToString converts an array of symbols to the format
// needed for a multiset Quandl request.
func symbolsToString(symbols []string) string {
	var result []string
	for _, symbol := range symbols {
		result = append(result, strings.Replace(symbol, "/", ".", -1))
	}
	return strings.Join(result, ",")
}

// marshallerError returns a formatted error that includes the response
func marshallerError(response []byte, err error) error {
	return errors.New("JSON Marshaller Error:\nRESPONSE:\n" + string(response) + "\n\nERROR:\n" + err.Error())
}
<commit_msg>Fixed auth_token will no longer be sent if empty<commit_after>package quandl

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
)

// ApiKey is used to set your api key before you make any call
var ApiKey = ""

// LastUrl will hold the last requested URL after each call
var LastUrl = ""

// CacheHandler is a reference to a struct that implements the Cacher interface.
// If set, it will use it to get documents from the cache or set to it.
// See the example cache handler in the quandl/cache package.
var CacheHandler Cacher

var urlTemplates map[string]string = map[string]string{
	"symbol":  "https://www.quandl.com/api/v1/datasets/%s.%s?%s",
	"symbols": "https://www.quandl.com/api/v1/multisets.%s?columns=%s&%s",
	"search":  "https://www.quandl.com/api/v1/datasets.%s?%s",
	"list":    "http://www.quandl.com/api/v2/datasets.%s?%s",
}

// Type Options is used to send additional parameters in the Quandl request
type Options url.Values

// Set registers a key=value pair to be sent in the Quandl request
func (o Options) Set(key, value string) {
	o[key] = []string{value}
}

type Cacher interface {
	Get(key string) []byte
	Set(key string, data []byte) error
}

// GetSymbol returns data for a given symbol
func GetSymbol(symbol string, params Options) (SymbolResponse, error) {
	raw, err := GetSymbolRaw(symbol, "json", params)
	var response SymbolResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetSymbols returns data for a symbols array
func GetSymbols(symbols []string, params Options) (SymbolsResponse, error) {
	raw, err := GetSymbolsRaw(symbols, "json", params)
	var response SymbolsResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetList returns a list of symbols for a source
func GetList(source string, page int, perPage int) (ListResponse, error) {
	raw, err := GetListRaw(source, "json", page, perPage)
	var response ListResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetSearch returns search results
func GetSearch(query string, page int, perPage int) (SearchResponse, error) {
	raw, err := GetSearchRaw(query, "json", page, perPage)
	var response SearchResponse
	if err != nil {
		return response, err
	}

	err = json.Unmarshal(raw, &response)
	if err != nil {
		return response, marshallerError(raw, err)
	}
	return response, nil
}

// GetSymbolRaw returns CSV, JSON or XML data for a given symbol
func GetSymbolRaw(symbol string, format string, params Options) ([]byte, error) {
	url := getUrl("symbol", symbol, format, arrangeParams(params))
	return getData(url)
}

// GetSymbolsRaw returns CSV, JSON or XML data for multiple symbols
func GetSymbolsRaw(symbols []string, format string, params Options) ([]byte, error) {
	url := getUrl("symbols", "json", symbolsToString(symbols), arrangeParams(params))
	return getData(url)
}

// GetListRaw returns a list of symbols for a source as CSV, JSON or XML
func GetListRaw(source string, format string, page int, perPage int) ([]byte, error) {
	params := Options{}

	params.Set("query", "*")
	params.Set("source_code", source)
	params.Set("per_page", strconv.Itoa(perPage))
	params.Set("page", strconv.Itoa(page))

	url := getUrl("list", "json", arrangeParams(params))
	return getData(url)
}

// GetSearchRaw returns search results as JSON or XML
func GetSearchRaw(query string, format string, page int, perPage int) ([]byte, error) {
	params := Options{}

	params.Set("query", query)
	params.Set("per_page", strconv.Itoa(perPage))
	params.Set("page", strconv.Itoa(page))

	url := getUrl("search", "json", arrangeParams(params))
	return getData(url)
}

// getData requests a URL from Quandl and returns the raw response string
func getData(url string) ([]byte, error) {

	if CacheHandler != nil {
		if response := CacheHandler.Get(url); response != nil {
			return response, nil
		}
	}

	response, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()
	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	if CacheHandler != nil {
		if err := CacheHandler.Set(url, contents); err != nil {
			return nil, err
		}
	}

	return contents, nil
}

// getUrl receives a kind that points to a URL template and
// a variable number of strings, which will be replaced
// in the template.
func getUrl(kind string, args ...interface{}) string {
	template := urlTemplates[kind]
	LastUrl = strings.Trim(fmt.Sprintf(template, args...), "&?")
	return LastUrl
}

// arrangeParams takes an Options map and converts it to
// a query string. It will also append the api key as needed.
func arrangeParams(qs Options) string {
	if qs == nil {
		if ApiKey == "" {
			return ""
		}
		qs = Options{}
	}
	if ApiKey != "" {
		qs.Set("auth_token", ApiKey)
	}
	return url.Values(qs).Encode()
}

// symbolsToString converts an array of symbols to the format
// needed for a multiset Quandl request.
func symbolsToString(symbols []string) string {
	var result []string
	for _, symbol := range symbols {
		result = append(result, strings.Replace(symbol, "/", ".", -1))
	}
	return strings.Join(result, ",")
}

// marshallerError returns a formatted error that includes the response
func marshallerError(response []byte, err error) error {
	return errors.New("JSON Marshaller Error:\nRESPONSE:\n" + string(response) + "\n\nERROR:\n" + err.Error())
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/explicite/i2c/bh1750"
	"github.com/explicite/i2c/lps331ap"
)

var tick = flag.Float64("tick", float64(3600), "tick in sec")
var mes = flag.String("mes", "", "measurement")

var (
	Trace   *log.Logger
	Info    *log.Logger
	Warning *log.Logger
	Error   *log.Logger
)

func transaction(f func() error) {
	err := f()
	if err != nil {
		panic(err)
	}
}

func init() {
	flag.Parse()
	if *mes == "" {
		panic("set measurement name")
	}
}

func Logging(
	traceHandle io.Writer,
	infoHandle io.Writer,
	warningHandle io.Writer,
	errorHandle io.Writer) {

	Trace = log.New(traceHandle,
		"TRACE: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Info = log.New(infoHandle,
		"INFO: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Warning = log.New(warningHandle,
		"WARNING: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Error = log.New(errorHandle,
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)
}

func out(l *lps331ap.LPS331AP, b *bh1750.BH1750) <-chan string {
	tmp := make(chan string)
	go func() {
		for {
			temp, _ := l.Temperature()
			press, _ := l.Pressure()
			lux, _ := b.Lux(bh1750.ConHRes1lx)
			str := fmt.Sprintf(
				"%s quantity=tmp value=%f\n%s quantity=press value=%f\n%s quantity=lux value=%f",
				*mes, temp, *mes, press, *mes, lux)
			tmp <- str
			time.Sleep(time.Duration(*tick) * time.Second)
		}
	}()

	return tmp
}

func main() {
	Logging(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)

	Trace.Println("lps331AP initialization")
	l := &lps331ap.LPS331AP{}
	transaction(func() error { return l.Init(0x5d, 1) })
	transaction(l.Active)
	defer l.Deactive()

	Trace.Println("bh1750 initialization")
	b := &bh1750.BH1750{}
	transaction(func() error { return b.Init(0x23, 1) })
	transaction(b.Active)
	defer b.Deactive()

	out := out(l, b)
	for {
		msg := []byte(<-out)
		resp, err := http.Post("http://localhost:8086/write?db=quartz", "text/plain", bytes.NewBuffer(msg))
		if err != nil {
			Warning.Println(fmt.Sprintf("cannot send measurement:%v", err))
		}
		Info.Println(fmt.Sprintf("response:%v", resp))

	}
}
<commit_msg>stub for si7021 support<commit_after>package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/explicite/i2c/bh1750"
	"github.com/explicite/i2c/lps331ap"
	"github.com/explicite/i2c/si7021"
)

var tick = flag.Float64("tick", float64(3600), "tick in sec")
var mes = flag.String("mes", "", "measurement")

var (
	Trace   *log.Logger
	Info    *log.Logger
	Warning *log.Logger
	Error   *log.Logger
)

func transaction(f func() error) {
	err := f()
	if err != nil {
		panic(err)
	}
}

func init() {
	flag.Parse()
	if *mes == "" {
		panic("set measurement name")
	}
}

func Logging(
	traceHandle io.Writer,
	infoHandle io.Writer,
	warningHandle io.Writer,
	errorHandle io.Writer) {

	Trace = log.New(traceHandle,
		"TRACE: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Info = log.New(infoHandle,
		"INFO: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Warning = log.New(warningHandle,
		"WARNING: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Error = log.New(errorHandle,
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)
}

func out(l *lps331ap.LPS331AP, b *bh1750.BH1750, s *si7021.SI7021) <-chan string {
	tmp := make(chan string)
	go func() {
		for {
			temp, _ := l.Temperature()
			press, _ := l.Pressure()
			lux, _ := b.Lux(bh1750.ConHRes1lx)
			rh, _ := s.RelativeHumidity(false)
			str := fmt.Sprintf(
				"%s quantity=tmp value=%f\n%s quantity=press value=%f\n%s quantity=lux value=%f\n%s quantity=rh value=%f",
				*mes, temp, *mes, press, *mes, lux, *mes, rh)
			tmp <- str
			time.Sleep(time.Duration(*tick) * time.Second)
		}
	}()

	return tmp
}

func main() {
	Logging(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)

	Trace.Println("lps331AP initialization")
	l := &lps331ap.LPS331AP{}
	transaction(func() error { return l.Init(0x5d, 1) })
	transaction(l.Active)
	defer l.Deactive()

	Trace.Println("bh1750 initialization")
	b := &bh1750.BH1750{}
	transaction(func() error { return b.Init(0x23, 1) })
	transaction(b.Active)
	defer b.Deactive()

	Trace.Println("si7021 initialization")
	s := &si7021.SI7021{}
	transaction(func() error { return s.Init(0x40, 1) })
	transaction(s.Active)
	defer s.Deactive()
	out := out(l, b, s)
	for {
		msg := []byte(<-out)
		resp, err := http.Post("http://localhost:8086/write?db=quartz", "text/plain", bytes.NewBuffer(msg))
		if err != nil {
			Warning.Println(fmt.Sprintf("cannot send measurement:%v", err))
		}
		Info.Println(fmt.Sprintf("response:%v", resp))

	}
}
<|endoftext|>


<commit_before>package rabbit

import (
	"math"
	"time"

	"github.com/streadway/amqp"
	"golang.org/x/net/context"
)

type logger interface {
	Debugf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Warnf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
}

type queue struct {
	name       string
	durable    bool
	autoDelete bool
	exclusive  bool
	noWait     bool
}

type queueBind struct {
	noWait bool
}

type exchange struct {
	name       string
	kind       string
	durable    bool
	autoDelete bool
	internal   bool
	noWait     bool
}

type consume struct {
	consumeTag string
	noAck      bool
	exclusive  bool
	noLocal    bool
	noWait     bool
}

type session struct {
	connection *amqp.Connection
	channel    *amqp.Channel
}

func (s *session) Close() {
	if s.channel != nil {
		_ = s.channel.Close()
	}

	if s.connection != nil {
		_ = s.connection.Close()
	}
}

type Rabbit interface {
	StartListening() <-chan amqp.Delivery
}

var _ Rabbit = (*RabbitImpl)(nil)

type RabbitImpl struct {
	context     context.Context
	authority   string
	exchange    exchange
	queue       queue
	queueBind   queueBind
	bindingKeys []string
	consume     consume
	logger      logger
}

type OptionFunc func(r *RabbitImpl)

func New(options ...OptionFunc) Rabbit {
	r := &RabbitImpl{}

	for _, fn := range options {
		fn(r)
	}

	return r
}

func OptionContext(ctx context.Context) OptionFunc {
	return func(r *RabbitImpl) {
		r.context = ctx
	}
}

func OptionAuthority(authority string) OptionFunc {
	return func(r *RabbitImpl) {
		r.authority = authority
	}
}

func OptionLogger(logger logger) OptionFunc {
	return func(r *RabbitImpl) {
		r.logger = logger
	}
}

func OptionExchange(name, kind string, durable, autoDelete, internal, noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.exchange = exchange{
			name:       name,
			kind:       kind,
			durable:    durable,
			autoDelete: autoDelete,
			internal:   internal,
			noWait:     noWait,
		}
	}
}

func OptionConsume(consumeTag string, noAck, exclusive, noLocal, noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.consume = consume{
			consumeTag: consumeTag,
			noAck:      noAck,
			exclusive:  exclusive,
			noLocal:    noLocal,
			noWait:     noWait,
		}
	}
}

func OptionQueue(name string, durable, autoDelete, exclusive, noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.queue = queue{
			name:       name,
			durable:    durable,
			autoDelete: autoDelete,
			exclusive:  exclusive,
			noWait:     noWait,
		}
	}
}

func OptionQueueBind(noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.queueBind = queueBind{
			noWait: noWait,
		}
	}
}

func OptionBindingKeys(keys []string) OptionFunc {
	return func(r *RabbitImpl) {
		r.bindingKeys = keys
	}
}

func (r *RabbitImpl) StartListening() <-chan amqp.Delivery {
	req, res := redial(r)

	out := make(chan amqp.Delivery, 100)

	go func() {
		defer close(out)

		for {
			req <- true
			session := <-res

			queue, err := session.channel.QueueDeclare(r.queue.name, r.queue.durable, r.queue.autoDelete, r.queue.exclusive, r.queue.noWait, nil)
			if err != nil {
				r.logger.Errorf("failed to declare queue: %s", queue.Name)
				session.Close()
				continue
			}

			for _, key := range r.bindingKeys {
				err = session.channel.QueueBind(
					r.queue.name,
					key,
					r.exchange.name,
					r.queueBind.noWait,
					nil,
				)
				if err != nil {
					r.logger.Errorf("failed to bind to key: %s, error: %s", key, err)
					session.Close()
					continue
				}
			}

			deliveries, err := session.channel.Consume(
				r.queue.name,
				r.consume.consumeTag,
				r.consume.noAck,
				r.consume.exclusive,
				r.consume.noLocal,
				r.consume.noWait,
				nil,
			)
			if err != nil {
				r.logger.Errorf("failed to consume from queue %s, error: %s", queue.Name, err)

				session.Close()
				continue
			}

			for d := range deliveries {
				select {
				case out <- d:
				case <-r.context.Done():
					r.logger.Infof("shutting down listener")
					session.Close()
					return

				}
			}

			session.Close()
		}
	}()

	return out
}

func redial(r *RabbitImpl) (request chan<- bool, response <-chan session) {
	req := make(chan bool)
	res := make(chan session)

	go func() {
		defer close(req)
		defer close(res)

		for {
			select {
			case <-req:
			case <-r.context.Done():
				r.logger.Infof("shutting down session reconnector")
				return
			}

			connect(r, res)
		}
	}()

	return req, res
}

func connect(r *RabbitImpl, out chan<- session) {
	backoff := 0.0

	exp := func(b float64) float64 {
		if b == 0.0 {
			return 0.25
		}

		return math.Min(float64(b*2), 30)
	}

	for {
		select {
		case <-time.After(time.Duration(backoff) * time.Second):
		case <-r.context.Done():
			r.logger.Infof("shutting down session reconnector.")
			return
		}

		conn, err := amqp.Dial(r.authority)
		if err != nil {
			backoff = exp(backoff)
			r.logger.Errorf("failed to connect to AMQP, will retry in: %f seconds", backoff)
			continue
		}

		channel, err := conn.Channel()
		if err != nil {
			conn.Close()
			backoff = exp(backoff)
			r.logger.Errorf("failed to get channel from AMQP, will retry in: %f seconds", backoff)
			continue
		}

		err = channel.ExchangeDeclare(
			r.exchange.name,
			r.exchange.kind,
			r.exchange.durable,
			r.exchange.autoDelete,
			r.exchange.internal,
			r.exchange.noWait,
			nil,
		)
		if err != nil {
			channel.Close()
			conn.Close()
			backoff = exp(backoff)
			r.logger.Errorf("failed to declare exchange in AMQP, will retry in: %f seconds, error: %s", backoff, err)
			continue
		}

		backoff = 0.0

		select {
		case out <- session{conn, channel}:
			r.logger.Infof("connected to AMQP")
			return
		case <-r.context.Done():
			r.logger.Infof("shutting down reconnector")
			return
		}
	}
}
<commit_msg>Change logger interface to match syb-core logger<commit_after>package rabbit

import (
	"math"
	"time"

	"github.com/streadway/amqp"
	"golang.org/x/net/context"
)

type logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Criticalf(format string, args ...interface{})
}

type queue struct {
	name       string
	durable    bool
	autoDelete bool
	exclusive  bool
	noWait     bool
}

type queueBind struct {
	noWait bool
}

type exchange struct {
	name       string
	kind       string
	durable    bool
	autoDelete bool
	internal   bool
	noWait     bool
}

type consume struct {
	consumeTag string
	noAck      bool
	exclusive  bool
	noLocal    bool
	noWait     bool
}

type session struct {
	connection *amqp.Connection
	channel    *amqp.Channel
}

func (s *session) Close() {
	if s.channel != nil {
		_ = s.channel.Close()
	}

	if s.connection != nil {
		_ = s.connection.Close()
	}
}

type Rabbit interface {
	StartListening() <-chan amqp.Delivery
}

var _ Rabbit = (*RabbitImpl)(nil)

type RabbitImpl struct {
	context     context.Context
	authority   string
	exchange    exchange
	queue       queue
	queueBind   queueBind
	bindingKeys []string
	consume     consume
	logger      logger
}

type OptionFunc func(r *RabbitImpl)

func New(options ...OptionFunc) Rabbit {
	r := &RabbitImpl{}

	for _, fn := range options {
		fn(r)
	}

	return r
}

func OptionContext(ctx context.Context) OptionFunc {
	return func(r *RabbitImpl) {
		r.context = ctx
	}
}

func OptionAuthority(authority string) OptionFunc {
	return func(r *RabbitImpl) {
		r.authority = authority
	}
}

func OptionLogger(logger logger) OptionFunc {
	return func(r *RabbitImpl) {
		r.logger = logger
	}
}

func OptionExchange(name, kind string, durable, autoDelete, internal, noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.exchange = exchange{
			name:       name,
			kind:       kind,
			durable:    durable,
			autoDelete: autoDelete,
			internal:   internal,
			noWait:     noWait,
		}
	}
}

func OptionConsume(consumeTag string, noAck, exclusive, noLocal, noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.consume = consume{
			consumeTag: consumeTag,
			noAck:      noAck,
			exclusive:  exclusive,
			noLocal:    noLocal,
			noWait:     noWait,
		}
	}
}

func OptionQueue(name string, durable, autoDelete, exclusive, noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.queue = queue{
			name:       name,
			durable:    durable,
			autoDelete: autoDelete,
			exclusive:  exclusive,
			noWait:     noWait,
		}
	}
}

func OptionQueueBind(noWait bool) OptionFunc {
	return func(r *RabbitImpl) {
		r.queueBind = queueBind{
			noWait: noWait,
		}
	}
}

func OptionBindingKeys(keys []string) OptionFunc {
	return func(r *RabbitImpl) {
		r.bindingKeys = keys
	}
}

func (r *RabbitImpl) StartListening() <-chan amqp.Delivery {
	req, res := redial(r)

	out := make(chan amqp.Delivery, 100)

	go func() {
		defer close(out)

		for {
			req <- true
			session := <-res

			queue, err := session.channel.QueueDeclare(r.queue.name, r.queue.durable, r.queue.autoDelete, r.queue.exclusive, r.queue.noWait, nil)
			if err != nil {
				r.logger.Errorf("failed to declare queue: %s", queue.Name)
				session.Close()
				continue
			}

			for _, key := range r.bindingKeys {
				err = session.channel.QueueBind(
					r.queue.name,
					key,
					r.exchange.name,
					r.queueBind.noWait,
					nil,
				)
				if err != nil {
					r.logger.Errorf("failed to bind to key: %s, error: %s", key, err)
					session.Close()
					continue
				}
			}

			deliveries, err := session.channel.Consume(
				r.queue.name,
				r.consume.consumeTag,
				r.consume.noAck,
				r.consume.exclusive,
				r.consume.noLocal,
				r.consume.noWait,
				nil,
			)
			if err != nil {
				r.logger.Errorf("failed to consume from queue %s, error: %s", queue.Name, err)

				session.Close()
				continue
			}

			for d := range deliveries {
				select {
				case out <- d:
				case <-r.context.Done():
					r.logger.Infof("shutting down listener")
					session.Close()
					return

				}
			}

			session.Close()
		}
	}()

	return out
}

func redial(r *RabbitImpl) (request chan<- bool, response <-chan session) {
	req := make(chan bool)
	res := make(chan session)

	go func() {
		defer close(req)
		defer close(res)

		for {
			select {
			case <-req:
			case <-r.context.Done():
				r.logger.Infof("shutting down session reconnector")
				return
			}

			connect(r, res)
		}
	}()

	return req, res
}

func connect(r *RabbitImpl, out chan<- session) {
	backoff := 0.0

	exp := func(b float64) float64 {
		if b == 0.0 {
			return 0.25
		}

		return math.Min(float64(b*2), 30)
	}

	for {
		select {
		case <-time.After(time.Duration(backoff) * time.Second):
		case <-r.context.Done():
			r.logger.Infof("shutting down session reconnector.")
			return
		}

		conn, err := amqp.Dial(r.authority)
		if err != nil {
			backoff = exp(backoff)
			r.logger.Errorf("failed to connect to AMQP, will retry in: %f seconds", backoff)
			continue
		}

		channel, err := conn.Channel()
		if err != nil {
			conn.Close()
			backoff = exp(backoff)
			r.logger.Errorf("failed to get channel from AMQP, will retry in: %f seconds", backoff)
			continue
		}

		err = channel.ExchangeDeclare(
			r.exchange.name,
			r.exchange.kind,
			r.exchange.durable,
			r.exchange.autoDelete,
			r.exchange.internal,
			r.exchange.noWait,
			nil,
		)
		if err != nil {
			channel.Close()
			conn.Close()
			backoff = exp(backoff)
			r.logger.Errorf("failed to declare exchange in AMQP, will retry in: %f seconds, error: %s", backoff, err)
			continue
		}

		backoff = 0.0

		select {
		case out <- session{conn, channel}:
			r.logger.Infof("connected to AMQP")
			return
		case <-r.context.Done():
			r.logger.Infof("shutting down reconnector")
			return
		}
	}
}
<|endoftext|>


<commit_before>// Copyright (c) 2014, Google Inc. All rights reserved.
//
// 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 main

import (
	"bytes"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"errors"
	"flag"
	"log"
	"math/big"
	"net"
	"time"

	"github.com/golang/protobuf/proto"
	"github.com/jlmucb/cloudproxy/go/support_libraries/domain_policy"
	"github.com/jlmucb/cloudproxy/go/tao"
	"github.com/jlmucb/cloudproxy/go/tao/auth"
	"github.com/jlmucb/cloudproxy/go/util"
)

var network = flag.String("network", "tcp", "The network to use for connections")
var addr = flag.String("addr", "localhost:8124", "The address to listen on")
var domainPass = flag.String("password", "xxx", "The domain password")
var configPath = flag.String("config",
	"/Domains/domain.simpleexample/tao.config", "The Tao domain config")
var servicePath = flag.String("service_path",
	"/Domains/domain.simpleexample/SimpleDomainService", "The Tao domain config")

var SerialNumber int64

func IsAuthenticationValid(name *string) bool {
	log.Printf("simpledomainservice, IsAuthenticationValid name is %s\n", *name)
	if name == nil {
		return false
	}
	log.Printf("simpledomainservice, IsAuthenticationValid returning true\n")
	return true
}

// First return is terminate flag.
func DomainRequest(conn net.Conn, policyKey *tao.Keys, guard tao.Guard) (bool, error) {
	log.Printf("DomainRequest\n")

	// Expect a request with attestation from client.
	ms := util.NewMessageStream(conn)
	var request domain_policy.DomainCertRequest
	err := ms.ReadMessage(&request)
	if err != nil {
		log.Printf("DomainRequest: Couldn't read attestation from channel. Error : %v", err)
		log.Printf("\n")
		return false, err
	}

	var a tao.Attestation
	err = proto.Unmarshal(request.Attestation, &a)
	if request.KeyType == nil {
		log.Printf("Domain: Empty key type")
		return false, errors.New("Empty key type")
	}

	switch *request.KeyType {
	case "ecdsap256-public", "ecdsap384-public", "ecdsap521-public",
		"ecdsap256", "ecdsap384", "ecdsap521":
		break
	default:
		log.Printf("Domain: bad key type")
		return false, errors.New("Domain: bad key type")
	}

	subjectVerifier, err := tao.VerifierKeyFromCanonicalKeyBytes(request.SubjectPublicKey)
	if err != nil {
		log.Printf("DomainRequest: can't get key from der")
		return false, errors.New("DomainRequest: can't get key from der")
	}
	subjectKeyHash, err := subjectVerifier.UniversalKeyNameFromVerifier()
	if err != nil {
		log.Printf("SimpleDomain DomainRequest: calculate universal name")
		return false, errors.New("SimpleDomain DomainRequest: can't get key from der")
	}
	peerCert := conn.(*tls.Conn).ConnectionState().PeerCertificates[0]
	log.Printf("Peer Cert: %x\n", peerCert)
	// TODO(jlm): Change this.
	err = tao.ValidatePeerAttestation(&a, peerCert, guard)
	/*
	   if err != nil {
	       log.Printf("Domain: RequestCouldn't validate peer attestation:", err)
	   		 return false, err
	   }
	   fmt.Printf("DomainRequest, peerCert: %x\n", peerCert)
	*/

	// Sign cert

	// Get Program name and key info from delegation.
	f, err := auth.UnmarshalForm(a.SerializedStatement)
	if err != nil {
		log.Printf("DomainRequest: Can't unmarshal a.SerializedStatement\n")
		return false, err
	}

	var saysStatement *auth.Says
	if ptr, ok := f.(*auth.Says); ok {
		saysStatement = ptr
	} else if val, ok := f.(auth.Says); ok {
		saysStatement = &val
	}
	sf, ok := saysStatement.Message.(auth.Speaksfor)
	if ok != true {
		log.Printf("DomainRequest: says doesnt have speaksfor message\n")
		return false, err
	}
	// this in the new key principal
	clientKeyPrincipal, ok := sf.Delegate.(auth.Prin)
	if ok != true {
		log.Printf("DomainRequest: speaksfor Delegate is not auth.Prin\n")
		return false, err
	}

	programPrincipal, ok := sf.Delegator.(auth.Prin)
	if ok != true {
		log.Printf("DomainRequest: Can't get subject principal\n")
		return false, errors.New("Can't get principal name from verifier")
	}
	programPrincipalName := programPrincipal.String()
	verified := IsAuthenticationValid(&programPrincipalName)
	if !verified {
		log.Printf("DomainRequest: name verification failed\n")
		return false, err
	}
	log.Printf("SimpleDomainService: key principal: %s, program principal: %s\n", clientKeyPrincipal, programPrincipalName)

	// Is the delegate the same key as was presented in the name in the request?
	namedHash := clientKeyPrincipal.KeyHash.(auth.Bytes)
	log.Printf("keyhash: %x\n", namedHash)
	if bytes.Compare(subjectKeyHash[:], namedHash) != 0 {
		log.Printf("DomainRequest: named hash is wrong, named: %x, computed: %x\n",
			namedHash, subjectKeyHash)
		return false, errors.New("DomainRequest: named hash is wrong")
	}

	// Sign program certificate.

	notBefore := time.Now()
	validFor := 365 * 24 * time.Hour
	notAfter := notBefore.Add(validFor)

	us := "US"
	issuerName := "Google"
	localhost := "localhost"
	x509SubjectName := &pkix.Name{
		Organization:       []string{programPrincipalName},
		OrganizationalUnit: []string{programPrincipalName},
		CommonName:         localhost,
		Country:            []string{us},
	}
	x509IssuerName := &pkix.Name{
		Organization:       []string{issuerName},
		OrganizationalUnit: []string{issuerName},
		CommonName:         localhost,
		Country:            []string{us},
	}

	// issuerName := tao.NewX509Name(&details)
	SerialNumber = SerialNumber + 1
	var sn big.Int
	certificateTemplate := &x509.Certificate{
		SerialNumber: &sn,

		Issuer:    *x509IssuerName,
		Subject:   *x509SubjectName,
		NotBefore: notBefore,
		NotAfter:  notAfter,
		KeyUsage:  x509.KeyUsageCertSign | x509.KeyUsageKeyAgreement | x509.KeyUsageDigitalSignature,
	}

	// FIX: pass certificate template
	signerAlg := tao.SignerTypeFromSuiteName(tao.TaoCryptoSuite)
	if signerAlg == nil {
		return false, errors.New("Bad suite")
	}
	pkAlg := tao.PublicKeyAlgFromSignerAlg(*signerAlg)
	sigAlg := tao.SignatureAlgFromSignerAlg(*signerAlg)
	if pkAlg < 0 || sigAlg < 0 {
		return false, errors.New("Bad signing algs")
	}
	clientCert, err := policyKey.SigningKey.CreateSignedX509FromTemplate(policyKey.Cert, certificateTemplate,
		subjectVerifier, pkAlg, sigAlg)

	zero := int32(0)
	var ra domain_policy.DomainCertResponse
	ra.Error = &zero
	ra.SignedCert = clientCert.Raw

	// Add cert chain (just policy cert for now).
	ra.CertChain = append(ra.CertChain, policyKey.Cert.Raw)

	_, err = ms.WriteMessage(&ra)
	if err != nil {
		log.Printf("DomainRequest: Couldn't return the attestation on the channel. Error: %v", err)
		log.Printf("\n")
		return false, err
	}
	return false, nil
}

func main() {
	flag.Parse()
	domain, err := tao.LoadDomain(*configPath, []byte(*domainPass))
	if domain == nil {
		log.Printf("simpledomainservice: no domain path - %s, pass - %s, err - %s\n",
			*configPath, *domainPass, err)
		return
	} else if err != nil {
		log.Printf("simpledomainservice: Couldn't load the config path %s: %s\n",
			*configPath, err)
		return
	}
	log.Printf("simpledomainservice: Loaded domain\n")

	// Set up temporary keys for the connection, since the only thing that
	// matters to the remote client is that they receive a correctly-signed new
	// attestation from the policy key.
	// JLM:  I left this in place but I'm not sure what a TLS connection with a
	//   self signed Cert buys in terms of security.
	//   The security of this protocol should not depend on the
	//   confidentiality or intergity of the channel.  All that said, if we
	//   do ever distribute a signed simpledomainservice cert
	// for this TLS channel, it would be good.
	keys, err := tao.NewTemporaryKeys(tao.Signing)
	if keys == nil || err != nil {
		log.Fatalln("simpledomainservice: Couldn't set up temporary keys for connection:", err)
		return
	}
	pkAlg := tao.PublicKeyAlgFromSignerAlg(*keys.SigningKey.Header.KeyType)
	sigAlg := tao.SignatureAlgFromSignerAlg(*keys.SigningKey.Header.KeyType)
	keys.Cert, err = keys.SigningKey.CreateSelfSignedX509(pkAlg, sigAlg, int64(1),
		&pkix.Name{
			Organization: []string{"Google Tao Demo"}})
	if err != nil {
		log.Fatalln("simpledomainservice: Couldn't set up a self-signed cert:", err)
		return
	}
	SerialNumber = int64(time.Now().UnixNano()) / (1000000)
	policyKey := domain.Keys
	log.Printf("SimpleDomainService: policyKey: %v\n", policyKey)

	tlsc, err := tao.EncodeTLSCert(keys)
	if err != nil {
		log.Fatalln("simpledomainservice: Couldn't encode a TLS cert:", err)
	}
	conf := &tls.Config{
		RootCAs:            x509.NewCertPool(),
		Certificates:       []tls.Certificate{*tlsc},
		InsecureSkipVerify: true,
		ClientAuth:         tls.RequireAnyClientCert,
	}
	sock, err := tls.Listen(*network, *addr, conf)
	if err != nil {
		log.Printf("simpledomainservice: error: %s\n", err)
	}
	if sock == nil {
		log.Printf("simpledomainservice: Empty socket, terminating\n")
		return
	}
	defer sock.Close()

	log.Printf("simpledomainservice: accepting connections\n")
	for {
		conn, err := sock.Accept()
		if conn == nil {
			log.Printf("simpledomainservice: Empty connection\n")
			return
		} else if err != nil {
			log.Printf("simpledomainservice: Couldn't accept a connection on %s: %s\n", *addr, err)
			return
		}
		go DomainRequest(conn, policyKey, domain.Guard)
	}
	log.Println("simpledomainservice: finishing")
}
<commit_msg>log signing operation<commit_after>// Copyright (c) 2014, Google Inc. All rights reserved.
//
// 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 main

import (
	"bytes"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"errors"
	"flag"
	"log"
	"math/big"
	"net"
	"time"

	"github.com/golang/protobuf/proto"
	"github.com/jlmucb/cloudproxy/go/support_libraries/domain_policy"
	"github.com/jlmucb/cloudproxy/go/tao"
	"github.com/jlmucb/cloudproxy/go/tao/auth"
	"github.com/jlmucb/cloudproxy/go/util"
)

var network = flag.String("network", "tcp", "The network to use for connections")
var addr = flag.String("addr", "localhost:8124", "The address to listen on")
var domainPass = flag.String("password", "xxx", "The domain password")
var configPath = flag.String("config",
	"/Domains/domain.simpleexample/tao.config", "The Tao domain config")
var servicePath = flag.String("service_path",
	"/Domains/domain.simpleexample/SimpleDomainService", "The Tao domain config")

var SerialNumber int64

func IsAuthenticationValid(name *string) bool {
	log.Printf("simpledomainservice, IsAuthenticationValid name is %s\n", *name)
	if name == nil {
		return false
	}
	log.Printf("simpledomainservice, IsAuthenticationValid returning true\n")
	return true
}

// First return is terminate flag.
func DomainRequest(conn net.Conn, policyKey *tao.Keys, guard tao.Guard) (bool, error) {
	log.Printf("DomainRequest\n")

	// Expect a request with attestation from client.
	ms := util.NewMessageStream(conn)
	var request domain_policy.DomainCertRequest
	err := ms.ReadMessage(&request)
	if err != nil {
		log.Printf("DomainRequest: Couldn't read attestation from channel. Error : %v", err)
		log.Printf("\n")
		return false, err
	}

	var a tao.Attestation
	err = proto.Unmarshal(request.Attestation, &a)
	if request.KeyType == nil {
		log.Printf("Domain: Empty key type")
		return false, errors.New("Empty key type")
	}

	switch *request.KeyType {
	case "ecdsap256-public", "ecdsap384-public", "ecdsap521-public",
		"ecdsap256", "ecdsap384", "ecdsap521":
		break
	default:
		log.Printf("Domain: bad key type")
		return false, errors.New("Domain: bad key type")
	}

	subjectVerifier, err := tao.VerifierKeyFromCanonicalKeyBytes(request.SubjectPublicKey)
	if err != nil {
		log.Printf("DomainRequest: can't get key from der")
		return false, errors.New("DomainRequest: can't get key from der")
	}
	subjectKeyHash, err := subjectVerifier.UniversalKeyNameFromVerifier()
	if err != nil {
		log.Printf("SimpleDomain DomainRequest: calculate universal name")
		return false, errors.New("SimpleDomain DomainRequest: can't get key from der")
	}
	peerCert := conn.(*tls.Conn).ConnectionState().PeerCertificates[0]
	log.Printf("\n")
	log.Printf("\nNumber of peer certs: %d\n", len(conn.(*tls.Conn).ConnectionState().PeerCertificates))
	for i := 0; i < len(conn.(*tls.Conn).ConnectionState().PeerCertificates); i++ {
		tao.PrintPKIXName("Issuer", &conn.(*tls.Conn).ConnectionState().PeerCertificates[i].Issuer)
		tao.PrintPKIXName("Subject", &conn.(*tls.Conn).ConnectionState().PeerCertificates[i].Subject)
		log.Printf("\n")
	}
	// TODO(jlm): Change this.
	err = tao.ValidatePeerAttestation(&a, peerCert, guard)
	/*
	   if err != nil {
	       log.Printf("Domain: RequestCouldn't validate peer attestation:", err)
	   		 return false, err
	   }
	   fmt.Printf("DomainRequest, peerCert: %x\n", peerCert)
	*/

	// Sign cert

	// Get Program name and key info from delegation.
	f, err := auth.UnmarshalForm(a.SerializedStatement)
	if err != nil {
		log.Printf("DomainRequest: Can't unmarshal a.SerializedStatement\n")
		return false, err
	}

	var saysStatement *auth.Says
	if ptr, ok := f.(*auth.Says); ok {
		saysStatement = ptr
	} else if val, ok := f.(auth.Says); ok {
		saysStatement = &val
	}
	sf, ok := saysStatement.Message.(auth.Speaksfor)
	if ok != true {
		log.Printf("DomainRequest: says doesnt have speaksfor message\n")
		return false, err
	}
	// this in the new key principal
	clientKeyPrincipal, ok := sf.Delegate.(auth.Prin)
	if ok != true {
		log.Printf("DomainRequest: speaksfor Delegate is not auth.Prin\n")
		return false, err
	}

	programPrincipal, ok := sf.Delegator.(auth.Prin)
	if ok != true {
		log.Printf("DomainRequest: Can't get subject principal\n")
		return false, errors.New("Can't get principal name from verifier")
	}
	programPrincipalName := programPrincipal.String()
	verified := IsAuthenticationValid(&programPrincipalName)
	if !verified {
		log.Printf("DomainRequest: name verification failed\n")
		return false, err
	}
	log.Printf("SimpleDomainService: key principal: %s, program principal: %s\n", clientKeyPrincipal, programPrincipalName)

	// Is the delegate the same key as was presented in the name in the request?
	namedHash := clientKeyPrincipal.KeyHash.(auth.Bytes)
	log.Printf("keyhash: %x\n", namedHash)
	if bytes.Compare(subjectKeyHash[:], namedHash) != 0 {
		log.Printf("DomainRequest: named hash is wrong, named: %x, computed: %x\n",
			namedHash, subjectKeyHash)
		return false, errors.New("DomainRequest: named hash is wrong")
	}

	// Sign program certificate.

	notBefore := time.Now()
	validFor := 365 * 24 * time.Hour
	notAfter := notBefore.Add(validFor)

	us := "US"
	issuerName := "Google"
	localhost := "localhost"
	x509SubjectName := &pkix.Name{
		Organization:       []string{programPrincipalName},
		OrganizationalUnit: []string{programPrincipalName},
		CommonName:         localhost,
		Country:            []string{us},
	}
	x509IssuerName := &pkix.Name{
		Organization:       []string{issuerName},
		OrganizationalUnit: []string{issuerName},
		CommonName:         localhost,
		Country:            []string{us},
	}

	log.Printf("Signing cert for ")
	tao.PrintPKIXName("Subject", x509SubjectName)
	log.Printf(" with \n")
	tao.PrintPKIXName("Issuer", x509IssuerName)
	log.Printf("\n")

	// issuerName := tao.NewX509Name(&details)
	SerialNumber = SerialNumber + 1
	var sn big.Int
	certificateTemplate := &x509.Certificate{
		SerialNumber: &sn,

		Issuer:    *x509IssuerName,
		Subject:   *x509SubjectName,
		NotBefore: notBefore,
		NotAfter:  notAfter,
		KeyUsage:  x509.KeyUsageCertSign | x509.KeyUsageKeyAgreement | x509.KeyUsageDigitalSignature,
	}

	// FIX: pass certificate template
	signerAlg := tao.SignerTypeFromSuiteName(tao.TaoCryptoSuite)
	if signerAlg == nil {
		return false, errors.New("Bad suite")
	}
	pkAlg := tao.PublicKeyAlgFromSignerAlg(*signerAlg)
	sigAlg := tao.SignatureAlgFromSignerAlg(*signerAlg)
	if pkAlg < 0 || sigAlg < 0 {
		return false, errors.New("Bad signing algs")
	}
	clientCert, err := policyKey.SigningKey.CreateSignedX509FromTemplate(policyKey.Cert, certificateTemplate,
		subjectVerifier, pkAlg, sigAlg)

	zero := int32(0)
	var ra domain_policy.DomainCertResponse
	ra.Error = &zero
	ra.SignedCert = clientCert.Raw

	// Add cert chain (just policy cert for now).
	ra.CertChain = append(ra.CertChain, policyKey.Cert.Raw)

	_, err = ms.WriteMessage(&ra)
	if err != nil {
		log.Printf("DomainRequest: Couldn't return the attestation on the channel. Error: %v", err)
		log.Printf("\n")
		return false, err
	}
	return false, nil
}

func main() {
	flag.Parse()
	domain, err := tao.LoadDomain(*configPath, []byte(*domainPass))
	if domain == nil {
		log.Printf("simpledomainservice: no domain path - %s, pass - %s, err - %s\n",
			*configPath, *domainPass, err)
		return
	} else if err != nil {
		log.Printf("simpledomainservice: Couldn't load the config path %s: %s\n",
			*configPath, err)
		return
	}
	log.Printf("simpledomainservice: Loaded domain\n")

	// Set up temporary keys for the connection, since the only thing that
	// matters to the remote client is that they receive a correctly-signed new
	// attestation from the policy key.
	// JLM:  I left this in place but I'm not sure what a TLS connection with a
	//   self signed Cert buys in terms of security.
	//   The security of this protocol should not depend on the
	//   confidentiality or intergity of the channel.  All that said, if we
	//   do ever distribute a signed simpledomainservice cert
	// for this TLS channel, it would be good.
	keys, err := tao.NewTemporaryKeys(tao.Signing)
	if keys == nil || err != nil {
		log.Fatalln("simpledomainservice: Couldn't set up temporary keys for connection:", err)
		return
	}
	pkAlg := tao.PublicKeyAlgFromSignerAlg(*keys.SigningKey.Header.KeyType)
	sigAlg := tao.SignatureAlgFromSignerAlg(*keys.SigningKey.Header.KeyType)
	keys.Cert, err = keys.SigningKey.CreateSelfSignedX509(pkAlg, sigAlg, int64(1),
		&pkix.Name{
			Organization: []string{"Google Tao Demo"}})
	if err != nil {
		log.Fatalln("simpledomainservice: Couldn't set up a self-signed cert:", err)
		return
	}
	SerialNumber = int64(time.Now().UnixNano()) / (1000000)
	policyKey := domain.Keys
	log.Printf("SimpleDomainService: policyKey: %v\n", policyKey)

	tlsc, err := tao.EncodeTLSCert(keys)
	if err != nil {
		log.Fatalln("simpledomainservice: Couldn't encode a TLS cert:", err)
	}
	conf := &tls.Config{
		RootCAs:            x509.NewCertPool(),
		Certificates:       []tls.Certificate{*tlsc},
		InsecureSkipVerify: true,
		ClientAuth:         tls.RequireAnyClientCert,
	}
	sock, err := tls.Listen(*network, *addr, conf)
	if err != nil {
		log.Printf("simpledomainservice: error: %s\n", err)
	}
	if sock == nil {
		log.Printf("simpledomainservice: Empty socket, terminating\n")
		return
	}
	defer sock.Close()

	log.Printf("simpledomainservice: accepting connections\n")
	for {
		conn, err := sock.Accept()
		if conn == nil {
			log.Printf("simpledomainservice: Empty connection\n")
			return
		} else if err != nil {
			log.Printf("simpledomainservice: Couldn't accept a connection on %s: %s\n", *addr, err)
			return
		}
		go DomainRequest(conn, policyKey, domain.Guard)
	}
	log.Println("simpledomainservice: finishing")
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/sagemaker"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsSagemakerModel() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsSagemakerModelCreate,
		Read:   resourceAwsSagemakerModelRead,
		Update: resourceAwsSagemakerModelUpdate,
		Delete: resourceAwsSagemakerModelDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"arn": {
				Type:     schema.TypeString,
				Computed: true,
			},

			"name": {
				Type:         schema.TypeString,
				Optional:     true,
				Computed:     true,
				ForceNew:     true,
				ValidateFunc: validateSagemakerName,
			},

			"primary_container": {
				Type:     schema.TypeList,
				MaxItems: 1,
				Required: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"container_hostname": {
							Type:         schema.TypeString,
							Optional:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerName,
						},

						"image": {
							Type:         schema.TypeString,
							Required:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerImage,
						},

						"model_data_url": {
							Type:         schema.TypeString,
							Optional:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerModelDataUrl,
						},

						"environment": {
							Type:         schema.TypeMap,
							Optional:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerEnvironment,
						},
					},
				},
			},

			"execution_role_arn": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},

			"tags": tagsSchema(),
		},
	}
}

func resourceAwsSagemakerModelCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	var name string
	if v, ok := d.GetOk("name"); ok {
		name = v.(string)
	} else {
		name = resource.UniqueId()
	}

	createOpts := &sagemaker.CreateModelInput{
		ModelName: aws.String(name),
	}

	pContainer := d.Get("primary_container").([]interface{})
	m := pContainer[0].(map[string]interface{})
	createOpts.PrimaryContainer = expandPrimaryContainers(m)

	if v, ok := d.GetOk("execution_role_arn"); ok {
		createOpts.ExecutionRoleArn = aws.String(v.(string))
	}

	if v, ok := d.GetOk("tags"); ok {
		createOpts.Tags = tagsFromMapSagemaker(v.(map[string]interface{}))
	}

	log.Printf("[DEBUG] Sagemaker model create config: %#v", *createOpts)
	_, err := retryOnAwsCode("ValidationException", func() (interface{}, error) {
		return conn.CreateModel(createOpts)
	})

	if err != nil {
		return fmt.Errorf("error creating Sagemaker model: %s", err)
	}

	return resourceAwsSagemakerModelRead(d, meta)
}

func resourceAwsSagemakerModelRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	request := &sagemaker.DescribeModelInput{
		ModelName: aws.String(d.Id()),
	}

	model, err := conn.DescribeModel(request)
	if err != nil {
		if sagemakerErr, ok := err.(awserr.Error); ok && sagemakerErr.Code() == "ResourceNotFound" {
			log.Printf("[INFO] unable to find the sagemaker resource and therfore it is removed from the state: %s", d.Id())
			d.SetId("")
			return nil
		}
		return fmt.Errorf("error reading Sagemaker model %s: %s", d.Id(), err)
	}

	if err := d.Set("arn", model.ModelArn); err != nil {
		return fmt.Errorf("unable to set arn for sagemaker model %q: %+v", d.Id(), err)
	}
	if err := d.Set("name", model.ModelName); err != nil {
		return err
	}
	if err := d.Set("execution_role_arn", model.ExecutionRoleArn); err != nil {
		return err
	}
	if err := d.Set("primary_container", flattenPrimaryContainer(model.PrimaryContainer)); err != nil {
		return err
	}

	tagsOutput, err := conn.ListTags(&sagemaker.ListTagsInput{
		ResourceArn: model.ModelArn,
	})
	if err := d.Set("tags", tagsToMapSagemaker(tagsOutput.Tags)); err != nil {
		return err
	}
	return nil
}

func resourceAwsSagemakerModelUpdate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	d.Partial(true)

	if err := setSagemakerTags(conn, d); err != nil {
		return err
	} else {
		d.SetPartial("tags")
	}

	d.Partial(false)

	return resourceAwsSagemakerModelRead(d, meta)
}

func resourceAwsSagemakerModelDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	deleteOpts := &sagemaker.DeleteModelInput{
		ModelName: aws.String(d.Id()),
	}
	log.Printf("[INFO] Deleting Sagemaker model: %s", d.Id())

	return resource.Retry(5*time.Minute, func() *resource.RetryError {
		_, err := conn.DeleteModel(deleteOpts)
		if err == nil {
			return nil
		}

		sagemakerErr, ok := err.(awserr.Error)
		if !ok {
			return resource.NonRetryableError(err)
		}

		if sagemakerErr.Code() == "ResourceNotFound" {
			return resource.RetryableError(err)
		}

		return resource.NonRetryableError(fmt.Errorf("error deleting Sagemaker model: %s", err))
	})
}

func expandPrimaryContainers(m map[string]interface{}) *sagemaker.ContainerDefinition {
	container := sagemaker.ContainerDefinition{
		Image: aws.String(m["image"].(string)),
	}

	if v, ok := m["container_hostname"]; ok && v.(string) != "" {
		container.ContainerHostname = aws.String(v.(string))
	}
	if v, ok := m["model_data_url"]; ok && v.(string) != "" {
		container.ModelDataUrl = aws.String(v.(string))
	}
	if v, ok := m["environment"]; ok {
		container.Environment = stringMapToPointers(v.(map[string]interface{}))
	}

	return &container
}

func flattenPrimaryContainer(container *sagemaker.ContainerDefinition) []interface{} {
	cfg := make(map[string]interface{}, 0)

	cfg["image"] = *container.Image

	if container.ContainerHostname != nil {
		cfg["container_hostname"] = *container.ContainerHostname
	}
	if container.ModelDataUrl != nil {
		cfg["model_data_url"] = *container.ModelDataUrl
	}
	if container.Environment != nil {
		cfg["environment"] = flattenEnvironment(container.Environment)
	}

	return []interface{}{cfg}
}

func flattenEnvironment(env map[string]*string) map[string]string {
	m := map[string]string{}
	for k, v := range env {
		m[k] = *v
	}
	return m
}
<commit_msg>Adding VPC config (#2)<commit_after>package aws

import (
	"fmt"
	"log"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/sagemaker"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsSagemakerModel() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsSagemakerModelCreate,
		Read:   resourceAwsSagemakerModelRead,
		Update: resourceAwsSagemakerModelUpdate,
		Delete: resourceAwsSagemakerModelDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"arn": {
				Type:     schema.TypeString,
				Computed: true,
			},

			"name": {
				Type:         schema.TypeString,
				Optional:     true,
				Computed:     true,
				ForceNew:     true,
				ValidateFunc: validateSagemakerName,
			},

			"primary_container": {
				Type:     schema.TypeList,
				MaxItems: 1,
				Required: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"container_hostname": {
							Type:         schema.TypeString,
							Optional:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerName,
						},

						"image": {
							Type:         schema.TypeString,
							Required:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerImage,
						},

						"model_data_url": {
							Type:         schema.TypeString,
							Optional:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerModelDataUrl,
						},

						"environment": {
							Type:         schema.TypeMap,
							Optional:     true,
							ForceNew:     true,
							ValidateFunc: validateSagemakerEnvironment,
						},
					},
				},
			},

			"vpc_config": {
				Type:     schema.TypeList,
				Optional: true,
				MaxItems: 1,
				ForceNew: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"subnets": {
							Type:     schema.TypeSet,
							Required: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
						},
						"security_group_ids": {
							Type:     schema.TypeSet,
							Required: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
						},
					},
				},
			},

			"execution_role_arn": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},

			"tags": tagsSchema(),
		},
	}
}

func resourceAwsSagemakerModelCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	var name string
	if v, ok := d.GetOk("name"); ok {
		name = v.(string)
	} else {
		name = resource.UniqueId()
	}

	createOpts := &sagemaker.CreateModelInput{
		ModelName: aws.String(name),
	}

	pContainer := d.Get("primary_container").([]interface{})
	m := pContainer[0].(map[string]interface{})
	createOpts.PrimaryContainer = expandPrimaryContainers(m)

	if v, ok := d.GetOk("execution_role_arn"); ok {
		createOpts.ExecutionRoleArn = aws.String(v.(string))
	}

	if v, ok := d.GetOk("tags"); ok {
		createOpts.Tags = tagsFromMapSagemaker(v.(map[string]interface{}))
	}

	if v, ok := d.GetOk("vpc_config"); ok {
		vpcConfig := expandSageMakerVpcConfigRequest(v.([]interface{}))
		createOpts.SetVpcConfig(vpcConfig)
	}

	log.Printf("[DEBUG] Sagemaker model create config: %#v", *createOpts)
	_, err := retryOnAwsCode("ValidationException", func() (interface{}, error) {
		return conn.CreateModel(createOpts)
	})

	if err != nil {
		return fmt.Errorf("error creating Sagemaker model: %s", err)
	}

	return resourceAwsSagemakerModelRead(d, meta)
}

func expandSageMakerVpcConfigRequest(l []interface{}) *sagemaker.VpcConfig {
	if len(l) == 0 {
		return nil
	}

	m := l[0].(map[string]interface{})

	return &sagemaker.VpcConfig{
		SecurityGroupIds: expandStringSet(m["security_group_ids"].(*schema.Set)),
		Subnets:          expandStringSet(m["subnets"].(*schema.Set)),
	}
}

func resourceAwsSagemakerModelRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	request := &sagemaker.DescribeModelInput{
		ModelName: aws.String(d.Id()),
	}

	model, err := conn.DescribeModel(request)
	if err != nil {
		if sagemakerErr, ok := err.(awserr.Error); ok && sagemakerErr.Code() == "ResourceNotFound" {
			log.Printf("[INFO] unable to find the sagemaker resource and therfore it is removed from the state: %s", d.Id())
			d.SetId("")
			return nil
		}
		return fmt.Errorf("error reading Sagemaker model %s: %s", d.Id(), err)
	}

	if err := d.Set("arn", model.ModelArn); err != nil {
		return fmt.Errorf("unable to set arn for sagemaker model %q: %+v", d.Id(), err)
	}
	if err := d.Set("name", model.ModelName); err != nil {
		return err
	}
	if err := d.Set("execution_role_arn", model.ExecutionRoleArn); err != nil {
		return err
	}
	if err := d.Set("primary_container", flattenPrimaryContainer(model.PrimaryContainer)); err != nil {
		return err
	}
	if err := d.Set("vpc_config", flattenSageMakerVpcConfigResponse(model.VpcConfig)); err != nil {
		return fmt.Errorf("error setting vpc_config: %s", err)
	}

	tagsOutput, err := conn.ListTags(&sagemaker.ListTagsInput{
		ResourceArn: model.ModelArn,
	})
	if err := d.Set("tags", tagsToMapSagemaker(tagsOutput.Tags)); err != nil {
		return err
	}
	return nil
}

func flattenSageMakerVpcConfigResponse(vpcConfig *sagemaker.VpcConfig) []map[string]interface{} {
	if vpcConfig == nil {
		return []map[string]interface{}{}
	}

	m := map[string]interface{}{
		"security_group_ids": schema.NewSet(schema.HashString, flattenStringList(vpcConfig.SecurityGroupIds)),
		"subnets":            schema.NewSet(schema.HashString, flattenStringList(vpcConfig.Subnets)),
	}

	return []map[string]interface{}{m}
}

func resourceAwsSagemakerModelUpdate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	d.Partial(true)

	if err := setSagemakerTags(conn, d); err != nil {
		return err
	} else {
		d.SetPartial("tags")
	}

	d.Partial(false)

	return resourceAwsSagemakerModelRead(d, meta)
}

func resourceAwsSagemakerModelDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).sagemakerconn

	deleteOpts := &sagemaker.DeleteModelInput{
		ModelName: aws.String(d.Id()),
	}
	log.Printf("[INFO] Deleting Sagemaker model: %s", d.Id())

	return resource.Retry(5*time.Minute, func() *resource.RetryError {
		_, err := conn.DeleteModel(deleteOpts)
		if err == nil {
			return nil
		}

		sagemakerErr, ok := err.(awserr.Error)
		if !ok {
			return resource.NonRetryableError(err)
		}

		if sagemakerErr.Code() == "ResourceNotFound" {
			return resource.RetryableError(err)
		}

		return resource.NonRetryableError(fmt.Errorf("error deleting Sagemaker model: %s", err))
	})
}

func expandPrimaryContainers(m map[string]interface{}) *sagemaker.ContainerDefinition {
	container := sagemaker.ContainerDefinition{
		Image: aws.String(m["image"].(string)),
	}

	if v, ok := m["container_hostname"]; ok && v.(string) != "" {
		container.ContainerHostname = aws.String(v.(string))
	}
	if v, ok := m["model_data_url"]; ok && v.(string) != "" {
		container.ModelDataUrl = aws.String(v.(string))
	}
	if v, ok := m["environment"]; ok {
		container.Environment = stringMapToPointers(v.(map[string]interface{}))
	}

	return &container
}

func flattenPrimaryContainer(container *sagemaker.ContainerDefinition) []interface{} {
	cfg := make(map[string]interface{}, 0)

	cfg["image"] = *container.Image

	if container.ContainerHostname != nil {
		cfg["container_hostname"] = *container.ContainerHostname
	}
	if container.ModelDataUrl != nil {
		cfg["model_data_url"] = *container.ModelDataUrl
	}
	if container.Environment != nil {
		cfg["environment"] = flattenEnvironment(container.Environment)
	}

	return []interface{}{cfg}
}

func flattenEnvironment(env map[string]*string) map[string]string {
	m := map[string]string{}
	for k, v := range env {
		m[k] = *v
	}
	return m
}
<|endoftext|>


<commit_before>package medias

import (
	"testing"
	"os"
	"fmt"
)

func TestCreateSaveFileIfNotExist(t *testing.T) {
	dir := "data_test"
	file := "media.config.test"

	//if test folder already exists, delete it
	if _, err := os.Stat(dir); err == nil {
		err = os.Remove(dir)
	}

	var fullPath string = fmt.Sprintf("%s%c%s", dir, os.PathSeparator, file)
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		CreateSaveFileIfNotExist(dir, file)

		if _, err := os.Stat(fullPath); os.IsNotExist(err) {
			t.Error(err)
		}

		os.Remove(dir)
	}

}
<commit_msg>[Fix] I definitely have to add some ...<commit_after>package medias

import (
	/*"testing"
	"os"
	"fmt"*/
)

/*func TestCreateSaveFileIfNotExist(t *testing.T) {
	dir := "data_test"
	file := "media.config.test"

	//if test folder already exists, delete it
	if _, err := os.Stat(dir); err == nil {
		err = os.Remove(dir)
	}

	var fullPath string = fmt.Sprintf("%s%c%s", dir, os.PathSeparator, file)
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		mediaManager := NewMediaManager()
		mediaManager.CreateSaveFileIfNotExist(dir, file)

		if _, err := os.Stat(fullPath); os.IsNotExist(err) {
			t.Error(err)
		}

		os.Remove(dir)
	}

	t.Logf("ok")
}
*/<|endoftext|>


<commit_before>package rest

import "strings"

// Sanatize ...
func (c *Client) Sanatize(s string) string {
	if strings.LastIndex(s, "/") > 0 {
		s = strings.Trim(s, "/")
	}
	return s
}

// IsEmpty ...
func IsEmpty(s string) bool {
	if s == "" || len(strings.TrimSpace(s)) == 0 {
		return true
	}
	return false
}
<commit_msg>added assignment golang spec ref...<commit_after>package rest

import "strings"

// Sanatize ...
func (c *Client) Sanatize(s string) string {
	if strings.LastIndex(s, "/") > 0 {
		s = strings.Trim(s, "/")
	}
	return s
}

// IsEmpty ...
// see http://golang.org/ref/spec#Assignability
func IsEmpty(s string) bool {
	if s == "" || len(strings.TrimSpace(s)) == 0 {
		return true
	}
	return false
}
<|endoftext|>