<commit_before>// Copyright 2013 gopm authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package cmd

import (
	serrors "errors"
	"fmt"
	"github.com/Unknwon/com"
	"github.com/gpmgo/gopm/doc"
	"github.com/syndtr/goleveldb/leveldb"
	"github.com/syndtr/goleveldb/leveldb/errors"
	"github.com/syndtr/goleveldb/leveldb/opt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
)

var (
	dbDir = "~/.gopm/db"
)

const (
	STOP = iota
	LOCALRUN
	RUNNING
)

var CmdServe = &Command{
	UsageLine: "serve [:port]",
	Short:     "serve for package search",
	Long: `
	serve provide a web service to search packages, download packages

The serve flags are:

	-l
		only service for localhost ip
`,
}

func init() {
	CmdServe.Run = runServe
	CmdServe.Flags = map[string]bool{
		"-l": false,
	}
}

func printServePrompt(flag string) {
	switch flag {
	case "-l":
		com.ColorLog("[INFO] You enabled start a service only localhost.\n")
	}
}

// Not implemented
func autoPort() string {
	return "8991"
}

func exePath() (string, error) {
	file, err := exec.LookPath(os.Args[0])
	if err != nil {
		return "", err
	}

	return filepath.Abs(file)
}

// search packages
func runServe(cmd *Command, args []string) {
	// Check flags.
	num := checkFlags(cmd.Flags, args, printServePrompt)
	if num == -1 {
		return
	}
	args = args[num:]

	var listen string
	var port string
	if cmd.Flags["-l"] {
		listen += "127.0.0.1"
		port = autoPort()
	} else {
		listen += "0.0.0.0"
		port = "8991"
	}

	// Check length of arguments.
	if len(args) >= 1 {
		port = args[0]
	}

	startService(listen, port)
}

func splitWord(word string, res *map[string]bool) {
	for i, _ := range word {
		for j, _ := range word[i:] {
			w := word[i : i+j+1]
			(*res)[w] = true
		}
	}
	return
}

func splitPkgName(pkgName string) (res map[string]bool) {
	//var src string
	ps := strings.Split(pkgName, "/")
	if len(ps) > 1 {
		ps = ps[1:]
	}

	res = make(map[string]bool, 0)
	res[strings.Join(ps, "/")] = true
	for _, w := range ps {
		splitWord(w, &res)
	}
	return
}

var (
	ro *opt.ReadOptions  = &opt.ReadOptions{}
	wo *opt.WriteOptions = &opt.WriteOptions{}
)

func dbGet(key string) (string, error) {
	v, err := db.Get([]byte(key), ro)
	return string(v), err
}

func dbPut(key string, value string) error {
	fmt.Println("put ", key, ": ", value)
	return db.Put([]byte(key), []byte(value), wo)
}

func batchPut(batch *leveldb.Batch, key string, value string) error {
	fmt.Println("put ", key, ": ", value)
	batch.Put([]byte(key), []byte(value))
	return nil
}

func getServeHost() string {
	return "localhost"
}

func getServePort() string {
	return "8991"
}

// for exernal of serve to add node to db
func saveNode(nod *doc.Node) error {
	urlPath := fmt.Sprintf("http://%v:%v/add", getServeHost(), getServePort())
	resp, err := http.PostForm(urlPath,
		url.Values{"importPath": {nod.ImportPath},
			"synopsis":    {nod.Synopsis},
			"downloadURL": {nod.DownloadURL},
			"isGetDeps":   {strconv.FormatBool(nod.IsGetDeps)},
			"type":        {nod.Type},
			"value":       {nod.Value}})

	if err != nil {
		com.ColorLog("[ERRO] Fail to save node[ %s ]\n", err)
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		return nil
	}
	return serrors.New("save node failed with " + resp.Status)
}

// for inetrnal of serve to add node to db
func addNode(nod *doc.Node) error {
	batch := new(leveldb.Batch)
	strLastId, err := dbGet("lastId")
	if err != nil {
		if err == errors.ErrNotFound {
			strLastId = "0"
			err = batchPut(batch, "lastId", strLastId)
		} else {
			return err
		}
	}
	if err != nil {
		return err
	}

	lastId, err := strconv.ParseInt(strLastId, 0, 64)
	if err != nil {
		return err
	}

	nodKey := fmt.Sprintf("index:%v", nod.ImportPath)

	id, err := dbGet(nodKey)
	if err != nil {
		if err == errors.ErrNotFound {
			id = fmt.Sprintf("%v", lastId+1)
			err = batchPut(batch, "lastId", id)
			if err == nil {
				err = batchPut(batch, nodKey, id)
			}
			if err == nil {
				err = batchPut(batch, "pkg:"+id, nod.ImportPath)
			}
			if err == nil {
				err = batchPut(batch, "desc:"+id, nod.Synopsis)
			}
			if err == nil {
				err = batchPut(batch, "down:"+id, nod.DownloadURL)
			}
			if err == nil {
				err = batchPut(batch, "deps:"+id, strconv.FormatBool(nod.IsGetDeps))
			}

			// save totals
			total, err := dbGet("total")
			if err != nil {
				if err == errors.ErrNotFound {
					total = "1"
				} else {
					return err
				}
			} else {
				totalInt, err := strconv.ParseInt(total, 0, 64)
				if err != nil {
					return err
				}
				totalInt = totalInt + 1
				total = fmt.Sprintf("%v", totalInt)
			}

			err = batchPut(batch, "total", total)
		} else {
			return err
		}
	}

	if err != nil {
		return err
	}

	// save vers
	vers, err := dbGet("ver:" + id)
	needSplit := (err == errors.ErrNotFound)
	if err != nil {
		if err != errors.ErrNotFound {
			return err
		}
	} else {
		return nil
	}

	if vers == "" {
		fmt.Println(nod)
		vers = nod.VerString()
	} else {
		if !strings.Contains(vers, nod.VerString()) {
			vers = vers + "," + nod.VerString()
		} else {
			return nil
		}
	}

	err = batchPut(batch, "ver:"+id, vers)
	if err != nil {
		return err
	}

	if !needSplit {
		return nil
	}

	// indexing package name
	keys := splitPkgName(nod.ImportPath)
	for key, _ := range keys {
		err = batchPut(batch, fmt.Sprintf("key:%v:%v", key, id), "")
		if err != nil {
			return err
		}
	}

	// TODO: indexing desc
	if nod.Synopsis != "" {
		//fields := strings.FieldsFunc(nod.Synopsis, f)
	}

	return db.Write(batch, wo)
}

func rmPkg(nod *doc.Node) {

}

var db *leveldb.DB

// service should be run
func AutoRun() error {
	s, _, _ := runningStatus()
	if s == STOP {
		// current path
		curPath, err := os.Getwd()
		if err != nil {
			return err
		}

		attr := &os.ProcAttr{
			Dir:   curPath,
			Env:   os.Environ(),
			Files: []*os.File{nil, nil, nil},
		}

		p, err := exePath()
		if err != nil {
			return err
		}

		_, err = os.StartProcess(p, []string{"gopm", "serve", "-l"}, attr)
		if err != nil {
			return err
		}
	}
	return nil
}

func runningStatus() (int, int, int) {
	pFile, err := getPidPath()
	if err != nil {
		return STOP, 0, 0
	}

	contentByte, err := ioutil.ReadFile(pFile)
	if err != nil {
		return STOP, 0, 0
	}
	content := string(contentByte)
	if len(content) < 0 || !strings.Contains(content, ",") {
		return STOP, 0, 0
	}
	cs := strings.Split(string(content), ",")
	if len(cs) != 3 {
		return STOP, 0, 0
	}
	status, err := strconv.Atoi(cs[0])
	if err != nil {
		return STOP, 0, 0
	}
	if status < STOP || status > RUNNING {
		return STOP, 0, 0
	}
	pid, err := strconv.Atoi(cs[1])
	if err != nil {
		return STOP, 0, 0
	}

	_, err = os.FindProcess(pid)
	if err != nil {
		return STOP, 0, 0
	}

	port, err := strconv.Atoi(cs[2])
	if err != nil {
		return STOP, 0, 0
	}

	return status, pid, port
}

func getPidPath() (string, error) {
	homeDir, err := com.HomeDir()
	if err != nil {
		return "", err
	}

	pFile := strings.Replace("~/.gopm/var/", "~", homeDir, -1)
	os.MkdirAll(pFile, os.ModePerm)
	return pFile + "pid", nil
}

func startService(listen, port string) error {
	homeDir, err := com.HomeDir()
	if err != nil {
		return err
	}

	pFile, err := getPidPath()
	if err != nil {
		return err
	}

	f, err := os.OpenFile(pFile, os.O_CREATE, 0700)
	if err != nil {
		return err
	}
	defer f.Close()
	f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, os.Getpid(), port))

	dbDir = strings.Replace(dbDir, "~", homeDir, -1)

	db, err = leveldb.OpenFile(dbDir, &opt.Options{Flag: opt.OFCreateIfMissing})
	if err != nil {
		return err
	}
	defer db.Close()

	// these handlers should only access by localhost
	http.HandleFunc("/add", addHandler)
	http.HandleFunc("/rm", rmHandler)

	// these handlers can be accessed according listen's ip
	http.HandleFunc("/search", searchHandler)
	http.HandleFunc("/searche", searcheHandler)
	http.ListenAndServe(listen+":"+port, nil)
	return nil
}

func searchHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	ids := make(map[string]bool)
	for key, _ := range r.Form {
		iter := db.NewIterator(ro)
		rkey := fmt.Sprintf("key:%v:", key)
		if iter.Seek([]byte(rkey)) {
			k := iter.Key()
			if !strings.HasPrefix(string(k), rkey) {
				break
			} else {
				ids[string(k)] = true
			}
		}
		for iter.Next() {
			k := iter.Key()
			if !strings.HasPrefix(string(k), rkey) {
				break
			}
			ids[string(k)] = true
		}
	}

	pkgs := make([]string, 0)

	for id, _ := range ids {
		idkeys := strings.SplitN(id, ":", -1)
		rId := idkeys[len(idkeys)-1]
		fmt.Println(rId)
		pkg, err := dbGet(fmt.Sprintf("pkg:%v", rId))
		if err != nil {
			com.ColorLog(err.Error())
			continue
		}
		pkgs = append(pkgs, pkg)
	}

	w.Write([]byte("[\"" + strings.Join(pkgs, "\", \"") + "\"]"))
}

func searcheHandler(w http.ResponseWriter, r *http.Request) {
	//if r.Method == "POST" {
	r.ParseForm()
	pkgs := make([]string, 0)
	for key, _ := range r.Form {
		_, err := dbGet("index:" + key)

		if err != nil {
			com.ColorLog(err.Error())
			continue
		}

		pkgs = append(pkgs, key)
	}

	w.Write([]byte("[\"" + strings.Join(pkgs, "\", \"") + "\"]"))
	//}
}

func addHandler(w http.ResponseWriter, r *http.Request) {
	//if r.Method == "POST" {
	r.ParseForm()

	nod := new(doc.Node)
	nod.ImportPath = r.FormValue("importPath")
	nod.Synopsis = r.FormValue("synopsis")
	nod.DownloadURL = r.FormValue("downloadURL")
	isGetDeps, err := strconv.ParseBool(r.FormValue("isGetDeps"))
	if err != nil {
		com.ColorLog("[ERRO] SEVER: Cannot get deps")
	}
	nod.IsGetDeps = isGetDeps
	nod.Type = r.FormValue("type")
	nod.Value = r.FormValue("value")

	err = addNode(nod)
	if err != nil {
		com.ColorLog("[ERRO] SEVER: Cannot add node[ %s ]\n", err)
	}
	//}
}

func rmHandler(w http.ResponseWriter, r *http.Request) {

}
<commit_msg>fixed because of leveldb's change<commit_after>// Copyright 2013 gopm authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package cmd

import (
	"errors"
	"fmt"
	"github.com/Unknwon/com"
	"github.com/gpmgo/gopm/doc"
	"github.com/syndtr/goleveldb/leveldb"
	"github.com/syndtr/goleveldb/leveldb/opt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
)

var (
	dbDir = "~/.gopm/db"
)

const (
	STOP = iota
	LOCALRUN
	RUNNING
)

var CmdServe = &Command{
	UsageLine: "serve [:port]",
	Short:     "serve for package search",
	Long: `
	serve provide a web service to search packages, download packages

The serve flags are:

	-l
		only service for localhost ip
`,
}

func init() {
	CmdServe.Run = runServe
	CmdServe.Flags = map[string]bool{
		"-l": false,
	}
}

func printServePrompt(flag string) {
	switch flag {
	case "-l":
		com.ColorLog("[INFO] You enabled start a service only localhost.\n")
	}
}

// Not implemented
func autoPort() string {
	return "8991"
}

func exePath() (string, error) {
	file, err := exec.LookPath(os.Args[0])
	if err != nil {
		return "", err
	}

	return filepath.Abs(file)
}

// search packages
func runServe(cmd *Command, args []string) {
	// Check flags.
	num := checkFlags(cmd.Flags, args, printServePrompt)
	if num == -1 {
		return
	}
	args = args[num:]

	var listen string
	var port string
	if cmd.Flags["-l"] {
		listen += "127.0.0.1"
		port = autoPort()
	} else {
		listen += "0.0.0.0"
		port = "8991"
	}

	// Check length of arguments.
	if len(args) >= 1 {
		port = args[0]
	}

	startService(listen, port)
}

func splitWord(word string, res *map[string]bool) {
	for i, _ := range word {
		for j, _ := range word[i:] {
			w := word[i : i+j+1]
			(*res)[w] = true
		}
	}
	return
}

func splitPkgName(pkgName string) (res map[string]bool) {
	//var src string
	ps := strings.Split(pkgName, "/")
	if len(ps) > 1 {
		ps = ps[1:]
	}

	res = make(map[string]bool, 0)
	res[strings.Join(ps, "/")] = true
	for _, w := range ps {
		splitWord(w, &res)
	}
	return
}

var (
	ro *opt.ReadOptions  = &opt.ReadOptions{}
	wo *opt.WriteOptions = &opt.WriteOptions{}
)

func dbGet(key string) (string, error) {
	v, err := db.Get([]byte(key), ro)
	return string(v), err
}

func dbPut(key string, value string) error {
	fmt.Println("put ", key, ": ", value)
	return db.Put([]byte(key), []byte(value), wo)
}

func batchPut(batch *leveldb.Batch, key string, value string) error {
	fmt.Println("put ", key, ": ", value)
	batch.Put([]byte(key), []byte(value))
	return nil
}

func getServeHost() string {
	return "localhost"
}

func getServePort() string {
	return "8991"
}

// for exernal of serve to add node to db
func saveNode(nod *doc.Node) error {
	urlPath := fmt.Sprintf("http://%v:%v/add", getServeHost(), getServePort())
	resp, err := http.PostForm(urlPath,
		url.Values{"importPath": {nod.ImportPath},
			"synopsis":    {nod.Synopsis},
			"downloadURL": {nod.DownloadURL},
			"isGetDeps":   {strconv.FormatBool(nod.IsGetDeps)},
			"type":        {nod.Type},
			"value":       {nod.Value}})

	if err != nil {
		com.ColorLog("[ERRO] Fail to save node[ %s ]\n", err)
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		return nil
	}
	return errors.New("save node failed with " + resp.Status)
}

// for inetrnal of serve to add node to db
func addNode(nod *doc.Node) error {
	batch := new(leveldb.Batch)
	strLastId, err := dbGet("lastId")
	if err != nil {
		if err == leveldb.ErrNotFound {
			strLastId = "0"
			err = batchPut(batch, "lastId", strLastId)
		} else {
			return err
		}
	}
	if err != nil {
		return err
	}

	lastId, err := strconv.ParseInt(strLastId, 0, 64)
	if err != nil {
		return err
	}

	nodKey := fmt.Sprintf("index:%v", nod.ImportPath)

	id, err := dbGet(nodKey)
	if err != nil {
		if err == leveldb.ErrNotFound {
			id = fmt.Sprintf("%v", lastId+1)
			err = batchPut(batch, "lastId", id)
			if err == nil {
				err = batchPut(batch, nodKey, id)
			}
			if err == nil {
				err = batchPut(batch, "pkg:"+id, nod.ImportPath)
			}
			if err == nil {
				err = batchPut(batch, "desc:"+id, nod.Synopsis)
			}
			if err == nil {
				err = batchPut(batch, "down:"+id, nod.DownloadURL)
			}
			if err == nil {
				err = batchPut(batch, "deps:"+id, strconv.FormatBool(nod.IsGetDeps))
			}

			// save totals
			total, err := dbGet("total")
			if err != nil {
				if err == leveldb.ErrNotFound {
					total = "1"
				} else {
					return err
				}
			} else {
				totalInt, err := strconv.ParseInt(total, 0, 64)
				if err != nil {
					return err
				}
				totalInt = totalInt + 1
				total = fmt.Sprintf("%v", totalInt)
			}

			err = batchPut(batch, "total", total)
		} else {
			return err
		}
	}

	if err != nil {
		return err
	}

	// save vers
	vers, err := dbGet("ver:" + id)
	needSplit := (err == leveldb.ErrNotFound)
	if err != nil {
		if err != leveldb.ErrNotFound {
			return err
		}
	} else {
		return nil
	}

	if vers == "" {
		fmt.Println(nod)
		vers = nod.VerString()
	} else {
		if !strings.Contains(vers, nod.VerString()) {
			vers = vers + "," + nod.VerString()
		} else {
			return nil
		}
	}

	err = batchPut(batch, "ver:"+id, vers)
	if err != nil {
		return err
	}

	if !needSplit {
		return nil
	}

	// indexing package name
	keys := splitPkgName(nod.ImportPath)
	for key, _ := range keys {
		err = batchPut(batch, fmt.Sprintf("key:%v:%v", key, id), "")
		if err != nil {
			return err
		}
	}

	// TODO: indexing desc
	if nod.Synopsis != "" {
		//fields := strings.FieldsFunc(nod.Synopsis, f)
	}

	return db.Write(batch, wo)
}

func rmPkg(nod *doc.Node) {

}

var db *leveldb.DB

// service should be run
func AutoRun() error {
	s, _, _ := runningStatus()
	if s == STOP {
		// current path
		curPath, err := os.Getwd()
		if err != nil {
			return err
		}

		attr := &os.ProcAttr{
			Dir:   curPath,
			Env:   os.Environ(),
			Files: []*os.File{nil, nil, nil},
		}

		p, err := exePath()
		if err != nil {
			return err
		}

		_, err = os.StartProcess(p, []string{"gopm", "serve", "-l"}, attr)
		if err != nil {
			return err
		}
	}
	return nil
}

func runningStatus() (int, int, int) {
	pFile, err := getPidPath()
	if err != nil {
		return STOP, 0, 0
	}

	contentByte, err := ioutil.ReadFile(pFile)
	if err != nil {
		return STOP, 0, 0
	}
	content := string(contentByte)
	if len(content) < 0 || !strings.Contains(content, ",") {
		return STOP, 0, 0
	}
	cs := strings.Split(string(content), ",")
	if len(cs) != 3 {
		return STOP, 0, 0
	}
	status, err := strconv.Atoi(cs[0])
	if err != nil {
		return STOP, 0, 0
	}
	if status < STOP || status > RUNNING {
		return STOP, 0, 0
	}
	pid, err := strconv.Atoi(cs[1])
	if err != nil {
		return STOP, 0, 0
	}

	_, err = os.FindProcess(pid)
	if err != nil {
		return STOP, 0, 0
	}

	port, err := strconv.Atoi(cs[2])
	if err != nil {
		return STOP, 0, 0
	}

	return status, pid, port
}

func getPidPath() (string, error) {
	homeDir, err := com.HomeDir()
	if err != nil {
		return "", err
	}

	pFile := strings.Replace("~/.gopm/var/", "~", homeDir, -1)
	os.MkdirAll(pFile, os.ModePerm)
	return pFile + "pid", nil
}

func startService(listen, port string) error {
	homeDir, err := com.HomeDir()
	if err != nil {
		return err
	}

	pFile, err := getPidPath()
	if err != nil {
		return err
	}

	f, err := os.OpenFile(pFile, os.O_CREATE, 0700)
	if err != nil {
		return err
	}
	defer f.Close()
	f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, os.Getpid(), port))

	dbDir = strings.Replace(dbDir, "~", homeDir, -1)

	db, err = leveldb.OpenFile(dbDir, &opt.Options{Flag: opt.OFCreateIfMissing})
	if err != nil {
		return err
	}
	defer db.Close()

	// these handlers should only access by localhost
	http.HandleFunc("/add", addHandler)
	http.HandleFunc("/rm", rmHandler)

	// these handlers can be accessed according listen's ip
	http.HandleFunc("/search", searchHandler)
	http.HandleFunc("/searche", searcheHandler)
	http.ListenAndServe(listen+":"+port, nil)
	return nil
}

func searchHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	ids := make(map[string]bool)
	for key, _ := range r.Form {
		iter := db.NewIterator(ro)
		rkey := fmt.Sprintf("key:%v:", key)
		if iter.Seek([]byte(rkey)) {
			k := iter.Key()
			if !strings.HasPrefix(string(k), rkey) {
				break
			} else {
				ids[string(k)] = true
			}
		}
		for iter.Next() {
			k := iter.Key()
			if !strings.HasPrefix(string(k), rkey) {
				break
			}
			ids[string(k)] = true
		}
	}

	pkgs := make([]string, 0)

	for id, _ := range ids {
		idkeys := strings.SplitN(id, ":", -1)
		rId := idkeys[len(idkeys)-1]
		fmt.Println(rId)
		pkg, err := dbGet(fmt.Sprintf("pkg:%v", rId))
		if err != nil {
			com.ColorLog(err.Error())
			continue
		}
		pkgs = append(pkgs, pkg)
	}

	w.Write([]byte("[\"" + strings.Join(pkgs, "\", \"") + "\"]"))
}

func searcheHandler(w http.ResponseWriter, r *http.Request) {
	//if r.Method == "POST" {
	r.ParseForm()
	pkgs := make([]string, 0)
	for key, _ := range r.Form {
		_, err := dbGet("index:" + key)

		if err != nil {
			com.ColorLog(err.Error())
			continue
		}

		pkgs = append(pkgs, key)
	}

	w.Write([]byte("[\"" + strings.Join(pkgs, "\", \"") + "\"]"))
	//}
}

func addHandler(w http.ResponseWriter, r *http.Request) {
	//if r.Method == "POST" {
	r.ParseForm()

	nod := new(doc.Node)
	nod.ImportPath = r.FormValue("importPath")
	nod.Synopsis = r.FormValue("synopsis")
	nod.DownloadURL = r.FormValue("downloadURL")
	isGetDeps, err := strconv.ParseBool(r.FormValue("isGetDeps"))
	if err != nil {
		com.ColorLog("[ERRO] SEVER: Cannot get deps")
	}
	nod.IsGetDeps = isGetDeps
	nod.Type = r.FormValue("type")
	nod.Value = r.FormValue("value")

	err = addNode(nod)
	if err != nil {
		com.ColorLog("[ERRO] SEVER: Cannot add node[ %s ]\n", err)
	}
	//}
}

func rmHandler(w http.ResponseWriter, r *http.Request) {

}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"path/filepath"
)

// CmdAllow is `direnv allow [PATH_TO_RC]`
var CmdAllow = &Cmd{
	Name:   "allow",
	Desc:   "Grants direnv to load the given .envrc",
	Args:   []string{"[PATH_TO_RC]"},
	Action: actionWithConfig(cmdAllowAction),
}

var migrationMessage = `
Migrating the allow data to the new location

The allowed .envrc permissions used to be stored in the XDG_CONFIG_DIR. It's
better to keep that folder for user-editable configuration so the data is
being moved to XDG_DATA_HOME.
`

func cmdAllowAction(env Env, args []string, config *Config) (err error) {
	var rcPath string
	if len(args) > 1 {
		rcPath = args[1]
	} else {
		if rcPath, err = os.Getwd(); err != nil {
			return err
		}
	}

	if _, err = os.Stat(config.AllowDir()); os.IsNotExist(err) {
		oldAllowDir := filepath.Join(config.ConfDir, "allow")
		if _, err = os.Stat(oldAllowDir); err == nil {
			fmt.Println(migrationMessage)

			fmt.Printf("moving %s to %s\n", oldAllowDir, config.AllowDir())
			if err = os.MkdirAll(config.AllowDir(), 0755); err != nil {
				return err
			}

			if err = os.Rename(oldAllowDir, config.AllowDir()); err != nil {
				return err
			}

			fmt.Printf("creating a symlink back from %s to %s for back-compat.\n", config.AllowDir(), oldAllowDir)
			if err = os.Symlink(config.AllowDir(), oldAllowDir); err != nil {
				return err
			}
			fmt.Println("")
			fmt.Println("All done, have a nice day!")
		}
	}

	rc := FindRC(rcPath, config)
	if rc == nil {
		return fmt.Errorf(".envrc file not found")
	}
	return rc.Allow()
}
<commit_msg>fixup! direnv allow: create allow dir during migration<commit_after>package main

import (
	"fmt"
	"os"
	"path/filepath"
)

// CmdAllow is `direnv allow [PATH_TO_RC]`
var CmdAllow = &Cmd{
	Name:   "allow",
	Desc:   "Grants direnv to load the given .envrc",
	Args:   []string{"[PATH_TO_RC]"},
	Action: actionWithConfig(cmdAllowAction),
}

var migrationMessage = `
Migrating the allow data to the new location

The allowed .envrc permissions used to be stored in the XDG_CONFIG_DIR. It's
better to keep that folder for user-editable configuration so the data is
being moved to XDG_DATA_HOME.
`

func cmdAllowAction(env Env, args []string, config *Config) (err error) {
	var rcPath string
	if len(args) > 1 {
		rcPath = args[1]
	} else {
		if rcPath, err = os.Getwd(); err != nil {
			return err
		}
	}

	if _, err = os.Stat(config.AllowDir()); os.IsNotExist(err) {
		oldAllowDir := filepath.Join(config.ConfDir, "allow")
		if _, err = os.Stat(oldAllowDir); err == nil {
			fmt.Println(migrationMessage)

			fmt.Printf("moving %s to %s\n", oldAllowDir, config.AllowDir())
			if err = os.MkdirAll(filepath.Dir(config.AllowDir()), 0755); err != nil {
				return err
			}

			if err = os.Rename(oldAllowDir, config.AllowDir()); err != nil {
				return err
			}

			fmt.Printf("creating a symlink back from %s to %s for back-compat.\n", config.AllowDir(), oldAllowDir)
			if err = os.Symlink(config.AllowDir(), oldAllowDir); err != nil {
				return err
			}
			fmt.Println("")
			fmt.Println("All done, have a nice day!")
		}
	}

	rc := FindRC(rcPath, config)
	if rc == nil {
		return fmt.Errorf(".envrc file not found")
	}
	return rc.Allow()
}
<|endoftext|>


<commit_before>package cmds

import "fmt"
import "errors"

import "github.com/wsxiaoys/terminal/color"
import "github.com/xogeny/gimpact/utils"

type InfoCommand struct {
	Verbose    bool `short:"v" login:"verbose" description:"Turn on verbose output"`
	Positional struct {
		LibraryName string `description:"Library name"`
	} `positional-args:"true" required:"true"`
}

func (x *InfoCommand) Execute(args []string) error {
	if len(args) > 0 {
		fmt.Print("Ignoring (extra) unrecognized arguments: ")
		fmt.Println(args)
	}
	index := utils.DownloadIndex()

	libname := utils.LibraryName(x.Positional.LibraryName)
	lib, ok := index[libname]
	if !ok {
		return errors.New("Unable to locate library named '" + string(libname) + "'")
	}
	color.Println("@{!}Name: @{g}" + string(lib.Name))
	color.Println("  @{!}Description: @{c}" + string(lib.Description))
	color.Println("  @{!}Homepage: @{y}" + string(lib.Homepage))
	for vname, v := range lib.Versions {
		if x.Verbose {
			color.Println("    @{!}Version: " + string(vname))
			color.Println(fmt.Sprintf("      Major Version: %d", v.Major))
			color.Println(fmt.Sprintf("      Minro Version: %d", v.Minor))
			color.Println(fmt.Sprintf("      Patch Version: %d", v.Patch))
			color.Println("      Canonical Version: " + string(v.Version))
		} else {
			color.Println("    @{!}Version: " + string(vname) + " (canonically " + string(v.Version) + ")")
		}
	}
	return nil
}
<commit_msg>Fixing a "minor" typo :-)<commit_after>package cmds

import "fmt"
import "errors"

import "github.com/wsxiaoys/terminal/color"
import "github.com/xogeny/gimpact/utils"

type InfoCommand struct {
	Verbose    bool `short:"v" login:"verbose" description:"Turn on verbose output"`
	Positional struct {
		LibraryName string `description:"Library name"`
	} `positional-args:"true" required:"true"`
}

func (x *InfoCommand) Execute(args []string) error {
	if len(args) > 0 {
		fmt.Print("Ignoring (extra) unrecognized arguments: ")
		fmt.Println(args)
	}
	index := utils.DownloadIndex()

	libname := utils.LibraryName(x.Positional.LibraryName)
	lib, ok := index[libname]
	if !ok {
		return errors.New("Unable to locate library named '" + string(libname) + "'")
	}
	color.Println("@{!}Name: @{g}" + string(lib.Name))
	color.Println("  @{!}Description: @{c}" + string(lib.Description))
	color.Println("  @{!}Homepage: @{y}" + string(lib.Homepage))
	for vname, v := range lib.Versions {
		if x.Verbose {
			color.Println("    @{!}Version: " + string(vname))
			color.Println(fmt.Sprintf("      Major Version: %d", v.Major))
			color.Println(fmt.Sprintf("      Minor Version: %d", v.Minor))
			color.Println(fmt.Sprintf("      Patch Version: %d", v.Patch))
			color.Println("      Canonical Version: " + string(v.Version))
		} else {
			color.Println("    @{!}Version: " + string(vname) + " (canonically " + string(v.Version) + ")")
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package cmds

import (
	"bytemark.co.uk/client/cmds/util"
	"bytemark.co.uk/client/util/log"
)

func (cmds *CommandSet) HelpForList() util.ExitCode {
	log.Log("bytemark list")
	log.Log("")
	log.Log("usage: bytemark list vms <group>")
	log.Log("       bytemark list groups <account>")
	log.Log("       bytemark list accounts")
	log.Log("       bytemark list discs <virtual machine>")
	return util.E_USAGE_DISPLAYED
}

func (cmds *CommandSet) ListVMs(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	nameStr, ok := util.ShiftArgument(&args, "group")
	if !ok {
		cmds.HelpForList()
		return util.E_PEBKAC
	}

	name := cmds.bigv.ParseGroupName(nameStr)

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	group, err := cmds.bigv.GetGroup(name)

	if err != nil {
		// TODO: try it as an account
		return util.ProcessError(err)
	}

	for _, vm := range group.VirtualMachines {
		log.Log(vm.Hostname)
	}
	return util.E_SUCCESS
}

func (cmds *CommandSet) ListGroups(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	name, ok := util.ShiftArgument(&args, "account")
	if !ok {
		cmds.HelpForList()
		return util.E_PEBKAC
	}

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	account, err := cmds.bigv.GetAccount(name)

	if err != nil {
		return util.ProcessError(err)
	}

	for _, group := range account.Groups {
		log.Output(group.Name)
	}
	return util.E_SUCCESS
}

func (cmds *CommandSet) ListAccounts(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	accounts, err := cmds.bigv.GetAccounts()

	if err != nil {
		return util.ProcessError(err)
	}

	for _, group := range accounts {
		log.Log(group.Name)
	}
	return util.E_SUCCESS
}

func (cmds *CommandSet) ListDiscs(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	nameStr, ok := util.ShiftArgument(&args, "virtual machine")
	if !ok {
		cmds.HelpForList()
		return util.E_PEBKAC
	}

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	name, err := cmds.bigv.ParseVirtualMachineName(nameStr)

	vm, err := cmds.bigv.GetVirtualMachine(name)

	for _, disc := range vm.Discs {
		log.Logf("%s: %dGiB %s\r\n", disc.Label, (disc.Size / 1024), disc.StorageGrade)
	}
	return util.E_SUCCESS
}
<commit_msg>Implement good defaulting in ListGroups<commit_after>package cmds

import (
	"bytemark.co.uk/client/cmds/util"
	"bytemark.co.uk/client/util/log"
)

func (cmds *CommandSet) HelpForList() util.ExitCode {
	log.Log("bytemark list")
	log.Log("")
	log.Log("usage: bytemark list vms [group]")
	log.Log("       bytemark list groups [account]")
	log.Log("       bytemark list accounts")
	log.Log("       bytemark list discs <virtual machine>")
	return util.E_USAGE_DISPLAYED
}

func (cmds *CommandSet) ListVMs(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	nameStr, ok := util.ShiftArgument(&args, "group")
	if !ok {
		//GetAccount
	}

	name := cmds.bigv.ParseGroupName(nameStr)

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	group, err := cmds.bigv.GetGroup(name)

	if err != nil {
		// TODO: try it as an account
		return util.ProcessError(err)
	}

	for _, vm := range group.VirtualMachines {
		log.Log(vm.Hostname)
	}
	return util.E_SUCCESS
}

func (cmds *CommandSet) ListGroups(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	name := ""
	if len(args) >= 1 {
		name = args[0]
	}
	// no error here - GetAccount will pick a sane default.

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	account, err := cmds.bigv.GetAccount(name)

	if err != nil {
		return util.ProcessError(err)
	}

	for _, group := range account.Groups {
		log.Output(group.Name)
	}
	return util.E_SUCCESS
}

func (cmds *CommandSet) ListAccounts(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	accounts, err := cmds.bigv.GetAccounts()

	if err != nil {
		return util.ProcessError(err)
	}

	for _, group := range accounts {
		log.Log(group.Name)
	}
	return util.E_SUCCESS
}

func (cmds *CommandSet) ListDiscs(args []string) util.ExitCode {
	flags := util.MakeCommonFlagSet()
	flags.Parse(args)
	args = cmds.config.ImportFlags(flags)

	nameStr, ok := util.ShiftArgument(&args, "virtual machine")
	if !ok {
		cmds.HelpForList()
		return util.E_PEBKAC
	}

	err := cmds.EnsureAuth()
	if err != nil {
		return util.ProcessError(err)
	}

	name, err := cmds.bigv.ParseVirtualMachineName(nameStr)

	vm, err := cmds.bigv.GetVirtualMachine(name)

	for _, disc := range vm.Discs {
		log.Logf("%s: %dGiB %s\r\n", disc.Label, (disc.Size / 1024), disc.StorageGrade)
	}
	return util.E_SUCCESS
}
<|endoftext|>


<commit_before>package rollbar

import (
	"bytes"
	"encoding/json"
	"fmt"
	"hash/adler32"
	"net/http"
	"os"
	"reflect"
	"runtime"
	"strings"
	"sync"
	"time"
)

const (
	NAME    = "go-rollbar"
	VERSION = "0.0.4"
)

var (
	// Rollbar access token. If this is blank, no errors will be reported to
	// Rollbar.
	Token = ""

	// All errors and messages will be submitted under this environment.
	Environment = "development"

	// API endpoint for Rollbar.
	Endpoint = "https://api.rollbar.com/api/1/item/"

	// Maximum number of errors allowed in the sending queue before we start
	// dropping new errors on the floor.
	Buffer = 100

	// Queue of messages to be sent.
	bodyChannel chan map[string]interface{}
	once        sync.Once
	waitGroup   sync.WaitGroup
)

// -- Error reporting

// Error asynchronously sends an error to Rollbar with the given severity level.
func Error(level string, err error) {
	ErrorWithStackSkip(level, err, 1)
}

// ErrorWithStackSkip asynchronously sends an error to Rollbar with the given
// severity level and a given number of stack trace frames skipped.
func ErrorWithStackSkip(level string, err error, skip int) {
	once.Do(initChannel)

	body := buildBody(level, err.Error())
	data := body["data"].(map[string]interface{})
	data["body"] = errorBody(err, skip)

	push(body)
}

// -- Message reporting

// Message asynchronously sends a message to Rollbar with the given severity
// level. Rollbar request is asynchronous.
func Message(level string, msg string) {
	once.Do(initChannel)

	body := buildBody(level, msg)
	data := body["data"].(map[string]interface{})
	data["body"] = messageBody(msg)

	push(body)
}

// -- Misc.

// Wait will block until the queue of errors / messages is empty.
func Wait() {
	waitGroup.Wait()
}

// Build the main JSON structure that will be sent to Rollbar with the
// appropriate metadata.
func buildBody(level, title string) map[string]interface{} {
	timestamp := time.Now().Unix()
	hostname, _ := os.Hostname()

	return map[string]interface{}{
		"access_token": Token,
		"data": map[string]interface{}{
			"environment": Environment,
			"title":       title,
			"level":       level,
			"timestamp":   timestamp,
			"platform":    runtime.GOOS,
			"language":    "go",
			"server": map[string]interface{}{
				"host": hostname,
			},
			"notifier": map[string]interface{}{
				"name":    NAME,
				"version": VERSION,
			},
		},
	}
}

// Build an error inner-body for the given error. If skip is provided, that
// number of stack trace frames will be skipped.
func errorBody(err error, skip int) map[string]interface{} {
	return map[string]interface{}{
		"trace": map[string]interface{}{
			"frames": stacktraceFrames(3 + skip),
			"exception": map[string]interface{}{
				"class":   errorClass(err),
				"message": err.Error(),
			},
		},
	}
}

// Build a message inner-body for the given message string.
func messageBody(s string) map[string]interface{} {
	return map[string]interface{}{
		"message": map[string]interface{}{
			"body": s,
		},
	}
}

func errorClass(err error) string {
	class := reflect.TypeOf(err).String()
	if class == "" {
		return "panic"
	} else if class == "*errors.errorString" {
		checksum := adler32.Checksum([]byte(err.Error()))
		return fmt.Sprintf("{%x}", checksum)
	} else {
		return strings.TrimPrefix(class, "*")
	}
}

// -- POST handling

// Start a goroutine that sends all errors and messages to Rollbar.
func initChannel() {
	bodyChannel = make(chan map[string]interface{}, Buffer)

	go func() {
		for body := range bodyChannel {
			post(body)
			waitGroup.Done()
		}
	}()
}

// Queue the given JSON body to be POSTed to Rollbar.
func push(body map[string]interface{}) {
	if len(bodyChannel) < Buffer {
		waitGroup.Add(1)
		bodyChannel <- body
	}
}

// POST the given JSON body to Rollbar synchronously.
func post(body map[string]interface{}) {
	if len(Token) == 0 {
		stderr("Token is empty")
		return
	}

	jsonBody, err := json.Marshal(body)
	if err != nil {
		stderr(fmt.Sprintf("Rollbar payload couldn't be encoded: %s", err.Error()))
		return
	}

	resp, err := http.Post(Endpoint, "application/json", bytes.NewReader(jsonBody))
	defer resp.Body.Close()
	if err != nil {
		stderr(fmt.Sprintf("Rollbar POST failed: %s", err.Error()))
	} else if resp.StatusCode != 200 {
		stderr(fmt.Sprintf("Rollbar response: %s", resp.Status))
	}
}
<commit_msg>calculate and send fingerprint which intelligently groups errors by their unique callstack (otherwise the same errors that have a variable text in their error message show up as unique)<commit_after>package rollbar

import (
	"bytes"
	"encoding/json"
	"fmt"
	"hash/adler32"
	"net/http"
	"os"
	"reflect"
	"runtime"
	"strings"
	"sync"
	"time"
)

const (
	NAME    = "go-rollbar"
	VERSION = "0.0.4"
)

var (
	// Rollbar access token. If this is blank, no errors will be reported to
	// Rollbar.
	Token = ""

	// All errors and messages will be submitted under this environment.
	Environment = "development"

	// API endpoint for Rollbar.
	Endpoint = "https://api.rollbar.com/api/1/item/"

	// Maximum number of errors allowed in the sending queue before we start
	// dropping new errors on the floor.
	Buffer = 100

	// Queue of messages to be sent.
	bodyChannel chan map[string]interface{}
	once        sync.Once
	waitGroup   sync.WaitGroup
)

// -- Error reporting

// Error asynchronously sends an error to Rollbar with the given severity level.
func Error(level string, err error) {
	ErrorWithStackSkip(level, err, 1)
}

// ErrorWithStackSkip asynchronously sends an error to Rollbar with the given
// severity level and a given number of stack trace frames skipped.
func ErrorWithStackSkip(level string, err error, skip int) {
	once.Do(initChannel)

	body := buildBody(level, err.Error())
	data := body["data"].(map[string]interface{})
	errBody, fingerprint := errorBody(err, skip)
	data["body"] = errBody
	data["fingerprint"] = fingerprint

	push(body)
}

// -- Message reporting

// Message asynchronously sends a message to Rollbar with the given severity
// level. Rollbar request is asynchronous.
func Message(level string, msg string) {
	once.Do(initChannel)

	body := buildBody(level, msg)
	data := body["data"].(map[string]interface{})
	data["body"] = messageBody(msg)

	push(body)
}

// -- Misc.

// Wait will block until the queue of errors / messages is empty.
func Wait() {
	waitGroup.Wait()
}

// Build the main JSON structure that will be sent to Rollbar with the
// appropriate metadata.
func buildBody(level, title string) map[string]interface{} {
	timestamp := time.Now().Unix()
	hostname, _ := os.Hostname()

	return map[string]interface{}{
		"access_token": Token,
		"data": map[string]interface{}{
			"environment": Environment,
			"title":       title,
			"level":       level,
			"timestamp":   timestamp,
			"platform":    runtime.GOOS,
			"language":    "go",
			"server": map[string]interface{}{
				"host": hostname,
			},
			"notifier": map[string]interface{}{
				"name":    NAME,
				"version": VERSION,
			},
		},
	}
}

// Create a fingerprint that uniqely identify a given message. We use the full
// callstack, including file names. That ensure that there are no false duplicates
// but also means that after changing the code (adding/removing lines), the
// fingerprints will change. It's a trade-off.
func calcFingerprint(frames []map[string]interface{}) string {
	s := ""
	for _, frame := range frames {
		fileName := frame["filename"].(string)
		method := frame["method"].(string)
		lineNo := frame["lineno"].(int)
		s += fmt.Sprintf("%s%d%s", fileName, lineNo, method)
	}
	checksum := adler32.Checksum([]byte(s))
	return fmt.Sprintf("%x", checksum)
}

// Build an error inner-body for the given error. If skip is provided, that
// number of stack trace frames will be skipped.
func errorBody(err error, skip int) (map[string]interface{}, string) {
	frames := stacktraceFrames(3 + skip)
	fingerprint := calcFingerprint(frames)
	errBody := map[string]interface{}{
		"trace": map[string]interface{}{
			"frames": frames,
			"exception": map[string]interface{}{
				"class":   errorClass(err),
				"message": err.Error(),
			},
		},
	}
	return errBody, fingerprint
}

// Build a message inner-body for the given message string.
func messageBody(s string) map[string]interface{} {
	return map[string]interface{}{
		"message": map[string]interface{}{
			"body": s,
		},
	}
}

func errorClass(err error) string {
	class := reflect.TypeOf(err).String()
	if class == "" {
		return "panic"
	} else if class == "*errors.errorString" {
		checksum := adler32.Checksum([]byte(err.Error()))
		return fmt.Sprintf("{%x}", checksum)
	} else {
		return strings.TrimPrefix(class, "*")
	}
}

// -- POST handling

// Start a goroutine that sends all errors and messages to Rollbar.
func initChannel() {
	bodyChannel = make(chan map[string]interface{}, Buffer)

	go func() {
		for body := range bodyChannel {
			post(body)
			waitGroup.Done()
		}
	}()
}

// Queue the given JSON body to be POSTed to Rollbar.
func push(body map[string]interface{}) {
	if len(bodyChannel) < Buffer {
		waitGroup.Add(1)
		bodyChannel <- body
	}
}

// POST the given JSON body to Rollbar synchronously.
func post(body map[string]interface{}) {
	if len(Token) == 0 {
		stderr("Token is empty")
		return
	}

	jsonBody, err := json.Marshal(body)
	if err != nil {
		stderr(fmt.Sprintf("Rollbar payload couldn't be encoded: %s", err.Error()))
		return
	}

	resp, err := http.Post(Endpoint, "application/json", bytes.NewReader(jsonBody))
	defer resp.Body.Close()
	if err != nil {
		stderr(fmt.Sprintf("Rollbar POST failed: %s", err.Error()))
	} else if resp.StatusCode != 200 {
		stderr(fmt.Sprintf("Rollbar response: %s", resp.Status))
	}
}
<|endoftext|>


<commit_before>package sparta

// THIS FILE IS AUTOMATICALLY GENERATED
// DO NOT EDIT
// CREATED: 2021-01-14 06:26:30.314758 +0000 UTC

// SpartaGitHash is the commit hash of this Sparta library
const SpartaGitHash = "7252cb4d16287b5ffd520bfdb83f7d7ee6607922"
<commit_msg>"Autogenerated build info"<commit_after>package sparta

// THIS FILE IS AUTOMATICALLY GENERATED
// DO NOT EDIT
// CREATED: 2021-01-15 03:49:05.456428 +0000 UTC

// SpartaGitHash is the commit hash of this Sparta library
const SpartaGitHash = "08e6a297e0687350eac5385f3bd539d9005954d5"
<|endoftext|>


<commit_before>package tango

import (
	"testing"
	"bytes"
	"strings"
	"net/http"
	"net/http/httptest"
	"fmt"
)

func TestCookie1(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ck := ctx.Cookies().Get("name")
		if ck != nil {
			return ck.Value
		}
		return ""
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}
	req.AddCookie(NewCookie("name", "test"))

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")
}

func TestCookie2(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ctx.Cookies().Set(NewCookie("name", "test"))
		return "test"
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")
	expect(t, strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0], "name=test")
}

func TestCookie3(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ck := ctx.SecureCookies("sssss").Get("name")
		if ck != nil {
			return ck.Value
		}
		return ""
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}
	req.AddCookie(NewSecureCookie("sssss", "name", "test"))

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")
}

func TestCookie4(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ctx.SecureCookies("ttttt").Set(NewSecureCookie("ttttt", "name", "test"))
		return "test"
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")

	r := strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0]
	s := strings.SplitN(r, "=", 2)
	name, value := s[0], s[1]
	expect(t, name, "name")
	expect(t, parseSecureCookie("ttttt", value), "test")
}
<commit_msg>remove unimported<commit_after>package tango

import (
	"testing"
	"bytes"
	"strings"
	"net/http"
	"net/http/httptest"
)

func TestCookie1(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ck := ctx.Cookies().Get("name")
		if ck != nil {
			return ck.Value
		}
		return ""
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}
	req.AddCookie(NewCookie("name", "test"))

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")
}

func TestCookie2(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ctx.Cookies().Set(NewCookie("name", "test"))
		return "test"
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")
	expect(t, strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0], "name=test")
}

func TestCookie3(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ck := ctx.SecureCookies("sssss").Get("name")
		if ck != nil {
			return ck.Value
		}
		return ""
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}
	req.AddCookie(NewSecureCookie("sssss", "name", "test"))

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")
}

func TestCookie4(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recorder.Body = buff

	o := Classic()
	o.Get("/", func(ctx *Context) string {
		ctx.SecureCookies("ttttt").Set(NewSecureCookie("ttttt", "name", "test"))
		return "test"
	})

	req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
	if err != nil {
		t.Error(err)
	}

	o.ServeHTTP(recorder, req)
	expect(t, recorder.Code, http.StatusOK)
	refute(t, len(buff.String()), 0)
	expect(t, buff.String(), "test")

	r := strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0]
	s := strings.SplitN(r, "=", 2)
	name, value := s[0], s[1]
	expect(t, name, "name")
	expect(t, parseSecureCookie("ttttt", value), "test")
}
<|endoftext|>


<commit_before>// Library for all functions used in Yieldbot alert handlers and dashboard generators
//
// LICENSE:
//   Copyright 2015 Yieldbot. <devops@yieldbot.com>
//   Released under the MIT License; see LICENSE
//   for details.

// Package dracky implements common data structures and functions for Yieldbot monitoring alerts and dashboards
package dracky

import (
	"encoding/json"
	"github.com/yieldbot/dhuran"
	"io/ioutil"
	"os"
)

// Generate a simple string for use by elasticsearch and internal logging of all monitoring alerts.
func Event_name(client string, check string) string {
	return client + "_" + check
}

// Set the correct device that is being monitored. In the case of snmp trap collection, containers, or applicance
// monitoring the device running the sensu-client may not be the device actually being monitored.
func (e Sensu_Event) Acquire_monitored_instance() string {
	var monitored_instance string
	if e.Check.Source != "" {
		monitored_instance = e.Check.Source
	} else {
		monitored_instance = e.Client.Name
	}
	return monitored_instance
}

func Set_time(t int) string {

	timeStamp := time.Unix(unixIntValue, 0)
	timestamp = time.Unix(timestamp, 0).Format(time.RFC822Z)

}

// Set the environment that the machine is running in based upon values
// dropped via Oahi during the Chef run.
func Define_sensu_env(env string) string {
	switch env {
	case "prd":
		return "Prod "
	case "dev":
		return "Dev "
	case "stg":
		return "Stg "
	case "vagrant":
		return "Vagrant "
	default:
		return "Test "
	}
}

// Convert the check result status from an integer to a string.
func Define_status(status int) string {
	switch status {
	case 0:
		return "OK"
	case 1:
		return "WARNING"
	case 2:
		return "CRITICAL"
	case 3:
		return "UNKNOWN"
	case 126:
		return "PERMISSION DENIED"
	case 127:
		return "CONFIG ERROR"
	default:
		return "UNKNOWN"
	}
}

// Creates a monitor name that is easliy searchable in ES using different
// levels of granularity.
func Create_check_name(check string) string {
	return check
}

// Calculate how long a monitor has been in a given state.
func Define_check_state_duration() string {
	return " "
}

// Read in the environment details provided by Oahi and drop it into a staticly defined struct.
func Set_sensu_env() *Env_Details {
	env_file, err := ioutil.ReadFile(ENVIRONMENT_FILE)
	if err != nil {
		dhuran.Check(err)
	}

	var env_details Env_Details
	err = json.Unmarshal(env_file, &env_details)
	if err != nil {
		dhuran.Check(err)
	}
	return &env_details
}

// Read in the check result generated by Snesu via stdin and drop it into a staticaly defined struct.
func (e Sensu_Event) Acquire_sensu_event() *Sensu_Event {
	results, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		dhuran.Check(err)
	}
	err = json.Unmarshal(results, &e)
	if err != nil {
		dhuran.Check(err)
	}
	return &e
}
<commit_msg>comment out time stuff for testing<commit_after>// Library for all functions used in Yieldbot alert handlers and dashboard generators
//
// LICENSE:
//   Copyright 2015 Yieldbot. <devops@yieldbot.com>
//   Released under the MIT License; see LICENSE
//   for details.

// Package dracky implements common data structures and functions for Yieldbot monitoring alerts and dashboards
package dracky

import (
	"encoding/json"
	"github.com/yieldbot/dhuran"
	"io/ioutil"
	"os"
)

// Generate a simple string for use by elasticsearch and internal logging of all monitoring alerts.
func Event_name(client string, check string) string {
	return client + "_" + check
}

// Set the correct device that is being monitored. In the case of snmp trap collection, containers, or applicance
// monitoring the device running the sensu-client may not be the device actually being monitored.
func (e Sensu_Event) Acquire_monitored_instance() string {
	var monitored_instance string
	if e.Check.Source != "" {
		monitored_instance = e.Check.Source
	} else {
		monitored_instance = e.Client.Name
	}
	return monitored_instance
}

// func Set_time(t int) string {
//
// 	timeStamp := time.Unix(unixIntValue, 0)
// 	timestamp = time.Unix(timestamp, 0).Format(time.RFC822Z)
//
// }

// Set the environment that the machine is running in based upon values
// dropped via Oahi during the Chef run.
func Define_sensu_env(env string) string {
	switch env {
	case "prd":
		return "Prod "
	case "dev":
		return "Dev "
	case "stg":
		return "Stg "
	case "vagrant":
		return "Vagrant "
	default:
		return "Test "
	}
}

// Convert the check result status from an integer to a string.
func Define_status(status int) string {
	switch status {
	case 0:
		return "OK"
	case 1:
		return "WARNING"
	case 2:
		return "CRITICAL"
	case 3:
		return "UNKNOWN"
	case 126:
		return "PERMISSION DENIED"
	case 127:
		return "CONFIG ERROR"
	default:
		return "UNKNOWN"
	}
}

// Creates a monitor name that is easliy searchable in ES using different
// levels of granularity.
func Create_check_name(check string) string {
	return check
}

// Calculate how long a monitor has been in a given state.
func Define_check_state_duration() string {
	return " "
}

// Read in the environment details provided by Oahi and drop it into a staticly defined struct.
func Set_sensu_env() *Env_Details {
	env_file, err := ioutil.ReadFile(ENVIRONMENT_FILE)
	if err != nil {
		dhuran.Check(err)
	}

	var env_details Env_Details
	err = json.Unmarshal(env_file, &env_details)
	if err != nil {
		dhuran.Check(err)
	}
	return &env_details
}

// Read in the check result generated by Snesu via stdin and drop it into a staticaly defined struct.
func (e Sensu_Event) Acquire_sensu_event() *Sensu_Event {
	results, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		dhuran.Check(err)
	}
	err = json.Unmarshal(results, &e)
	if err != nil {
		dhuran.Check(err)
	}
	return &e
}
<|endoftext|>


<commit_before>package handlers

import (
	"context"
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"strings"
	"time"

	"github.com/alphagov/router/logger"
)

var TLSSkipVerify bool

func NewBackendHandler(backendURL *url.URL, connectTimeout, headerTimeout time.Duration, logger logger.Logger) http.Handler {
	proxy := httputil.NewSingleHostReverseProxy(backendURL)
	proxy.Transport = newBackendTransport(connectTimeout, headerTimeout, logger)

	defaultDirector := proxy.Director
	proxy.Director = func(req *http.Request) {
		defaultDirector(req)

		// Set the Host header to match the backend hostname instead of the one from the incoming request.
		req.Host = backendURL.Host

		// Setting a blank User-Agent causes the http lib not to output one, whereas if there
		// is no header, it will output a default one.
		// See: https://github.com/golang/go/blob/release-branch.go1.5/src/net/http/request.go#L419
		if _, present := req.Header["User-Agent"]; !present {
			req.Header.Set("User-Agent", "")
		}

		populateViaHeader(req.Header, fmt.Sprintf("%d.%d", req.ProtoMajor, req.ProtoMinor))
	}

	return proxy
}

func populateViaHeader(header http.Header, httpVersion string) {
	via := httpVersion + " router"
	if prior, ok := header["Via"]; ok {
		via = strings.Join(prior, ", ") + ", " + via
	}
	header.Set("Via", via)
}

type backendTransport struct {
	wrapped *http.Transport
	logger  logger.Logger
}

// Construct a backendTransport that wraps an http.Transport and implements http.RoundTripper.
// This allows us to intercept the response from the backend and modify it before it's copied
// back to the client.
func newBackendTransport(connectTimeout, headerTimeout time.Duration, logger logger.Logger) *backendTransport {
	transport := http.Transport{}

	transport.DialContext = func(_ context.Context, network, address string) (net.Conn, error) {
		return net.DialTimeout(network, address, connectTimeout)
	}

	// Remember, we have one transport per backend
	//
	// Using the below settings, and (for example) we have 25 backends
	//   25 * 60 = 1500
	// we will have a maximum of 1500 open idle connections
	//
	// The Go http.DefaultTransport sets this to 100,
	// we set to 60 because of potential file handle limits
	// because we have multiple backends
	transport.MaxIdleConns = 60
	// This is an arbitrarily selected number that is less than 60
	transport.MaxIdleConnsPerHost = 20
	// By default, idle connections do not expire,
	// unless they are closed by the other end of the connection,
	// and sometimes the other end will silently close the connection.
	// We should expire idle connections after a while.
	//
	// We arbitrarily chose 10 minutes
	transport.IdleConnTimeout = 10 * time.Minute

	// If we do not configure the timeouts, then connections will hang
	//
	// Configured by the caller
	transport.ResponseHeaderTimeout = headerTimeout
	//
	// Same values as http.DefaultTransport
	transport.TLSHandshakeTimeout = 10 * time.Second
	transport.ExpectContinueTimeout = 1 * time.Second

	if TLSSkipVerify {
		transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
	}

	return &backendTransport{&transport, logger}
}

func closeBody(resp *http.Response) {
	if resp != nil && resp.Body != nil {
		resp.Body.Close()
	}
}

func (bt *backendTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
	resp, err = bt.wrapped.RoundTrip(req)
	if err == nil {
		populateViaHeader(resp.Header, fmt.Sprintf("%d.%d", resp.ProtoMajor, resp.ProtoMinor))
	} else {
		// Log the error (deferred to allow special case error handling to add/change details)
		logDetails := map[string]interface{}{"error": err.Error(), "status": 500}
		defer bt.logger.LogFromBackendRequest(logDetails, req)
		defer logger.NotifySentry(logger.ReportableError{Error: err, Request: req, Response: resp})
		defer closeBody(resp)

		// Intercept some specific errors and generate an appropriate HTTP error response
		if netErr, ok := err.(net.Error); ok {
			if netErr.Timeout() {
				logDetails["status"] = 504
				return newErrorResponse(504), nil
			}
		}
		if strings.Contains(err.Error(), "connection refused") {
			logDetails["status"] = 502
			return newErrorResponse(502), nil
		}

		// 500 for all other errors
		return newErrorResponse(500), nil
	}
	return
}

func newErrorResponse(status int) (resp *http.Response) {
	resp = &http.Response{StatusCode: status}
	resp.Body = ioutil.NopCloser(strings.NewReader(""))
	return
}
<commit_msg>Use net.Dialer DialContext instead of custom func<commit_after>package handlers

import (
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"strings"
	"time"

	"github.com/alphagov/router/logger"
)

var TLSSkipVerify bool

func NewBackendHandler(backendURL *url.URL, connectTimeout, headerTimeout time.Duration, logger logger.Logger) http.Handler {
	proxy := httputil.NewSingleHostReverseProxy(backendURL)
	proxy.Transport = newBackendTransport(connectTimeout, headerTimeout, logger)

	defaultDirector := proxy.Director
	proxy.Director = func(req *http.Request) {
		defaultDirector(req)

		// Set the Host header to match the backend hostname instead of the one from the incoming request.
		req.Host = backendURL.Host

		// Setting a blank User-Agent causes the http lib not to output one, whereas if there
		// is no header, it will output a default one.
		// See: https://github.com/golang/go/blob/release-branch.go1.5/src/net/http/request.go#L419
		if _, present := req.Header["User-Agent"]; !present {
			req.Header.Set("User-Agent", "")
		}

		populateViaHeader(req.Header, fmt.Sprintf("%d.%d", req.ProtoMajor, req.ProtoMinor))
	}

	return proxy
}

func populateViaHeader(header http.Header, httpVersion string) {
	via := httpVersion + " router"
	if prior, ok := header["Via"]; ok {
		via = strings.Join(prior, ", ") + ", " + via
	}
	header.Set("Via", via)
}

type backendTransport struct {
	wrapped *http.Transport
	logger  logger.Logger
}

// Construct a backendTransport that wraps an http.Transport and implements http.RoundTripper.
// This allows us to intercept the response from the backend and modify it before it's copied
// back to the client.
func newBackendTransport(connectTimeout, headerTimeout time.Duration, logger logger.Logger) *backendTransport {
	transport := http.Transport{}

	transport.DialContext = (&net.Dialer{
		Timeout:   connectTimeout,   // Configured by caller
		KeepAlive: 30 * time.Second, // same as DefaultTransport
		DualStack: true,             // same as DefaultTransport
	}).DialContext

	// Remember, we have one transport per backend
	//
	// Using the below settings, and (for example) we have 25 backends
	//   25 * 60 = 1500
	// we will have a maximum of 1500 open idle connections
	//
	// The Go http.DefaultTransport sets this to 100,
	// we set to 60 because of potential file handle limits
	// because we have multiple backends
	transport.MaxIdleConns = 60
	// This is an arbitrarily selected number that is less than 60
	transport.MaxIdleConnsPerHost = 20
	// By default, idle connections do not expire,
	// unless they are closed by the other end of the connection,
	// and sometimes the other end will silently close the connection.
	// We should expire idle connections after a while.
	//
	// We arbitrarily chose 10 minutes
	transport.IdleConnTimeout = 10 * time.Minute

	// If we do not configure the timeouts, then connections will hang
	//
	// Configured by the caller
	transport.ResponseHeaderTimeout = headerTimeout
	//
	// Same values as http.DefaultTransport
	transport.TLSHandshakeTimeout = 10 * time.Second
	transport.ExpectContinueTimeout = 1 * time.Second

	if TLSSkipVerify {
		transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
	}

	return &backendTransport{&transport, logger}
}

func closeBody(resp *http.Response) {
	if resp != nil && resp.Body != nil {
		resp.Body.Close()
	}
}

func (bt *backendTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
	resp, err = bt.wrapped.RoundTrip(req)
	if err == nil {
		populateViaHeader(resp.Header, fmt.Sprintf("%d.%d", resp.ProtoMajor, resp.ProtoMinor))
	} else {
		// Log the error (deferred to allow special case error handling to add/change details)
		logDetails := map[string]interface{}{"error": err.Error(), "status": 500}
		defer bt.logger.LogFromBackendRequest(logDetails, req)
		defer logger.NotifySentry(logger.ReportableError{Error: err, Request: req, Response: resp})
		defer closeBody(resp)

		// Intercept some specific errors and generate an appropriate HTTP error response
		if netErr, ok := err.(net.Error); ok {
			if netErr.Timeout() {
				logDetails["status"] = 504
				return newErrorResponse(504), nil
			}
		}
		if strings.Contains(err.Error(), "connection refused") {
			logDetails["status"] = 502
			return newErrorResponse(502), nil
		}

		// 500 for all other errors
		return newErrorResponse(500), nil
	}
	return
}

func newErrorResponse(status int) (resp *http.Response) {
	resp = &http.Response{StatusCode: status}
	resp.Body = ioutil.NopCloser(strings.NewReader(""))
	return
}
<|endoftext|>


<commit_before>package fate

import (
	"math/rand"
	"strings"
	"sync/atomic"
	"testing"
)

// Make random corpuses of ngram data for scale testing.

func corpus(words []string, count int, sentlen func() int) []string {
	var sentences = make([]string, 0, count)
	for i := 0; i < count; i++ {
		sentences = append(sentences, sentence(words, sentlen()))
	}
	return sentences
}

func vocab(n int) []string {
	var words = make([]string, 0, n)
	for i := 0; i < n; i++ {
		words = append(words, randword(clamp(gauss(8, 5))))
	}
	return words
}

func gauss(mean, stddev float64) float64 {
	return rand.NormFloat64()*stddev + mean
}

func clamp(v float64) int {
	n := int(v + 0.5)
	if n < 1 {
		return 1
	}
	return n
}

var runes = []rune("abcdefghijklmnopqrstuvwxyz")

func randword(length int) string {
	var word = make([]rune, 0, length)
	for i := 0; i < length; i++ {
		word = append(word, runechoice(runes))
	}
	return string(word)
}

func runechoice(runes []rune) rune {
	return runes[rand.Intn(len(runes))]
}

func strchoice(strs []string) string {
	return strs[rand.Intn(len(strs))]
}

func sentence(words []string, n int) string {
	var ret = make([]string, 0, n)
	for i := 0; i < n; i++ {
		ret = append(ret, strchoice(words))
	}
	return strings.Join(ret, " ")
}

func BenchmarkLearn(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		model.Learn(sentences[i%len(sentences)])
	}
}

func BenchmarkReply(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})
	for _, sen := range sentences {
		model.Learn(sen)
	}

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		model.Reply(sentences[i%len(sentences)])
	}
}

func BenchmarkLearnParallel(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})

	b.ReportAllocs()
	b.ResetTimer()

	var i int32
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			j := atomic.AddInt32(&i, 1)
			model.Learn(sentences[int(j)%len(sentences)])
		}
	})
}

func BenchmarkReplyParallel(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})
	for _, sen := range sentences {
		model.Learn(sen)
	}

	b.ReportAllocs()
	b.ResetTimer()

	b.RunParallel(func(pb *testing.PB) {
		var i int
		for pb.Next() {
			model.Reply(sentences[i%len(sentences)])
			i++
		}
	})
}
<commit_msg>Add throughput stats to the learning benchmarks<commit_after>package fate

import (
	"math/rand"
	"strings"
	"sync/atomic"
	"testing"
)

// Make random corpuses of ngram data for scale testing.

func corpus(words []string, count int, sentlen func() int) []string {
	var sentences = make([]string, 0, count)
	for i := 0; i < count; i++ {
		sentences = append(sentences, sentence(words, sentlen()))
	}
	return sentences
}

func vocab(n int) []string {
	var words = make([]string, 0, n)
	for i := 0; i < n; i++ {
		words = append(words, randword(clamp(gauss(8, 5))))
	}
	return words
}

func gauss(mean, stddev float64) float64 {
	return rand.NormFloat64()*stddev + mean
}

func clamp(v float64) int {
	n := int(v + 0.5)
	if n < 1 {
		return 1
	}
	return n
}

var runes = []rune("abcdefghijklmnopqrstuvwxyz")

func randword(length int) string {
	var word = make([]rune, 0, length)
	for i := 0; i < length; i++ {
		word = append(word, runechoice(runes))
	}
	return string(word)
}

func runechoice(runes []rune) rune {
	return runes[rand.Intn(len(runes))]
}

func strchoice(strs []string) string {
	return strs[rand.Intn(len(strs))]
}

func sentence(words []string, n int) string {
	var ret = make([]string, 0, n)
	for i := 0; i < n; i++ {
		ret = append(ret, strchoice(words))
	}
	return strings.Join(ret, " ")
}

func BenchmarkLearn(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		sen := sentences[i%len(sentences)]
		model.Learn(sen)
		b.SetBytes(int64(len(sen)))
	}
}

func BenchmarkReply(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})
	for _, sen := range sentences {
		model.Learn(sen)
	}

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		model.Reply(sentences[i%len(sentences)])
	}
}

func BenchmarkLearnParallel(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})

	b.ReportAllocs()
	b.ResetTimer()

	var i int32
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			j := atomic.AddInt32(&i, 1)
			sen := sentences[int(j)%len(sentences)]
			model.Learn(sen)
			b.SetBytes(int64(len(sen)))
		}
	})
}

func BenchmarkReplyParallel(b *testing.B) {
	sentences := corpus(vocab(100000), b.N, func() int {
		return clamp(gauss(10, 5))
	})

	model := NewModel(Config{})
	for _, sen := range sentences {
		model.Learn(sen)
	}

	b.ReportAllocs()
	b.ResetTimer()

	b.RunParallel(func(pb *testing.PB) {
		var i int
		for pb.Next() {
			model.Reply(sentences[i%len(sentences)])
			i++
		}
	})
}
<|endoftext|>


<commit_before>package fads

import (
	"github.com/go-hep/fmom"
	"github.com/go-hep/hepmc"
)

type Particle interface {
	P4() fmom.P4
	Charge() int32
}

type MissingEt struct {
	MET float64 // missing transverse energy
	Phi float64 // missing energy azimuthal angle
}

// scalar sum of transverse momenta
type ScalarHt float64

// rho energy density
type Rho float64

type mcParticle struct {
	pid    int32 // pdg id number
	status int32 // particle status
	ispu   byte  // 0 or 1 for particles from pile-up interactions

	m1 int // particle 1st mother
	m2 int // particle 2nd mother
	d1 int // particle 1st daughter
	d2 int // particle 2nd daughter

	charge int     // particle charge
	mass   float64 // particle mass

	mom fmom.PxPyPzE // particle momentum (px,py,pz,e)
	pt  float64      // particle transverse momentum
	eta float64      // particle pseudo-rapidity
	phi float64      // particle azimuthal angle

	rapidity float64 // particle rapidity

	pos [4]float64 // particle vertex position (t,x,y,z)
}

type Photon struct {
	p4    fmom.PtEtaPhiM // photon momentum
	ehoem float64        // ratio of the hadronic over electromagnetic energy deposited in the calorimeter

	McPart *hepmc.Particle // generated particle
}

type Electron struct {
	p4          fmom.PtEtaPhiM // electron momentum
	charge      int32          // electron charge
	EhadOverEem float64        // ratio of the hadronic versus electromagnetic energy deposited in the calorimeter

	McPart *hepmc.Particle // generated particle
}

type Muon struct {
	p4     fmom.PtEtaPhiM // muon momentum
	charge int32          // muon charge

	McPart *hepmc.Particle // generated particle
}

type Jet struct {
	p4     fmom.PtEtaPhiM // jet momentum
	charge int32          // jet charge

	deta float64 // jet radius in pseudo-rapidity
	dphi float64 // jet radius in azimuthal angle

	btag   byte // 0 or 1 for a jet that has been tagged as containing a heavy quark
	tautag byte // 0 or 1 for a jet that has been tagged as a tau

	Constituents []Particle        // pointers to constituents
	McParts      []*hepmc.Particle // pointers to generated particles
}

type Track struct {
	pid    int32          // HEP ID number
	charge int32          // track charge
	p4     fmom.PtEtaPhiM // track momentum

	eta float64 // track pseudo-rapidity at the tracker edge
	phi float64 // track azimuthal angle at the tracker edge

	x float64 // track vertex position
	y float64 // track vertex position
	z float64 // track vertex position

	xout float64 // track vertex position at the tracker edge
	yout float64 // track vertex position at the tracker edge
	zout float64 // track vertex position at the tracker edge

	mc *hepmc.Particle // pointer to generated particle
}

type Tower struct {
	p4   fmom.EtEtaPhiM // calorimeter tower momentum
	ene  float64        // calorimeter tower energy
	eem  float64        // calorimeter tower electromagnetic energy
	ehad float64        // calorimter tower hadronic energy

	edges [4]float64 // calorimeter tower edges

	mcparts []*hepmc.Particle // pointers to generated particles
}

type Candidate struct {
	pid            int32   // HEP ID number
	status         int32   // particle status
	m1, m2, d1, d2 int32   // particle mothers and daughters
	charge         int32   // particle charge
	mass           float64 // particle mass

	ispu          byte // 0 or 1 for particles from pile-up interactions
	isconstituent byte // 0 or 1 for particles being constituents
	btag          byte // 0 or 1 for a candidate that has been tagged as containing a heavy quark
	tautag        byte // 0 or 1 for a candidate that has been tagged as a tau

	eem  float64 // electromagnetic energy
	ehad float64 // hadronic energy

	edges [4]float64
	deta  float64
	dphi  float64

	mom  fmom.P4
	pos  fmom.P4
	area fmom.P4

	arr []Candidate
}

// EOF
<commit_msg>datamodel: make Electron, Photon, ... implement the fads.Particle interface<commit_after>package fads

import (
	"github.com/go-hep/fmom"
	"github.com/go-hep/hepmc"
)

type Particle interface {
	P4() fmom.P4
	Charge() int32
}

type MissingEt struct {
	MET float64 // missing transverse energy
	Phi float64 // missing energy azimuthal angle
}

// scalar sum of transverse momenta
type ScalarHt float64

// rho energy density
type Rho float64

type mcParticle struct {
	Pid    int32 // pdg id number
	Status int32 // particle status
	IsPU   byte  // 0 or 1 for particles from pile-up interactions

	M1 int // particle 1st mother
	M2 int // particle 2nd mother
	D1 int // particle 1st daughter
	D2 int // particle 2nd daughter

	McCharge int     // particle charge
	Mass     float64 // particle mass

	Mom fmom.PxPyPzE // particle momentum (px,py,pz,e)

	Pt  float64 // particle transverse momentum
	Eta float64 // particle pseudo-rapidity
	Phi float64 // particle azimuthal angle

	Rapidity float64 // particle rapidity

	Pos [4]float64 // particle vertex position (t,x,y,z)
}

func (mc *mcParticle) P4() fmom.P4 {
	return &mc.Mom
}

func (mc *mcParticle) Charge() int32 {
	return int32(mc.McCharge)
}

type Photon struct {
	Mom    fmom.PtEtaPhiM // photon momentum
	EhoEem float64        // ratio of the hadronic over electromagnetic energy deposited in the calorimeter

	McPart *hepmc.Particle // generated particle
}

func (pho *Photon) P4() fmom.P4 {
	return &pho.Mom
}

func (pho *Photon) Charge() int32 {
	return 0
}

type Electron struct {
	Mom       fmom.PtEtaPhiM // electron momentum
	EleCharge int32          // electron charge
	EhoEem    float64        // ratio of the hadronic versus electromagnetic energy deposited in the calorimeter

	McPart *hepmc.Particle // generated particle
}

func (ele *Electron) P4() fmom.P4 {
	return &ele.Mom
}

func (ele *Electron) Charge() int32 {
	return ele.EleCharge
}

type Muon struct {
	Mom      fmom.PtEtaPhiM // muon momentum
	MuCharge int32          // muon charge

	McPart *hepmc.Particle // generated particle
}

func (muon *Muon) P4() fmom.P4 {
	return &muon.Mom
}

func (muon *Muon) Charge() int32 {
	return muon.MuCharge
}

type Jet struct {
	Mom       fmom.PtEtaPhiM // jet momentum
	JetCharge int32          // jet charge

	DEta float64 // jet radius in pseudo-rapidity
	DPhi float64 // jet radius in azimuthal angle

	BTag   byte // 0 or 1 for a jet that has been tagged as containing a heavy quark
	TauTag byte // 0 or 1 for a jet that has been tagged as a tau

	Constituents []Particle        // pointers to constituents
	McParts      []*hepmc.Particle // pointers to generated particles
}

func (jet *Jet) P4() fmom.P4 {
	return &jet.Mom
}

func (jet *Jet) Charge() int32 {
	return jet.JetCharge
}

type Track struct {
	Pid       int32          // HEP ID number
	Mom       fmom.PtEtaPhiM // track momentum
	TrkCharge int32          // track charge

	Eta float64 // track pseudo-rapidity at the tracker edge
	Phi float64 // track azimuthal angle at the tracker edge

	X float64 // track vertex position
	Y float64 // track vertex position
	Z float64 // track vertex position

	Xout float64 // track vertex position at the tracker edge
	Yout float64 // track vertex position at the tracker edge
	Zout float64 // track vertex position at the tracker edge

	McPart *hepmc.Particle // pointer to generated particle
}

func (trk *Track) P4() fmom.P4 {
	return &trk.Mom
}

func (trk *Track) Charge() int32 {
	return trk.TrkCharge
}

type Tower struct {
	Mom  fmom.EtEtaPhiM // calorimeter tower momentum
	Ene  float64        // calorimeter tower energy
	Eem  float64        // calorimeter tower electromagnetic energy
	Ehad float64        // calorimter tower hadronic energy

	Edges [4]float64 // calorimeter tower edges

	McParts []*hepmc.Particle // pointers to generated particles
}

func (tower *Tower) P4() fmom.P4 {
	return &tower.Mom
}

type Candidate struct {
	Pid            int32   // HEP ID number
	Status         int32   // particle status
	M1, M2, D1, D2 int32   // particle mothers and daughters
	CandCharge     int32   // particle charge
	CandMass       float64 // particle mass

	IsPU          byte // 0 or 1 for particles from pile-up interactions
	IsConstituent byte // 0 or 1 for particles being constituents
	BTag          byte // 0 or 1 for a candidate that has been tagged as containing a heavy quark
	TauTag        byte // 0 or 1 for a candidate that has been tagged as a tau

	Eem  float64 // electromagnetic energy
	Ehad float64 // hadronic energy

	Edges [4]float64
	DEta  float64
	DPhi  float64

	Mom  fmom.P4
	Pos  fmom.P4
	Area fmom.P4

	Arr []Candidate
}

func (cand *Candidate) P4() fmom.P4 {
	return cand.Mom
}

func (cand *Candidate) Charge() int32 {
	return cand.CandCharge
}

// EOF
<|endoftext|>


<commit_before><commit_msg>Changed the ParseMarkdown function to actually parse markdown.<commit_after><|endoftext|>


<commit_before>package main

// this is used as both source and destination MAC
var myMACAddr = [8]byte{ 0xb4, 0x75, 0x0e, 0x23, 0x5c, 0x87, // actual MAC address
    0x00, 0x00, // not used
}

var MyIfIndex = int(4)<commit_msg>myMACAddr.go deleted online with Bitbucket<commit_after><|endoftext|>


<commit_before>package lang

import (
	"reflect"
	"strconv"
	"testing"

	"github.com/hashicorp/terraform/config/lang/ast"
)

func TestEval(t *testing.T) {
	cases := []struct {
		Input      string
		Scope      *ast.BasicScope
		Error      bool
		Result     interface{}
		ResultType ast.Type
	}{
		{
			"foo",
			nil,
			false,
			"foo",
			ast.TypeString,
		},

		{
			"foo ${bar}",
			&ast.BasicScope{
				VarMap: map[string]ast.Variable{
					"bar": ast.Variable{
						Value: "baz",
						Type:  ast.TypeString,
					},
				},
			},
			false,
			"foo baz",
			ast.TypeString,
		},

		{
			"foo ${42+1}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42-1}",
			nil,
			false,
			"foo 41",
			ast.TypeString,
		},

		{
			"foo ${42*2}",
			nil,
			false,
			"foo 84",
			ast.TypeString,
		},

		{
			"foo ${42/2}",
			nil,
			false,
			"foo 21",
			ast.TypeString,
		},

		{
			"foo ${42%4}",
			nil,
			false,
			"foo 2",
			ast.TypeString,
		},

		{
			"foo ${42.0+1.0}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42.0+1}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42+1.0}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42+2*2}",
			nil,
			false,
			"foo 88",
			ast.TypeString,
		},

		{
			"foo ${42+(2*2)}",
			nil,
			false,
			"foo 46",
			ast.TypeString,
		},

		{
			"foo ${bar+1}",
			&ast.BasicScope{
				VarMap: map[string]ast.Variable{
					"bar": ast.Variable{
						Value: 41,
						Type:  ast.TypeInt,
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		{
			"foo ${rand()}",
			&ast.BasicScope{
				FuncMap: map[string]ast.Function{
					"rand": ast.Function{
						ReturnType: ast.TypeString,
						Callback: func([]interface{}) (interface{}, error) {
							return "42", nil
						},
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		{
			`foo ${rand("foo", "bar")}`,
			&ast.BasicScope{
				FuncMap: map[string]ast.Function{
					"rand": ast.Function{
						ReturnType:   ast.TypeString,
						Variadic:     true,
						VariadicType: ast.TypeString,
						Callback: func(args []interface{}) (interface{}, error) {
							var result string
							for _, a := range args {
								result += a.(string)
							}
							return result, nil
						},
					},
				},
			},
			false,
			"foo foobar",
			ast.TypeString,
		},

		// Testing implicit type conversions

		{
			"foo ${bar}",
			&ast.BasicScope{
				VarMap: map[string]ast.Variable{
					"bar": ast.Variable{
						Value: 42,
						Type:  ast.TypeInt,
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		{
			`foo ${foo("42")}`,
			&ast.BasicScope{
				FuncMap: map[string]ast.Function{
					"foo": ast.Function{
						ArgTypes:   []ast.Type{ast.TypeInt},
						ReturnType: ast.TypeString,
						Callback: func(args []interface{}) (interface{}, error) {
							return strconv.FormatInt(int64(args[0].(int)), 10), nil
						},
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},
	}

	for _, tc := range cases {
		node, err := Parse(tc.Input)
		if err != nil {
			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
		}

		out, outType, err := Eval(node, &EvalConfig{GlobalScope: tc.Scope})
		if (err != nil) != tc.Error {
			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
		}
		if outType != tc.ResultType {
			t.Fatalf("Bad: %s\n\nInput: %s", outType, tc.Input)
		}
		if !reflect.DeepEqual(out, tc.Result) {
			t.Fatalf("Bad: %#v\n\nInput: %s", out, tc.Input)
		}
	}
}
<commit_msg>config/lang: test case for multiline lang<commit_after>package lang

import (
	"reflect"
	"strconv"
	"testing"

	"github.com/hashicorp/terraform/config/lang/ast"
)

func TestEval(t *testing.T) {
	cases := []struct {
		Input      string
		Scope      *ast.BasicScope
		Error      bool
		Result     interface{}
		ResultType ast.Type
	}{
		{
			"foo",
			nil,
			false,
			"foo",
			ast.TypeString,
		},

		{
			"foo ${bar}",
			&ast.BasicScope{
				VarMap: map[string]ast.Variable{
					"bar": ast.Variable{
						Value: "baz",
						Type:  ast.TypeString,
					},
				},
			},
			false,
			"foo baz",
			ast.TypeString,
		},

		{
			"foo ${42+1}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42-1}",
			nil,
			false,
			"foo 41",
			ast.TypeString,
		},

		{
			"foo ${42*2}",
			nil,
			false,
			"foo 84",
			ast.TypeString,
		},

		{
			"foo ${42/2}",
			nil,
			false,
			"foo 21",
			ast.TypeString,
		},

		{
			"foo ${42%4}",
			nil,
			false,
			"foo 2",
			ast.TypeString,
		},

		{
			"foo ${42.0+1.0}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42.0+1}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42+1.0}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},

		{
			"foo ${42+2*2}",
			nil,
			false,
			"foo 88",
			ast.TypeString,
		},

		{
			"foo ${42+(2*2)}",
			nil,
			false,
			"foo 46",
			ast.TypeString,
		},

		{
			"foo ${bar+1}",
			&ast.BasicScope{
				VarMap: map[string]ast.Variable{
					"bar": ast.Variable{
						Value: 41,
						Type:  ast.TypeInt,
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		{
			"foo ${rand()}",
			&ast.BasicScope{
				FuncMap: map[string]ast.Function{
					"rand": ast.Function{
						ReturnType: ast.TypeString,
						Callback: func([]interface{}) (interface{}, error) {
							return "42", nil
						},
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		{
			`foo ${rand("foo", "bar")}`,
			&ast.BasicScope{
				FuncMap: map[string]ast.Function{
					"rand": ast.Function{
						ReturnType:   ast.TypeString,
						Variadic:     true,
						VariadicType: ast.TypeString,
						Callback: func(args []interface{}) (interface{}, error) {
							var result string
							for _, a := range args {
								result += a.(string)
							}
							return result, nil
						},
					},
				},
			},
			false,
			"foo foobar",
			ast.TypeString,
		},

		// Testing implicit type conversions

		{
			"foo ${bar}",
			&ast.BasicScope{
				VarMap: map[string]ast.Variable{
					"bar": ast.Variable{
						Value: 42,
						Type:  ast.TypeInt,
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		{
			`foo ${foo("42")}`,
			&ast.BasicScope{
				FuncMap: map[string]ast.Function{
					"foo": ast.Function{
						ArgTypes:   []ast.Type{ast.TypeInt},
						ReturnType: ast.TypeString,
						Callback: func(args []interface{}) (interface{}, error) {
							return strconv.FormatInt(int64(args[0].(int)), 10), nil
						},
					},
				},
			},
			false,
			"foo 42",
			ast.TypeString,
		},

		// Multiline
		{
			"foo ${42+\n1.0}",
			nil,
			false,
			"foo 43",
			ast.TypeString,
		},
	}

	for _, tc := range cases {
		node, err := Parse(tc.Input)
		if err != nil {
			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
		}

		out, outType, err := Eval(node, &EvalConfig{GlobalScope: tc.Scope})
		if (err != nil) != tc.Error {
			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
		}
		if outType != tc.ResultType {
			t.Fatalf("Bad: %s\n\nInput: %s", outType, tc.Input)
		}
		if !reflect.DeepEqual(out, tc.Result) {
			t.Fatalf("Bad: %#v\n\nInput: %s", out, tc.Input)
		}
	}
}
<|endoftext|>


<commit_before>package msi

const (
	STR_MS_T                    = "STR_PI"
	INT_MS_T                    = "INT_PI"
	INT16_MS_T                  = "INT16_PI"
	CHAR_MS_T                   = "CHAR_PI"
	BUF_LEN_MS_T                = "BUF_LEN_PI"
	STREAM_MS_T                 = "INT_PI"
	DOUBLE_MS_T                 = "DOUBLE_PI"
	FLOAT_MS_T                  = "FLOAT_PI"
	BOOL_MS_T                   = "BOOL_PI"
	DataObjInp_MS_T             = "DataObjInp_PI"
	DataObjCloseInp_MS_T        = "DataObjCloseInp_PI"
	DataObjCopyInp_MS_T         = "DataObjCopyInp_PI"
	DataObjReadInp_MS_T         = "dataObjReadInp_PI"
	DataObjWriteInp_MS_T        = "dataObjWriteInp_PI"
	DataObjLseekInp_MS_T        = "fileLseekInp_PI"
	DataObjLseekOut_MS_T        = "fileLseekOut_PI"
	KeyValPair_MS_T             = "KeyValPair_PI"
	TagStruct_MS_T              = "TagStruct_PI"
	CollInp_MS_T                = "CollInpNew_PI"
	ExecCmd_MS_T                = "ExecCmd_PI"
	ExecCmdOut_MS_T             = "ExecCmdOut_PI"
	RodsObjStat_MS_T            = "RodsObjStat_PI"
	VaultPathPolicy_MS_T        = "VaultPathPolicy_PI"
	StrArray_MS_T               = "StrArray_PI"
	IntArray_MS_T               = "IntArray_PI"
	GenQueryInp_MS_T            = "GenQueryInp_PI"
	GenQueryOut_MS_T            = "GenQueryOut_PI"
	XmsgTicketInfo_MS_T         = "XmsgTicketInfo_PI"
	SendXmsgInfo_MS_T           = "SendXmsgInfo_PI"
	GetXmsgTicketInp_MS_T       = "GetXmsgTicketInp_PI"
	SendXmsgInp_MS_T            = "SendXmsgInp_PI"
	RcvXmsgInp_MS_T             = "RcvXmsgInp_PI"
	RcvXmsgOut_MS_T             = "RcvXmsgOut_PI"
	StructFileExtAndRegInp_MS_T = "StructFileExtAndRegInp_PI"
	RuleSet_MS_T                = "RuleSet_PI"
	RuleStruct_MS_T             = "RuleStruct_PI"
	DVMapStruct_MS_T            = "DVMapStruct_PI"
	FNMapStruct_MS_T            = "FNMapStruct_PI"
	MsrvcStruct_MS_T            = "MsrvcStruct_PI"
	NcOpenInp_MS_T              = "NcOpenInp_PI"
	NcInqIdInp_MS_T             = "NcInqIdInp_PI"
	NcInqWithIdOut_MS_T         = "NcInqWithIdOut_PI"
	NcInqInp_MS_T               = "NcInqInp_PI"
	NcCloseInp_MS_T             = "NcCloseInp_PI"
	NcGetVarInp_MS_T            = "NcGetVarInp_PI"
	NcGetVarOut_MS_T            = "NcGetVarOut_PI"
	NcInqOut_MS_T               = "NcInqOut_PI"
	NcInqGrpsOut_MS_T           = "NcInqGrpsOut_PI"
	Dictionary_MS_T             = "Dictionary_PI"
	DictArray_MS_T              = "DictArray_PI"
	GenArray_MS_T               = "GenArray_PI"
	DataObjInfo_MS_T            = "DataObjInfo_PI"
)
<commit_msg>Create types.go<commit_after>package msi

// Types avaliable for use in msi.NewParam()
const (
	STR_MS_T                    = "STR_PI"
	INT_MS_T                    = "INT_PI"
	INT16_MS_T                  = "INT16_PI"
	CHAR_MS_T                   = "CHAR_PI"
	BUF_LEN_MS_T                = "BUF_LEN_PI"
	STREAM_MS_T                 = "INT_PI"
	DOUBLE_MS_T                 = "DOUBLE_PI"
	FLOAT_MS_T                  = "FLOAT_PI"
	BOOL_MS_T                   = "BOOL_PI"
	DataObjInp_MS_T             = "DataObjInp_PI"
	DataObjCloseInp_MS_T        = "DataObjCloseInp_PI"
	DataObjCopyInp_MS_T         = "DataObjCopyInp_PI"
	DataObjReadInp_MS_T         = "dataObjReadInp_PI"
	DataObjWriteInp_MS_T        = "dataObjWriteInp_PI"
	DataObjLseekInp_MS_T        = "fileLseekInp_PI"
	DataObjLseekOut_MS_T        = "fileLseekOut_PI"
	KeyValPair_MS_T             = "KeyValPair_PI"
	TagStruct_MS_T              = "TagStruct_PI"
	CollInp_MS_T                = "CollInpNew_PI"
	ExecCmd_MS_T                = "ExecCmd_PI"
	ExecCmdOut_MS_T             = "ExecCmdOut_PI"
	RodsObjStat_MS_T            = "RodsObjStat_PI"
	VaultPathPolicy_MS_T        = "VaultPathPolicy_PI"
	StrArray_MS_T               = "StrArray_PI"
	IntArray_MS_T               = "IntArray_PI"
	GenQueryInp_MS_T            = "GenQueryInp_PI"
	GenQueryOut_MS_T            = "GenQueryOut_PI"
	XmsgTicketInfo_MS_T         = "XmsgTicketInfo_PI"
	SendXmsgInfo_MS_T           = "SendXmsgInfo_PI"
	GetXmsgTicketInp_MS_T       = "GetXmsgTicketInp_PI"
	SendXmsgInp_MS_T            = "SendXmsgInp_PI"
	RcvXmsgInp_MS_T             = "RcvXmsgInp_PI"
	RcvXmsgOut_MS_T             = "RcvXmsgOut_PI"
	StructFileExtAndRegInp_MS_T = "StructFileExtAndRegInp_PI"
	RuleSet_MS_T                = "RuleSet_PI"
	RuleStruct_MS_T             = "RuleStruct_PI"
	DVMapStruct_MS_T            = "DVMapStruct_PI"
	FNMapStruct_MS_T            = "FNMapStruct_PI"
	MsrvcStruct_MS_T            = "MsrvcStruct_PI"
	NcOpenInp_MS_T              = "NcOpenInp_PI"
	NcInqIdInp_MS_T             = "NcInqIdInp_PI"
	NcInqWithIdOut_MS_T         = "NcInqWithIdOut_PI"
	NcInqInp_MS_T               = "NcInqInp_PI"
	NcCloseInp_MS_T             = "NcCloseInp_PI"
	NcGetVarInp_MS_T            = "NcGetVarInp_PI"
	NcGetVarOut_MS_T            = "NcGetVarOut_PI"
	NcInqOut_MS_T               = "NcInqOut_PI"
	NcInqGrpsOut_MS_T           = "NcInqGrpsOut_PI"
	Dictionary_MS_T             = "Dictionary_PI"
	DictArray_MS_T              = "DictArray_PI"
	GenArray_MS_T               = "GenArray_PI"
	DataObjInfo_MS_T            = "DataObjInfo_PI"
)
<|endoftext|>