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

package iptables

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"syscall"
)

// Adds the output of stderr to exec.ExitError
type Error struct {
	exec.ExitError
	msg string
}

func (e *Error) ExitStatus() int {
	return e.Sys().(syscall.WaitStatus).ExitStatus()
}

func (e *Error) Error() string {
	return fmt.Sprintf("exit status %v: %v", e.ExitStatus(), e.msg)
}

type IPTables struct {
	path string
}

func New() (*IPTables, error) {
	path, err := exec.LookPath("iptables")
	if err != nil {
		return nil, err
	}

	return &IPTables{path}, nil
}

// Exists checks if given rulespec in specified table/chain exists
func (ipt *IPTables) Exists(table, chain string, rulespec ...string) (bool, error) {
	checkPresent, err := getIptablesHasCheckCommand()
	if err != nil {
		log.Printf("Error checking iptables version, assuming version at least 1.4.11: %v", err)
		checkPresent = true
	}

	if !checkPresent {
		cmd := append([]string{"-A", chain}, rulespec...)
		return existsForOldIpTables(table, strings.Join(cmd, " "))
	} else {
		cmd := append([]string{"-t", table, "-C", chain}, rulespec...)
		err := ipt.run(cmd...)

		switch {
		case err == nil:
			return true, nil
		case err.(*Error).ExitStatus() == 1:
			return false, nil
		default:
			return false, err
		}
	}
}

// Insert inserts rulespec to specified table/chain (in specified pos)
func (ipt *IPTables) Insert(table, chain string, pos int, rulespec ...string) error {
	cmd := append([]string{"-t", table, "-I", chain, strconv.Itoa(pos)}, rulespec...)
	return ipt.run(cmd...)
}

// Append appends rulespec to specified table/chain
func (ipt *IPTables) Append(table, chain string, rulespec ...string) error {
	cmd := append([]string{"-t", table, "-A", chain}, rulespec...)
	return ipt.run(cmd...)
}

// AppendUnique acts like Append except that it won't add a duplicate
func (ipt *IPTables) AppendUnique(table, chain string, rulespec ...string) error {
	exists, err := ipt.Exists(table, chain, rulespec...)
	if err != nil {
		return err
	}

	if !exists {
		return ipt.Append(table, chain, rulespec...)
	}

	return nil
}

// Delete removes rulespec in specified table/chain
func (ipt *IPTables) Delete(table, chain string, rulespec ...string) error {
	cmd := append([]string{"-t", table, "-D", chain}, rulespec...)
	return ipt.run(cmd...)
}

// List rules in specified table/chain
func (ipt *IPTables) List(table, chain string) ([]string, error) {
	var stdout, stderr bytes.Buffer
	cmd := exec.Cmd{
		Path:   ipt.path,
		Args:   []string{ipt.path, "--wait", "-t", table, "-S", chain},
		Stdout: &stdout,
		Stderr: &stderr,
	}

	if err := cmd.Run(); err != nil {
		return nil, &Error{*(err.(*exec.ExitError)), stderr.String()}
	}

	rules := strings.Split(stdout.String(), "\n")
	if len(rules) > 0 && rules[len(rules)-1] == "" {
		rules = rules[:len(rules)-1]
	}

	return rules, nil
}

func (ipt *IPTables) NewChain(table, chain string) error {
	return ipt.run("-t", table, "-N", chain)
}

// ClearChain flushed (deletes all rules) in the specifed table/chain.
// If the chain does not exist, new one will be created
func (ipt *IPTables) ClearChain(table, chain string) error {
	err := ipt.NewChain(table, chain)

	switch {
	case err == nil:
		return nil
	case err.(*Error).ExitStatus() == 1:
		// chain already exists. Flush (clear) it.
		return ipt.run("-t", table, "-F", chain)
	default:
		return err
	}
}

// DeleteChain deletes the chain in the specified table.
// The chain must be empty
func (ipt *IPTables) DeleteChain(table, chain string) error {
	return ipt.run("-t", table, "-X", chain)
}

func (ipt *IPTables) run(args ...string) error {
	var stderr bytes.Buffer
	args = append([]string{"--wait"}, args...)
	cmd := exec.Cmd{
		Path:   ipt.path,
		Args:   append([]string{ipt.path}, args...),
		Stderr: &stderr,
	}

	if err := cmd.Run(); err != nil {
		return &Error{*(err.(*exec.ExitError)), stderr.String()}
	}

	return nil
}

// Checks if iptables has the "-C" flag
func getIptablesHasCheckCommand() (bool, error) {
	vstring, err := getIptablesVersionString()
	if err != nil {
		return false, err
	}

	v1, v2, v3, err := extractIptablesVersion(vstring)
	if err != nil {
		return false, err
	}

	return iptablesHasCheckCommand(v1, v2, v3), nil
}

// getIptablesVersion returns the first three components of the iptables version.
// e.g. "iptables v1.3.66" would return (1, 3, 66, nil)
func extractIptablesVersion(str string) (int, int, int, error) {
	versionMatcher := regexp.MustCompile("v([0-9]+)\\.([0-9]+)\\.([0-9]+)")
	result := versionMatcher.FindStringSubmatch(str)
	if result == nil {
		return 0, 0, 0, fmt.Errorf("no iptables version found in string: %s", str)
	}

	v1, err := strconv.Atoi(result[1])
	if err != nil {
		return 0, 0, 0, err
	}

	v2, err := strconv.Atoi(result[2])
	if err != nil {
		return 0, 0, 0, err
	}

	v3, err := strconv.Atoi(result[3])
	if err != nil {
		return 0, 0, 0, err
	}

	return v1, v2, v3, nil
}

// Runs "iptables --version" to get the version string
func getIptablesVersionString() (string, error) {
	cmd := exec.Command("iptables", "--version")
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return "", err
	}
	return out.String(), nil
}

// Checks if an iptables version is after 1.4.11, when --check was added
func iptablesHasCheckCommand(v1 int, v2 int, v3 int) bool {
	if v1 > 1 {
		return true
	}
	if v1 == 1 && v2 > 4 {
		return true
	}
	if v1 == 1 && v2 == 4 && v3 >= 11 {
		return true
	}
	return false
}

// Checks if a rule specification exists for a table
func existsForOldIpTables(table string, ruleSpec string) (bool, error) {
	cmd := exec.Command("iptables", "-t", table, "-S")
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return false, err
	}
	rules := out.String()
	return strings.Contains(rules, ruleSpec), nil
}
<commit_msg>remove wait option<commit_after>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package iptables

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"syscall"
)

// Adds the output of stderr to exec.ExitError
type Error struct {
	exec.ExitError
	msg string
}

func (e *Error) ExitStatus() int {
	return e.Sys().(syscall.WaitStatus).ExitStatus()
}

func (e *Error) Error() string {
	return fmt.Sprintf("exit status %v: %v", e.ExitStatus(), e.msg)
}

type IPTables struct {
	path string
}

func New() (*IPTables, error) {
	path, err := exec.LookPath("iptables")
	if err != nil {
		return nil, err
	}

	return &IPTables{path}, nil
}

// Exists checks if given rulespec in specified table/chain exists
func (ipt *IPTables) Exists(table, chain string, rulespec ...string) (bool, error) {
	checkPresent, err := getIptablesHasCheckCommand()
	if err != nil {
		log.Printf("Error checking iptables version, assuming version at least 1.4.11: %v", err)
		checkPresent = true
	}

	if !checkPresent {
		cmd := append([]string{"-A", chain}, rulespec...)
		return existsForOldIpTables(table, strings.Join(cmd, " "))
	} else {
		cmd := append([]string{"-t", table, "-C", chain}, rulespec...)
		err := ipt.run(cmd...)

		switch {
		case err == nil:
			return true, nil
		case err.(*Error).ExitStatus() == 1:
			return false, nil
		default:
			return false, err
		}
	}
}

// Insert inserts rulespec to specified table/chain (in specified pos)
func (ipt *IPTables) Insert(table, chain string, pos int, rulespec ...string) error {
	cmd := append([]string{"-t", table, "-I", chain, strconv.Itoa(pos)}, rulespec...)
	return ipt.run(cmd...)
}

// Append appends rulespec to specified table/chain
func (ipt *IPTables) Append(table, chain string, rulespec ...string) error {
	cmd := append([]string{"-t", table, "-A", chain}, rulespec...)
	return ipt.run(cmd...)
}

// AppendUnique acts like Append except that it won't add a duplicate
func (ipt *IPTables) AppendUnique(table, chain string, rulespec ...string) error {
	exists, err := ipt.Exists(table, chain, rulespec...)
	if err != nil {
		return err
	}

	if !exists {
		return ipt.Append(table, chain, rulespec...)
	}

	return nil
}

// Delete removes rulespec in specified table/chain
func (ipt *IPTables) Delete(table, chain string, rulespec ...string) error {
	cmd := append([]string{"-t", table, "-D", chain}, rulespec...)
	return ipt.run(cmd...)
}

// List rules in specified table/chain
func (ipt *IPTables) List(table, chain string) ([]string, error) {
	var stdout, stderr bytes.Buffer
	cmd := exec.Cmd{
		Path:   ipt.path,
		Args:   []string{ipt.path, "-t", table, "-S", chain},
		Stdout: &stdout,
		Stderr: &stderr,
	}

	if err := cmd.Run(); err != nil {
		return nil, &Error{*(err.(*exec.ExitError)), stderr.String()}
	}

	rules := strings.Split(stdout.String(), "\n")
	if len(rules) > 0 && rules[len(rules)-1] == "" {
		rules = rules[:len(rules)-1]
	}

	return rules, nil
}

func (ipt *IPTables) NewChain(table, chain string) error {
	return ipt.run("-t", table, "-N", chain)
}

// ClearChain flushed (deletes all rules) in the specifed table/chain.
// If the chain does not exist, new one will be created
func (ipt *IPTables) ClearChain(table, chain string) error {
	err := ipt.NewChain(table, chain)

	switch {
	case err == nil:
		return nil
	case err.(*Error).ExitStatus() == 1:
		// chain already exists. Flush (clear) it.
		return ipt.run("-t", table, "-F", chain)
	default:
		return err
	}
}

// DeleteChain deletes the chain in the specified table.
// The chain must be empty
func (ipt *IPTables) DeleteChain(table, chain string) error {
	return ipt.run("-t", table, "-X", chain)
}

func (ipt *IPTables) run(args ...string) error {
	var stderr bytes.Buffer
	// args = append([]string{"--wait"}, args...)
	cmd := exec.Cmd{
		Path:   ipt.path,
		Args:   append([]string{ipt.path}, args...),
		Stderr: &stderr,
	}

	if err := cmd.Run(); err != nil {
		return &Error{*(err.(*exec.ExitError)), stderr.String()}
	}

	return nil
}

// Checks if iptables has the "-C" flag
func getIptablesHasCheckCommand() (bool, error) {
	vstring, err := getIptablesVersionString()
	if err != nil {
		return false, err
	}

	v1, v2, v3, err := extractIptablesVersion(vstring)
	if err != nil {
		return false, err
	}

	return iptablesHasCheckCommand(v1, v2, v3), nil
}

// getIptablesVersion returns the first three components of the iptables version.
// e.g. "iptables v1.3.66" would return (1, 3, 66, nil)
func extractIptablesVersion(str string) (int, int, int, error) {
	versionMatcher := regexp.MustCompile("v([0-9]+)\\.([0-9]+)\\.([0-9]+)")
	result := versionMatcher.FindStringSubmatch(str)
	if result == nil {
		return 0, 0, 0, fmt.Errorf("no iptables version found in string: %s", str)
	}

	v1, err := strconv.Atoi(result[1])
	if err != nil {
		return 0, 0, 0, err
	}

	v2, err := strconv.Atoi(result[2])
	if err != nil {
		return 0, 0, 0, err
	}

	v3, err := strconv.Atoi(result[3])
	if err != nil {
		return 0, 0, 0, err
	}

	return v1, v2, v3, nil
}

// Runs "iptables --version" to get the version string
func getIptablesVersionString() (string, error) {
	cmd := exec.Command("iptables", "--version")
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return "", err
	}
	return out.String(), nil
}

// Checks if an iptables version is after 1.4.11, when --check was added
func iptablesHasCheckCommand(v1 int, v2 int, v3 int) bool {
	if v1 > 1 {
		return true
	}
	if v1 == 1 && v2 > 4 {
		return true
	}
	if v1 == 1 && v2 == 4 && v3 >= 11 {
		return true
	}
	return false
}

// Checks if a rule specification exists for a table
func existsForOldIpTables(table string, ruleSpec string) (bool, error) {
	cmd := exec.Command("iptables", "-t", table, "-S")
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return false, err
	}
	rules := out.String()
	return strings.Contains(rules, ruleSpec), nil
}
<|endoftext|>


<commit_before>package dsc

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/viant/toolbox"
)

type metaRecordMapper struct {
	converter        toolbox.Converter
	structType       interface{}
	columnToFieldMap map[string](map[string]string)
	usePointer       bool
}

//NewMetaRecordMapped creates a new MetaRecordMapped to map a data record to a struct, it takes target struct and flag if it is a pointer as parameters.
func NewMetaRecordMapped(targetType interface{}, usePointer bool) RecordMapper {
	structType := targetType
	if usePointer {
		var originalType = targetType.(reflect.Type).Elem()
		structType = originalType
	}
	return &metaRecordMapper{
		converter:        *toolbox.NewColumnConverter(""),
		structType:       structType,
		usePointer:       usePointer,
		columnToFieldMap: toolbox.NewFieldSettingByKey(targetType, "column")}
}

func (rm *metaRecordMapper) getValueMappingCount(columns []string) int {
	result := 0
	for _, key := range columns {
		if mapping, ok := rm.columnToFieldMap[strings.ToLower(key)]; ok {
			if _, found := mapping["valueMap"]; found {
				result++
			}
		}
	}

	return result
}

func (rm *metaRecordMapper) allocateValueMapByKey(columns []string) map[string]interface{} {
	var valuesPointers = make([]interface{}, rm.getValueMappingCount(columns))
	index := 0
	var result = make(map[string]interface{})
	for _, key := range columns {
		if mapping, ok := rm.columnToFieldMap[strings.ToLower(key)]; ok {
			if _, found := mapping["valueMap"]; found {
				result[key] = &valuesPointers[index]
				index++
			}
		}
	}
	return result
}

func (rm *metaRecordMapper) applyFieldMapValuesIfNeeded(fieldsValueMap map[string]interface{}, structPointer reflect.Value) error {
	for key, rawValue := range fieldsValueMap {

		valueMapping := rm.columnToFieldMap[strings.ToLower(key)]
		fieldName := valueMapping["fieldName"]
		field := structPointer.Elem().FieldByName(fieldName)
		unwrappedValue := reflect.ValueOf(rawValue).Elem()
		if unwrappedValue.IsNil() {
			if field.Kind() != reflect.Ptr {
				return fmt.Errorf("Failed to apply value map on %v, unable to set nil", fieldName)
			}
			continue
		}

		rawValue = unwrappedValue.Interface()
		var value string
		if valueAsBytes, ok := rawValue.([]byte); ok {
			value = string(valueAsBytes)
		} else {
			value = toolbox.AsString(rawValue)
		}

		valueMap := toolbox.MakeStringMap(valueMapping["valueMap"], ":", ",")
		stringValue := toolbox.AsString(value)
		if mappedValue, found := valueMap[stringValue]; found {
			fieldValuePointer := field.Addr().Interface()
			err := rm.converter.AssignConverted(fieldValuePointer, mappedValue)
			if err != nil {
				return fmt.Errorf("Failed to map record, unable convert,dur to %v", err)

			}
		} else {
			return fmt.Errorf("Failed to map record, unable to find valid mapping, want one of %s, but had %v", valueMap, stringValue)
		}

	}
	return nil
}

func (rm *metaRecordMapper) scanData(scanner Scanner) (result interface{}, err error) {
	structType := toolbox.DiscoverTypeByKind(rm.structType, reflect.Struct)
	structPointer := reflect.New(structType)
	resultStruct := structPointer.Elem()
	columns, _ := scanner.Columns()
	var fieldValuePointers = make([]interface{}, len(columns))
	var fieldsValueMap map[string]interface{}

	hasFieldValueMap := rm.getValueMappingCount(columns) > 0
	if hasFieldValueMap {
		fieldsValueMap = rm.allocateValueMapByKey(columns)
	}
	for i, key := range columns {

		if fieldMapping, ok := rm.columnToFieldMap[strings.ToLower(key)]; ok {
			fieldName := fieldMapping["fieldName"]
			field := resultStruct.FieldByName(fieldName)

			if _, found := fieldMapping["valueMap"]; found {
				fieldValuePointers[i] = fieldsValueMap[key]
				continue
			}

			fieldValuePointers[i] = field.Addr().Interface()

		} else {
			return nil, fmt.Errorf("Unable map column %v to the result", key)
		}
	}
	err = scanner.Scan(fieldValuePointers...)
	if err != nil {
		return nil, err
	}

	if hasFieldValueMap {
		err := rm.applyFieldMapValuesIfNeeded(fieldsValueMap, structPointer)
		if err != nil {
			return nil, err
		}
	}

	if !rm.usePointer {
		result = structPointer.Elem().Interface()
		return result, err
	}
	return structPointer.Interface(), err
}

func (rm *metaRecordMapper) mapFromValues(vaues []interface{}) (result interface{}, err error) {
	return nil, nil
}

func (rm *metaRecordMapper) Map(scanner Scanner) (result interface{}, err error) {
	return rm.scanData(scanner)
}

type columnarRecordMapper struct {
	usePointer  bool
	targetSlice reflect.Type
}

//NewColumnarRecordMapper creates a new ColumnarRecordMapper, to map a data record to slice.
func NewColumnarRecordMapper(usePointer bool, targetSlice reflect.Type) RecordMapper {
	return &columnarRecordMapper{usePointer, targetSlice}
}

func (rm *columnarRecordMapper) Map(scanner Scanner) (interface{}, error) {
	result, _, err := ScanRow(scanner)
	if err != nil {
		return nil, err
	}
	if rm.usePointer {
		return result, nil
	}
	return result, nil
}

type mapRecordMapper struct {
	usePointer  bool
	targetSlice reflect.Type
}

func (rm *mapRecordMapper) Map(scanner Scanner) (interface{}, error) {
	result, _, err := ScanRow(scanner)
	if err != nil {
		return nil, err
	}
	columns, _ := scanner.Columns()
	aMap := make(map[string]interface{})
	for i, column := range columns {
		aMap[column] = result[i]
	}
	if rm.usePointer {
		return &aMap, nil
	}
	return aMap, nil
}

//NewMapRecordMapper creates a new ColumnarRecordMapper, to map a data record to slice.
func NewMapRecordMapper(usePointer bool, targetSlice reflect.Type) RecordMapper {
	return &mapRecordMapper{usePointer, targetSlice}
}

//NewRecordMapper create a new record mapper, if struct is passed it would be MetaRecordMapper, or for slice ColumnRecordMapper
func NewRecordMapper(targetType reflect.Type) RecordMapper {
	switch targetType.Kind() {
	case reflect.Struct:
		var mapper = NewMetaRecordMapped(targetType, false)
		return mapper

	case reflect.Map:
		var mapper = NewMapRecordMapper(false, targetType)
		return mapper
	case reflect.Slice:
		var mapper = NewColumnarRecordMapper(false, targetType)
		return mapper
	case reflect.Ptr:
		if targetType.Elem().Kind() == reflect.Slice {
			var mapper = NewColumnarRecordMapper(true, targetType.Elem())
			return mapper
		} else if targetType.Elem().Kind() == reflect.Struct {
			var mapper = NewMetaRecordMapped(targetType, true)
			return mapper
		}
	default:
		panic("Unsupported type: " + targetType.Name())
	}
	return nil
}

//NewRecordMapperIfNeeded create a new mapper if passed in mapper is nil. It takes target type for the record mapper.
func NewRecordMapperIfNeeded(mapper RecordMapper, targetType reflect.Type) RecordMapper {
	if mapper != nil {
		return mapper
	}
	return NewRecordMapper(targetType)
}

//ScanRow takes scanner to scans row.
func ScanRow(scanner Scanner) ([]interface{}, []string, error) {
	columns, _ := scanner.Columns()

	count := len(columns)
	var valuePointers = make([]interface{}, count)
	var rowValues = make([]interface{}, count)

	for i := range rowValues {
		valuePointers[i] = &rowValues[i]
	}

	err := scanner.Scan(valuePointers...)
	if err != nil {
		return nil, nil, fmt.Errorf("Failed to scan row due to %v", err)
	}

	for i := range rowValues {
		var value interface{}
		rawValue := rowValues[i]
		b, ok := rawValue.([]byte)
		if ok {
			value = string(b)
		} else {
			value = rawValue
		}
		rowValues[i] = value
	}
	return rowValues, columns, nil
}
<commit_msg>reformated<commit_after>package dsc

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/viant/toolbox"
)

type metaRecordMapper struct {
	converter        toolbox.Converter
	structType       interface{}
	columnToFieldMap map[string](map[string]string)
	usePointer       bool
}

//NewMetaRecordMapped creates a new MetaRecordMapped to map a data record to a struct, it takes target struct and flag if it is a pointer as parameters.
func NewMetaRecordMapped(targetType interface{}, usePointer bool) RecordMapper {
	structType := targetType
	if usePointer {
		var originalType = targetType.(reflect.Type).Elem()
		structType = originalType
	}
	return &metaRecordMapper{
		converter:        *toolbox.NewColumnConverter(""),
		structType:       structType,
		usePointer:       usePointer,
		columnToFieldMap: toolbox.NewFieldSettingByKey(targetType, "column")}
}

func (rm *metaRecordMapper) getValueMappingCount(columns []string) int {
	result := 0
	for _, key := range columns {
		if mapping, ok := rm.columnToFieldMap[strings.ToLower(key)]; ok {
			if _, found := mapping["valueMap"]; found {
				result++
			}
		}
	}

	return result
}

func (rm *metaRecordMapper) allocateValueMapByKey(columns []string) map[string]interface{} {
	var valuesPointers = make([]interface{}, rm.getValueMappingCount(columns))
	index := 0
	var result = make(map[string]interface{})
	for _, key := range columns {
		if mapping, ok := rm.columnToFieldMap[strings.ToLower(key)]; ok {
			if _, found := mapping["valueMap"]; found {
				result[key] = &valuesPointers[index]
				index++
			}
		}
	}
	return result
}

func (rm *metaRecordMapper) applyFieldMapValuesIfNeeded(fieldsValueMap map[string]interface{}, structPointer reflect.Value) error {
	for key, rawValue := range fieldsValueMap {

		valueMapping := rm.columnToFieldMap[strings.ToLower(key)]
		fieldName := valueMapping["fieldName"]
		field := structPointer.Elem().FieldByName(fieldName)
		unwrappedValue := reflect.ValueOf(rawValue).Elem()
		if unwrappedValue.IsNil() {
			if field.Kind() != reflect.Ptr {
				return fmt.Errorf("Failed to apply value map on %v, unable to set nil", fieldName)
			}
			continue
		}

		rawValue = unwrappedValue.Interface()
		var value string
		if valueAsBytes, ok := rawValue.([]byte); ok {
			value = string(valueAsBytes)
		} else {
			value = toolbox.AsString(rawValue)
		}

		valueMap := toolbox.MakeStringMap(valueMapping["valueMap"], ":", ",")
		stringValue := toolbox.AsString(value)
		if mappedValue, found := valueMap[stringValue]; found {
			fieldValuePointer := field.Addr().Interface()
			err := rm.converter.AssignConverted(fieldValuePointer, mappedValue)
			if err != nil {
				return fmt.Errorf("Failed to map record, unable convert,dur to %v", err)

			}
		} else {
			return fmt.Errorf("Failed to map record, unable to find valid mapping, want one of %s, but had %v", valueMap, stringValue)
		}

	}
	return nil
}

func (rm *metaRecordMapper) scanData(scanner Scanner) (result interface{}, err error) {
	structType := toolbox.DiscoverTypeByKind(rm.structType, reflect.Struct)
	structPointer := reflect.New(structType)
	resultStruct := structPointer.Elem()
	columns, _ := scanner.Columns()
	var fieldValuePointers = make([]interface{}, len(columns))
	var fieldsValueMap map[string]interface{}

	hasFieldValueMap := rm.getValueMappingCount(columns) > 0
	if hasFieldValueMap {
		fieldsValueMap = rm.allocateValueMapByKey(columns)
	}
	for i, key := range columns {

		if fieldMapping, ok := rm.columnToFieldMap[strings.ToLower(key)]; ok {
			fieldName := fieldMapping["fieldName"]
			field := resultStruct.FieldByName(fieldName)

			if _, found := fieldMapping["valueMap"]; found {
				fieldValuePointers[i] = fieldsValueMap[key]
				continue
			}

			fieldValuePointers[i] = field.Addr().Interface()

		} else {
			return nil, fmt.Errorf("Unable map column %v to the result", key)
		}
	}
	err = scanner.Scan(fieldValuePointers...)
	if err != nil {
		return nil, err
	}

	if hasFieldValueMap {
		err := rm.applyFieldMapValuesIfNeeded(fieldsValueMap, structPointer)
		if err != nil {
			return nil, err
		}
	}

	if !rm.usePointer {
		result = structPointer.Elem().Interface()
		return result, err
	}
	return structPointer.Interface(), err
}

func (rm *metaRecordMapper) mapFromValues(vaues []interface{}) (result interface{}, err error) {
	return nil, nil
}

func (rm *metaRecordMapper) Map(scanner Scanner) (result interface{}, err error) {
	return rm.scanData(scanner)
}

type columnarRecordMapper struct {
	usePointer  bool
	targetSlice reflect.Type
}

//NewColumnarRecordMapper creates a new ColumnarRecordMapper, to map a data record to slice.
func NewColumnarRecordMapper(usePointer bool, targetSlice reflect.Type) RecordMapper {
	return &columnarRecordMapper{usePointer, targetSlice}
}

func (rm *columnarRecordMapper) Map(scanner Scanner) (interface{}, error) {
	result, _, err := ScanRow(scanner)
	if err != nil {
		return nil, err
	}
	if rm.usePointer {
		return result, nil
	}
	return result, nil
}

type mapRecordMapper struct {
	usePointer  bool
	targetSlice reflect.Type
}

func (rm *mapRecordMapper) Map(scanner Scanner) (interface{}, error) {
	result, _, err := ScanRow(scanner)
	if err != nil {
		return nil, err
	}
	columns, _ := scanner.Columns()
	aMap := make(map[string]interface{})
	for i, column := range columns {
		aMap[column] = result[i]
	}
	if rm.usePointer {
		return &aMap, nil
	}
	return aMap, nil
}

//NewMapRecordMapper creates a new ColumnarRecordMapper, to map a data record to slice.
func NewMapRecordMapper(usePointer bool, targetSlice reflect.Type) RecordMapper {
	return &mapRecordMapper{usePointer, targetSlice}
}

//NewRecordMapper create a new record mapper, if struct is passed it would be MetaRecordMapper, or for slice ColumnRecordMapper
func NewRecordMapper(targetType reflect.Type) RecordMapper {
	switch targetType.Kind() {
	case reflect.Struct:
		var mapper = NewMetaRecordMapped(targetType, false)
		return mapper

	case reflect.Map:
		var mapper = NewMapRecordMapper(false, targetType)
		return mapper
	case reflect.Slice:
		var mapper = NewColumnarRecordMapper(false, targetType)
		return mapper
	case reflect.Ptr:
		if targetType.Elem().Kind() == reflect.Slice {
			var mapper = NewColumnarRecordMapper(true, targetType.Elem())
			return mapper
		} else if targetType.Elem().Kind() == reflect.Struct {
			var mapper = NewMetaRecordMapped(targetType, true)
			return mapper
		}
	default:
		panic("Unsupported type: " + targetType.Name())
	}
	return nil
}

//NewRecordMapperIfNeeded create a new mapper if passed in mapper is nil. It takes target type for the record mapper.
func NewRecordMapperIfNeeded(mapper RecordMapper, targetType reflect.Type) RecordMapper {
	if mapper != nil {
		return mapper
	}
	return NewRecordMapper(targetType)
}

//ScanRow takes scanner to scans row.
func ScanRow(scanner Scanner) ([]interface{}, []string, error) {
	columns, _ := scanner.Columns()

	count := len(columns)
	var valuePointers = make([]interface{}, count)
	var rowValues = make([]interface{}, count)

	for i := range rowValues {
		valuePointers[i] = &rowValues[i]
	}

	err := scanner.Scan(valuePointers...)
	if err != nil {
		return nil, nil, fmt.Errorf("Failed to scan row due to %v", err)
	}

	for i := range rowValues {
		var value interface{}
		rawValue := rowValues[i]
		b, ok := rawValue.([]byte)
		if ok {
			value = string(b)
		} else {
			value = rawValue
		}
		rowValues[i] = value
	}
	return rowValues, columns, nil
}
<|endoftext|>


<commit_before>/**
随机数处理
create by gloomy 2017-03-29 22:11:23
*/
package gutil

import "math"

/**
四舍五入取舍
create by gloomy 2017-03-29 22:11:18
需要取舍的浮点数 取舍几位
*/
func Rounding(f float64, n int) float64 {
	pow10_n := math.Pow10(n)
	return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
}

/**
四舍五入取舍
create by gloomy 2017-03-29 22:18:46
除数 被除数 取舍几位
*/
func RoundingByInt(number, subNumber, n int) float64 {
	f := float64(number) / float64(subNumber)
	pow10_n := math.Pow10(n)
	return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
}

/**
四舍五入取舍 百分比
create by gloomy 2017-03-29 22:18:46
除数 被除数 取舍几位
*/
func RoundingPercentageByInt(number, subNumber, n int) float64 {
	f := float64(number) / float64(subNumber)
	n += 2
	pow10_n := math.Pow10(n)
	percentage := math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
	return percentage * 100
}
<commit_msg>修正参数为nan和inf时的错误判断<commit_after>/**
随机数处理
create by gloomy 2017-03-29 22:11:23
*/
package gutil

import (
	"math"
)

/**
四舍五入取舍
create by gloomy 2017-03-29 22:11:18
需要取舍的浮点数 取舍几位
*/
func Rounding(f float64, n int) float64 {
	pow10_n := math.Pow10(n)
	return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
}

/**
四舍五入取舍
create by gloomy 2017-03-29 22:18:46
除数 被除数 取舍几位
*/
func RoundingByInt(number, subNumber, n int) float64 {
	f := float64(number) / float64(subNumber)
	pow10_n := math.Pow10(n)
	return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
}

/**
四舍五入取舍 百分比
create by gloomy 2017-03-29 22:18:46
除数 被除数 取舍几位
*/
func RoundingPercentageByInt(number, subNumber, n int) float64 {
	f := float64(number) / float64(subNumber)
	n += 2
	pow10_n := math.Pow10(n)
	percentage := math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n
	if math.IsInf(percentage, 0) || math.IsNaN(percentage) {
		percentage = 0
	}
	return percentage * 100
}
<|endoftext|>


<commit_before>// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 ogletest

import (
	"testing"
)

var someInt int = -17

type testCase struct {
	candidate      interface{}
	expectedResult MatchResult
	expectedError  string
}

func checkTestCases(t *testing.T, matcher Matcher, cases []testCase) {
	for i, c := range cases {
		result, err := matcher.Matches(c.candidate)

		if result != c.expectedResult {
			t.Errorf(
				"Case %d (candidate %v): expected %v, got %v",
				i,
				c.candidate,
				c.expectedResult,
				result)
		}

		if err != c.expectedError {
			t.Errorf("Case %d: expected error %v, got %v", i, c.expectedError, err)
		}
	}
}

func TestNegativeInt8(t *testing.T) {
	matcher := Equals(int8(-17))
	desc := matcher.Description()
	expectedDesc := "-17"

	if desc != expectedDesc {
		t.Errorf("Expected description \"%s\", got \"%s\".", expectedDesc, desc)
	}

	cases := []testCase{
		// Various types of -17.
		testCase{-17, MATCH_TRUE, ""},
		testCase{-17.0, MATCH_TRUE, ""},
		testCase{-17 + 0i, MATCH_TRUE, ""},
		testCase{int(-17), MATCH_TRUE, ""},
		testCase{int8(-17), MATCH_TRUE, ""},
		testCase{int16(-17), MATCH_TRUE, ""},
		testCase{int32(-17), MATCH_TRUE, ""},
		testCase{int64(-17), MATCH_TRUE, ""},
		testCase{float32(-17), MATCH_TRUE, ""},
		testCase{float64(-17), MATCH_TRUE, ""},
		testCase{complex64(-17), MATCH_TRUE, ""},
		testCase{complex128(-17), MATCH_TRUE, ""},
		testCase{interface{}(int(-17)), MATCH_TRUE, ""},

		// Values that would be -17 in two's complement.
		testCase{uint((1 << 32) - 17), MATCH_FALSE, ""},
		testCase{uint8((1 << 8) - 17), MATCH_FALSE, ""},
		testCase{uint16((1 << 16) - 17), MATCH_FALSE, ""},
		testCase{uint32((1 << 32) - 17), MATCH_FALSE, ""},
		testCase{uint64((1 << 64) - 17), MATCH_FALSE, ""},

		// Non-equal values of signed integer type.
		testCase{int(-16), MATCH_FALSE, ""},
		testCase{int8(-16), MATCH_FALSE, ""},
		testCase{int16(-16), MATCH_FALSE, ""},
		testCase{int32(-16), MATCH_FALSE, ""},
		testCase{int64(-16), MATCH_FALSE, ""},

		// Non-equal values of other numeric types.
		testCase{float32(-17.1), MATCH_FALSE, ""},
		testCase{float32(-16.9), MATCH_FALSE, ""},
		testCase{complex64(-16), MATCH_FALSE, ""},
		testCase{complex64(-17 + 2i), MATCH_FALSE, ""},

		// Non-numeric types.
		testCase{uintptr((1 << 32) - 17), MATCH_UNDEFINED, "which is not numeric"},
		testCase{true, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[...]int{-17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{make(chan int), MATCH_UNDEFINED, "which is not numeric"},
		testCase{func() {}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{map[int]int{}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{&someInt, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[]int{-17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{"-17", MATCH_UNDEFINED, "which is not numeric"},
		testCase{testCase{}, MATCH_UNDEFINED, "which is not numeric"},
	}

	checkTestCases(t, matcher, cases)
}

func TestZeroInt8(t *testing.T) {
	matcher := Equals(int8(0))
	desc := matcher.Description()
	expectedDesc := "0"

	if desc != expectedDesc {
		t.Errorf("Expected description \"%s\", got \"%s\".", expectedDesc, desc)
	}

	cases := []testCase{
		// Various types of 17.
		testCase{0, MATCH_TRUE, ""},
		testCase{0.0, MATCH_TRUE, ""},
		testCase{0 + 0i, MATCH_TRUE, ""},
		testCase{int(0), MATCH_TRUE, ""},
		testCase{int8(0), MATCH_TRUE, ""},
		testCase{int16(0), MATCH_TRUE, ""},
		testCase{int32(0), MATCH_TRUE, ""},
		testCase{int64(0), MATCH_TRUE, ""},
		testCase{float32(0), MATCH_TRUE, ""},
		testCase{float64(0), MATCH_TRUE, ""},
		testCase{complex64(0), MATCH_TRUE, ""},
		testCase{complex128(0), MATCH_TRUE, ""},
		testCase{interface{}(int(0)), MATCH_TRUE, ""},
		testCase{uint(0), MATCH_TRUE, ""},
		testCase{uint8(0), MATCH_TRUE, ""},
		testCase{uint16(0), MATCH_TRUE, ""},
		testCase{uint32(0), MATCH_TRUE, ""},
		testCase{uint64(0), MATCH_TRUE, ""},

		// Non-equal values of numeric type.
		testCase{int(1), MATCH_FALSE, ""},
		testCase{int8(1), MATCH_FALSE, ""},
		testCase{int16(1), MATCH_FALSE, ""},
		testCase{int32(1), MATCH_FALSE, ""},
		testCase{int64(1), MATCH_FALSE, ""},
		testCase{float32(-0.1), MATCH_FALSE, ""},
		testCase{float32(0.1), MATCH_FALSE, ""},
		testCase{complex64(1), MATCH_FALSE, ""},
		testCase{complex64(0 + 2i), MATCH_FALSE, ""},

		// Non-numeric types.
		testCase{uintptr(0), MATCH_UNDEFINED, "which is not numeric"},
		testCase{true, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[...]int{0}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{make(chan int), MATCH_UNDEFINED, "which is not numeric"},
		testCase{func() {}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{map[int]int{}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{&someInt, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[]int{0}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{"0", MATCH_UNDEFINED, "which is not numeric"},
		testCase{testCase{}, MATCH_UNDEFINED, "which is not numeric"},
	}

	checkTestCases(t, matcher, cases)
}

func TestPositiveInt8(t *testing.T) {
	matcher := Equals(int8(17))
	desc := matcher.Description()
	expectedDesc := "17"

	if desc != expectedDesc {
		t.Errorf("Expected description \"%s\", got \"%s\".", expectedDesc, desc)
	}

	cases := []testCase{
		// Various types of 17.
		testCase{17, MATCH_TRUE, ""},
		testCase{17.0, MATCH_TRUE, ""},
		testCase{17 + 0i, MATCH_TRUE, ""},
		testCase{int(17), MATCH_TRUE, ""},
		testCase{int8(17), MATCH_TRUE, ""},
		testCase{int16(17), MATCH_TRUE, ""},
		testCase{int32(17), MATCH_TRUE, ""},
		testCase{int64(17), MATCH_TRUE, ""},
		testCase{float32(17), MATCH_TRUE, ""},
		testCase{float64(17), MATCH_TRUE, ""},
		testCase{complex64(17), MATCH_TRUE, ""},
		testCase{complex128(17), MATCH_TRUE, ""},
		testCase{interface{}(int(17)), MATCH_TRUE, ""},
		testCase{uint(17), MATCH_TRUE, ""},
		testCase{uint8(17), MATCH_TRUE, ""},
		testCase{uint16(17), MATCH_TRUE, ""},
		testCase{uint32(17), MATCH_TRUE, ""},
		testCase{uint64(17), MATCH_TRUE, ""},

		// Non-equal values of numeric type.
		testCase{int(16), MATCH_FALSE, ""},
		testCase{int8(16), MATCH_FALSE, ""},
		testCase{int16(16), MATCH_FALSE, ""},
		testCase{int32(16), MATCH_FALSE, ""},
		testCase{int64(16), MATCH_FALSE, ""},
		testCase{float32(16.9), MATCH_FALSE, ""},
		testCase{float32(17.1), MATCH_FALSE, ""},
		testCase{complex64(16), MATCH_FALSE, ""},
		testCase{complex64(17 + 2i), MATCH_FALSE, ""},

		// Non-numeric types.
		testCase{uintptr(17), MATCH_UNDEFINED, "which is not numeric"},
		testCase{true, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[...]int{17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{make(chan int), MATCH_UNDEFINED, "which is not numeric"},
		testCase{func() {}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{map[int]int{}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{&someInt, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[]int{17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{"17", MATCH_UNDEFINED, "which is not numeric"},
		testCase{testCase{}, MATCH_UNDEFINED, "which is not numeric"},
	}

	checkTestCases(t, matcher, cases)
}
<commit_msg>Added visual banners.<commit_after>// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 ogletest

import (
	"testing"
)

var someInt int = -17

////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////

type testCase struct {
	candidate      interface{}
	expectedResult MatchResult
	expectedError  string
}

func checkTestCases(t *testing.T, matcher Matcher, cases []testCase) {
	for i, c := range cases {
		result, err := matcher.Matches(c.candidate)

		if result != c.expectedResult {
			t.Errorf(
				"Case %d (candidate %v): expected %v, got %v",
				i,
				c.candidate,
				c.expectedResult,
				result)
		}

		if err != c.expectedError {
			t.Errorf("Case %d: expected error %v, got %v", i, c.expectedError, err)
		}
	}
}

////////////////////////////////////////////////////////////
// int8
////////////////////////////////////////////////////////////

func TestNegativeInt8(t *testing.T) {
	matcher := Equals(int8(-17))
	desc := matcher.Description()
	expectedDesc := "-17"

	if desc != expectedDesc {
		t.Errorf("Expected description \"%s\", got \"%s\".", expectedDesc, desc)
	}

	cases := []testCase{
		// Various types of -17.
		testCase{-17, MATCH_TRUE, ""},
		testCase{-17.0, MATCH_TRUE, ""},
		testCase{-17 + 0i, MATCH_TRUE, ""},
		testCase{int(-17), MATCH_TRUE, ""},
		testCase{int8(-17), MATCH_TRUE, ""},
		testCase{int16(-17), MATCH_TRUE, ""},
		testCase{int32(-17), MATCH_TRUE, ""},
		testCase{int64(-17), MATCH_TRUE, ""},
		testCase{float32(-17), MATCH_TRUE, ""},
		testCase{float64(-17), MATCH_TRUE, ""},
		testCase{complex64(-17), MATCH_TRUE, ""},
		testCase{complex128(-17), MATCH_TRUE, ""},
		testCase{interface{}(int(-17)), MATCH_TRUE, ""},

		// Values that would be -17 in two's complement.
		testCase{uint((1 << 32) - 17), MATCH_FALSE, ""},
		testCase{uint8((1 << 8) - 17), MATCH_FALSE, ""},
		testCase{uint16((1 << 16) - 17), MATCH_FALSE, ""},
		testCase{uint32((1 << 32) - 17), MATCH_FALSE, ""},
		testCase{uint64((1 << 64) - 17), MATCH_FALSE, ""},

		// Non-equal values of signed integer type.
		testCase{int(-16), MATCH_FALSE, ""},
		testCase{int8(-16), MATCH_FALSE, ""},
		testCase{int16(-16), MATCH_FALSE, ""},
		testCase{int32(-16), MATCH_FALSE, ""},
		testCase{int64(-16), MATCH_FALSE, ""},

		// Non-equal values of other numeric types.
		testCase{float32(-17.1), MATCH_FALSE, ""},
		testCase{float32(-16.9), MATCH_FALSE, ""},
		testCase{complex64(-16), MATCH_FALSE, ""},
		testCase{complex64(-17 + 2i), MATCH_FALSE, ""},

		// Non-numeric types.
		testCase{uintptr((1 << 32) - 17), MATCH_UNDEFINED, "which is not numeric"},
		testCase{true, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[...]int{-17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{make(chan int), MATCH_UNDEFINED, "which is not numeric"},
		testCase{func() {}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{map[int]int{}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{&someInt, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[]int{-17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{"-17", MATCH_UNDEFINED, "which is not numeric"},
		testCase{testCase{}, MATCH_UNDEFINED, "which is not numeric"},
	}

	checkTestCases(t, matcher, cases)
}

func TestZeroInt8(t *testing.T) {
	matcher := Equals(int8(0))
	desc := matcher.Description()
	expectedDesc := "0"

	if desc != expectedDesc {
		t.Errorf("Expected description \"%s\", got \"%s\".", expectedDesc, desc)
	}

	cases := []testCase{
		// Various types of 17.
		testCase{0, MATCH_TRUE, ""},
		testCase{0.0, MATCH_TRUE, ""},
		testCase{0 + 0i, MATCH_TRUE, ""},
		testCase{int(0), MATCH_TRUE, ""},
		testCase{int8(0), MATCH_TRUE, ""},
		testCase{int16(0), MATCH_TRUE, ""},
		testCase{int32(0), MATCH_TRUE, ""},
		testCase{int64(0), MATCH_TRUE, ""},
		testCase{float32(0), MATCH_TRUE, ""},
		testCase{float64(0), MATCH_TRUE, ""},
		testCase{complex64(0), MATCH_TRUE, ""},
		testCase{complex128(0), MATCH_TRUE, ""},
		testCase{interface{}(int(0)), MATCH_TRUE, ""},
		testCase{uint(0), MATCH_TRUE, ""},
		testCase{uint8(0), MATCH_TRUE, ""},
		testCase{uint16(0), MATCH_TRUE, ""},
		testCase{uint32(0), MATCH_TRUE, ""},
		testCase{uint64(0), MATCH_TRUE, ""},

		// Non-equal values of numeric type.
		testCase{int(1), MATCH_FALSE, ""},
		testCase{int8(1), MATCH_FALSE, ""},
		testCase{int16(1), MATCH_FALSE, ""},
		testCase{int32(1), MATCH_FALSE, ""},
		testCase{int64(1), MATCH_FALSE, ""},
		testCase{float32(-0.1), MATCH_FALSE, ""},
		testCase{float32(0.1), MATCH_FALSE, ""},
		testCase{complex64(1), MATCH_FALSE, ""},
		testCase{complex64(0 + 2i), MATCH_FALSE, ""},

		// Non-numeric types.
		testCase{uintptr(0), MATCH_UNDEFINED, "which is not numeric"},
		testCase{true, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[...]int{0}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{make(chan int), MATCH_UNDEFINED, "which is not numeric"},
		testCase{func() {}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{map[int]int{}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{&someInt, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[]int{0}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{"0", MATCH_UNDEFINED, "which is not numeric"},
		testCase{testCase{}, MATCH_UNDEFINED, "which is not numeric"},
	}

	checkTestCases(t, matcher, cases)
}

func TestPositiveInt8(t *testing.T) {
	matcher := Equals(int8(17))
	desc := matcher.Description()
	expectedDesc := "17"

	if desc != expectedDesc {
		t.Errorf("Expected description \"%s\", got \"%s\".", expectedDesc, desc)
	}

	cases := []testCase{
		// Various types of 17.
		testCase{17, MATCH_TRUE, ""},
		testCase{17.0, MATCH_TRUE, ""},
		testCase{17 + 0i, MATCH_TRUE, ""},
		testCase{int(17), MATCH_TRUE, ""},
		testCase{int8(17), MATCH_TRUE, ""},
		testCase{int16(17), MATCH_TRUE, ""},
		testCase{int32(17), MATCH_TRUE, ""},
		testCase{int64(17), MATCH_TRUE, ""},
		testCase{float32(17), MATCH_TRUE, ""},
		testCase{float64(17), MATCH_TRUE, ""},
		testCase{complex64(17), MATCH_TRUE, ""},
		testCase{complex128(17), MATCH_TRUE, ""},
		testCase{interface{}(int(17)), MATCH_TRUE, ""},
		testCase{uint(17), MATCH_TRUE, ""},
		testCase{uint8(17), MATCH_TRUE, ""},
		testCase{uint16(17), MATCH_TRUE, ""},
		testCase{uint32(17), MATCH_TRUE, ""},
		testCase{uint64(17), MATCH_TRUE, ""},

		// Non-equal values of numeric type.
		testCase{int(16), MATCH_FALSE, ""},
		testCase{int8(16), MATCH_FALSE, ""},
		testCase{int16(16), MATCH_FALSE, ""},
		testCase{int32(16), MATCH_FALSE, ""},
		testCase{int64(16), MATCH_FALSE, ""},
		testCase{float32(16.9), MATCH_FALSE, ""},
		testCase{float32(17.1), MATCH_FALSE, ""},
		testCase{complex64(16), MATCH_FALSE, ""},
		testCase{complex64(17 + 2i), MATCH_FALSE, ""},

		// Non-numeric types.
		testCase{uintptr(17), MATCH_UNDEFINED, "which is not numeric"},
		testCase{true, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[...]int{17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{make(chan int), MATCH_UNDEFINED, "which is not numeric"},
		testCase{func() {}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{map[int]int{}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{&someInt, MATCH_UNDEFINED, "which is not numeric"},
		testCase{[]int{17}, MATCH_UNDEFINED, "which is not numeric"},
		testCase{"17", MATCH_UNDEFINED, "which is not numeric"},
		testCase{testCase{}, MATCH_UNDEFINED, "which is not numeric"},
	}

	checkTestCases(t, matcher, cases)
}
<|endoftext|>


<commit_before>package main

import (
	"errors"

	log "github.com/gonet2/libs/nsq-logger"
	. "github.com/gonet2/libs/services/proto"
)

import (
	. "types"
)

var (
	ERROR_NOT_AUTHORIZED = errors.New("User not authorized")
)

// forward messages to game server
func forward(sess *Session, p []byte) error {
	frame := &Game_Frame{
		Type:    Game_Message,
		Message: p,
	}

	// validation
	if sess.Flag&SESS_AUTHORIZED == 0 {
		return ERROR_NOT_AUTHORIZED
	}

	// forward the frame to game
	if err := sess.Stream.Send(frame); err != nil {
		log.Error(err)
		return err
	}
	return nil
}
<commit_msg>update<commit_after>package main

import (
	"errors"

	log "github.com/gonet2/libs/nsq-logger"
	. "github.com/gonet2/libs/services/proto"
)

import (
	. "types"
)

var (
	ERROR_STREAM_NOT_OPEN = errors.New("stream not opened yet")
)

// forward messages to game server
func forward(sess *Session, p []byte) error {
	frame := &Game_Frame{
		Type:    Game_Message,
		Message: p,
	}

	// check stream
	if sess.Stream == nil {
		return ERROR_STREAM_NOT_OPEN
	}

	// forward the frame to game
	if err := sess.Stream.Send(frame); err != nil {
		log.Error(err)
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package api

import (
	"aguin/backend"
	"aguin/model"
	"aguin/utils"
	"aguin/validator"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"log"
	"net/http"
	"net/url"
	"time"
)

type RequestData struct {
	app       model.Application
	data      url.Values
}

type ApplicationSetting struct {
	dbSession *mgo.Session
}

func serveOK(render render.Render) {
	render.JSON(http.StatusOK, map[string]interface{}{"status": "OK"})
}

func serveForbidden(render render.Render) {
	render.JSON(http.StatusForbidden, map[string]interface{}{"status": "FORBIDDEN"})
}

func serveBadRequestData(render render.Render) {
	render.JSON(http.StatusBadRequest, map[string]interface{}{"status": "ERROR", "msg": "Invalid data"})
}

func serveBadRequestJson(render render.Render) {
	render.JSON(http.StatusBadRequest, map[string]interface{}{"status": "ERROR", "msg": "Invalid json"})
}

func serveInternalServerError(render render.Render) {
	render.JSON(http.StatusInternalServerError, map[string]interface{}{"status": "ERROR"})
}

func VerifyRequest() interface{} {
	return func(c martini.Context, res http.ResponseWriter, req *http.Request, render render.Render, log *log.Logger) {
		// Recover from panic and serve internal server error in json format
		defer func() {
			if r := recover(); r != nil {
				log.Print(r)
				serveInternalServerError(render)
			}
		}()

		apiKey := req.Header.Get("X-AGUIN-API-KEY")

		if apiKey == "" {
			serveForbidden(render)
			return
		}
		// If empty then data must be encrypted
		apiSecret := req.Header.Get("X-AGUIN-API-SECRET")

		app := model.Application{}

		setting := ApplicationSetting{}
		requestData := RequestData{}

		setting.dbSession = model.Session()

		defer setting.dbSession.Close()

		err := model.AppCollection(setting.dbSession).FindId(bson.ObjectIdHex(apiKey)).One(&app)

		log.Print(app, err)

		if err != nil {
			serveForbidden(render)
			return
		}

		requestData.app = app

		// parse form for data
		req.ParseForm()
		// if secret empty it must be crypted
		if apiSecret == "" {
			// decrypting data here

		} else {
			// have secret, compare with the one in database
			if apiSecret != app.Secret {
				serveForbidden(render)
				return
			}
		}
		requestData.data = req.Form
		c.Map(requestData)
		c.Map(setting)
		c.Next()
	}
}

func IndexGet(res http.ResponseWriter, req *http.Request, render render.Render, log *log.Logger, requestData RequestData, setting ApplicationSetting) {
	info, err := backend.Test()
	log.Println(info)
	log.Println(err)
	log.Print(requestData.app, requestData.data)

	var results []model.Entity
	da := []byte(requestData.data.Get("message"))

	data3, err := utils.Bytes2json(&da)
	if err != nil {
		log.Print(err)
		serveBadRequestJson(render)
		return
	}
	criteria := validator.ValidateSearch(data3)
	if criteria.Validated == false {
		serveBadRequestData(render)
		return
	}
	err = model.EntityCollection(setting.dbSession).Find(bson.M{"name": criteria.Entity, "appid": requestData.app.Id}).All(&results)

	render.JSON(http.StatusOK, results)
}

func IndexPost(res http.ResponseWriter, req *http.Request, render render.Render, log *log.Logger, requestData RequestData, setting ApplicationSetting) {
	da := []byte(requestData.data.Get("message"))
	data3, err := utils.Bytes2json(&da)
	if err != nil {
		log.Print(err)
		serveBadRequestJson(render)
		return
	}
	entity, data, validated := validator.ValidateEntity(data3)
	if validated && entity != "" {
		doc := model.Entity{}
		doc.Name = entity
		doc.AppId = requestData.app.Id
		doc.CreatedAt = time.Now()
		doc.Data = data
		err = doc.Save(setting.dbSession)
		if err == nil {
			serveOK(render)
			return
		} else {
			log.Print(err)
			serveInternalServerError(render)
			return
		}
	}
	serveBadRequestData(render)
}

func IndexStatus(log *log.Logger, render render.Render) {
	serveOK(render)
}

func NotFound(log *log.Logger, render render.Render) {
	render.JSON(http.StatusNotFound, map[string]interface{}{"status": "ERROR", "msg": "Not found"})
}
<commit_msg>Use own log for application views<commit_after>package api

import (
	"aguin/model"
	"aguin/utils"
	"aguin/validator"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"log"
	"net/http"
	"net/url"
	"time"
)

type RequestData struct {
	app       model.Application
	data      url.Values
}

type ApplicationSetting struct {
	dbSession *mgo.Session
	log *log.Logger // General log
}

func serveOK(render render.Render) {
	render.JSON(http.StatusOK, map[string]interface{}{"status": "OK"})
}

func serveForbidden(render render.Render) {
	render.JSON(http.StatusForbidden, map[string]interface{}{"status": "FORBIDDEN"})
}

func serveBadRequestData(render render.Render) {
	render.JSON(http.StatusBadRequest, map[string]interface{}{"status": "ERROR", "msg": "Invalid data"})
}

func serveBadRequestJson(render render.Render) {
	render.JSON(http.StatusBadRequest, map[string]interface{}{"status": "ERROR", "msg": "Invalid json"})
}

func serveInternalServerError(render render.Render) {
	render.JSON(http.StatusInternalServerError, map[string]interface{}{"status": "ERROR"})
}

func VerifyRequest() interface{} {
	return func(c martini.Context, res http.ResponseWriter, req *http.Request, render render.Render) {
		log := utils.GetLogger("aguin")
		// Recover from panic and serve internal server error in json format
		defer func() {
			if r := recover(); r != nil {
				log.Print(r)
				serveInternalServerError(render)
			}
		}()

		apiKey := req.Header.Get("X-AGUIN-API-KEY")

		if apiKey == "" {
			serveForbidden(render)
			return
		}
		// If empty then data must be encrypted
		apiSecret := req.Header.Get("X-AGUIN-API-SECRET")

		app := model.Application{}

		setting := ApplicationSetting{}
		requestData := RequestData{}
		setting.log = log
		setting.dbSession = model.Session()

		defer setting.dbSession.Close()

		err := model.AppCollection(setting.dbSession).FindId(bson.ObjectIdHex(apiKey)).One(&app)

		log.Print(app, err)

		if err != nil {
			serveForbidden(render)
			return
		}

		requestData.app = app

		// parse form for data
		req.ParseForm()
		// if secret empty it must be crypted
		if apiSecret == "" {
			// decrypting data here

		} else {
			// have secret, compare with the one in database
			if apiSecret != app.Secret {
				serveForbidden(render)
				return
			}
		}
		requestData.data = req.Form
		c.Map(requestData)
		c.Map(setting)
		c.Next()
	}
}

func IndexGet(res http.ResponseWriter, req *http.Request, render render.Render, requestData RequestData, setting ApplicationSetting) {
	log := setting.log

	var results []model.Entity
	da := []byte(requestData.data.Get("message"))

	data3, err := utils.Bytes2json(&da)
	if err != nil {
		log.Print(err)
		serveBadRequestJson(render)
		return
	}
	criteria := validator.ValidateSearch(data3)
	if criteria.Validated == false {
		serveBadRequestData(render)
		return
	}
	err = model.EntityCollection(setting.dbSession).Find(bson.M{"name": criteria.Entity, "appid": requestData.app.Id}).All(&results)

	render.JSON(http.StatusOK, results)
}

func IndexPost(res http.ResponseWriter, req *http.Request, render render.Render, requestData RequestData, setting ApplicationSetting) {
	log := setting.log
	da := []byte(requestData.data.Get("message"))
	data3, err := utils.Bytes2json(&da)
	if err != nil {
		log.Print(err)
		serveBadRequestJson(render)
		return
	}
	entity, data, validated := validator.ValidateEntity(data3)
	if validated && entity != "" {
		doc := model.Entity{}
		doc.Name = entity
		doc.AppId = requestData.app.Id
		doc.CreatedAt = time.Now()
		doc.Data = data
		err = doc.Save(setting.dbSession)
		if err == nil {
			serveOK(render)
			return
		} else {
			log.Print(err)
			serveInternalServerError(render)
			return
		}
	}
	serveBadRequestData(render)
}

func IndexStatus(render render.Render) {
	serveOK(render)
}

func NotFound(render render.Render) {
	render.JSON(http.StatusNotFound, map[string]interface{}{"status": "ERROR", "msg": "Not found"})
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/mh-cbon/go-bin-deb/stringexec"
)

// CreatePackage creates a debian package
func CreatePackage(reposlug, ghToken, email, version, archs, outbuild string, push bool) {

	x := strings.Split(reposlug, "/")
	user := x[0]
	name := x[1]

	gopath := os.Getenv("GOPATH")
	repoPath := filepath.Join(gopath, "src", "github.com", reposlug)
	fmt.Println("repoPath", repoPath)

	if _, err := os.Stat(repoPath); os.IsNotExist(err) {
		os.MkdirAll(repoPath, os.ModePerm)
		os.Chdir(repoPath)
		exec(`git clone https://github.com/%v.git .`, reposlug)
		exec(`git config user.name %v`, user)
		exec(`git config user.email %v`, email)
	}

	os.Chdir(repoPath)
	fmt.Println("Chdir", repoPath)

	exec(`sudo apt-get install build-essential lintian curl -y`)

	if tryexec(`latest -v`) != nil {
		exec(`go get -u github.com/mh-cbon/latest`)
	}

	if tryexec(`changelog -v`) != nil {
		exec(`latest -repo=%v`, "mh-cbon/changelog")
	}

	if tryexec(`go-bin-deb -v`) != nil {
		exec(`latest -repo=%v`, "mh-cbon/go-bin-deb")
	}

	dir, err := ioutil.TempDir("", "go-bin-deb")
	if err != nil {
		panic(err)
	}

	exec(`ls -al %v`, repoPath)
	exec(`ls -al %v`, outbuild)

	for _, arch := range strings.Split(archs, ",") {
		arch = strings.TrimSpace(arch)
		arch = strings.ToLower(arch)
		if arch == "i386" {
			arch = "386"
		} else if arch == "x64" {
			arch = "amd64"
		}

		workDir := filepath.Join(dir, arch)
		outFile := fmt.Sprintf("%v-%v.deb", name, arch)
		out := filepath.Join(outbuild, outFile)

		os.MkdirAll(workDir, os.ModePerm)
		exec(`ls -al %v`, workDir)

		os.MkdirAll(workDir, os.ModePerm)
		exec(`go-bin-deb generate -a %v --version %v -w %v -o %v`, arch, version, workDir, out)
	}

	exec(`ls -al .`)
	exec(`ls -al %v`, outbuild)

	if push == true {
		if tryexec(`gh-api-cli -v`) != nil {
			exec(`latest -repo=%v`, "mh-cbon/gh-api-cli")
		}
		exec(`gh-api-cli rm-assets --owner %v --repository %v --ver %v -t %v --glob %v`, user, name, version, ghToken, "*.deb")
		exec(`gh-api-cli upload-release-asset --owner %v --repository %v --ver %v -t %v --glob %q`, user, name, version, ghToken, outbuild+"/*.deb")
	}
}

var alwaysHide = map[string]string{}

func clean(s string) string {
	for search, replace := range alwaysHide {
		s = strings.Replace(s, search, replace, -1)
	}
	return s
}

func tryexec(w string, params ...interface{}) error {
	w = fmt.Sprintf(w, params...)
	cwd, err := os.Getwd()
	if err != nil {
		return err
	}
	fmt.Println("exec", clean(w))
	cmd, err := stringexec.Command(cwd, w)
	if err != nil {
		return err
	}
	// out, err := cmd.CombinedOutput()
	// sout := string(out)
	// fmt.Println(clean(sout))
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func exec(w string, params ...interface{}) {
	if err := tryexec(w, params...); err != nil {
		panic(err)
	}
}
<commit_msg>support: updated all support script<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/mh-cbon/go-bin-deb/stringexec"
)

// CreatePackage creates a debian package
func CreatePackage(reposlug, ghToken, email, version, archs, outbuild string, push bool) {

	x := strings.Split(reposlug, "/")
	user := x[0]
	name := x[1]

	gopath := os.Getenv("GOPATH")
	repoPath := filepath.Join(gopath, "src", "github.com", reposlug)
	fmt.Println("repoPath", repoPath)

	if _, err := os.Stat(repoPath); os.IsNotExist(err) {
		os.MkdirAll(repoPath, os.ModePerm)
		os.Chdir(repoPath)
		exec(`git clone https://github.com/%v.git .`, reposlug)
		exec(`git config user.name %v`, user)
		exec(`git config user.email %v`, email)
	}

	os.Chdir(repoPath)
	fmt.Println("Chdir", repoPath)
	exec(`ls -al %v`, repoPath)

	exec(`sudo apt-get install build-essential lintian curl -y`)
	exec(`ls -al %v`, repoPath)

	if tryexec(`latest -v`) != nil {
		exec(`go get -u github.com/mh-cbon/latest`)
	}
	exec(`ls -al %v`, repoPath)

	if tryexec(`changelog -v`) != nil {
		exec(`latest -repo=%v`, "mh-cbon/changelog")
	}
	exec(`ls -al %v`, repoPath)

	if tryexec(`go-bin-deb -v`) != nil {
		exec(`latest -repo=%v`, "mh-cbon/go-bin-deb")
	}
	exec(`ls -al %v`, repoPath)

	dir, err := ioutil.TempDir("", "go-bin-deb")
	if err != nil {
		panic(err)
	}

	exec(`ls -al %v`, repoPath)

	for _, arch := range strings.Split(archs, ",") {
		arch = strings.TrimSpace(arch)
		arch = strings.ToLower(arch)
		if arch == "i386" {
			arch = "386"
		} else if arch == "x64" {
			arch = "amd64"
		}

		workDir := filepath.Join(dir, arch)
		outFile := fmt.Sprintf("%v-%v.deb", name, arch)
		out := filepath.Join(outbuild, outFile)

		os.MkdirAll(workDir, os.ModePerm)
		exec(`ls -al %v`, workDir)

		os.MkdirAll(workDir, os.ModePerm)
		exec(`go-bin-deb generate -a %v --version %v -w %v -o %v`, arch, version, workDir, out)
	}

	exec(`ls -al .`)
	exec(`ls -al %v`, outbuild)

	if push == true {
		if tryexec(`gh-api-cli -v`) != nil {
			exec(`latest -repo=%v`, "mh-cbon/gh-api-cli")
		}
		exec(`gh-api-cli rm-assets --owner %v --repository %v --ver %v -t %v --glob %v`, user, name, version, ghToken, "*.deb")
		exec(`gh-api-cli upload-release-asset --owner %v --repository %v --ver %v -t %v --glob %q`, user, name, version, ghToken, outbuild+"/*.deb")
	}
}

var alwaysHide = map[string]string{}

func clean(s string) string {
	for search, replace := range alwaysHide {
		s = strings.Replace(s, search, replace, -1)
	}
	return s
}

func tryexec(w string, params ...interface{}) error {
	w = fmt.Sprintf(w, params...)
	cwd, err := os.Getwd()
	if err != nil {
		return err
	}
	fmt.Println("exec", clean(w))
	cmd, err := stringexec.Command(cwd, w)
	if err != nil {
		return err
	}
	// out, err := cmd.CombinedOutput()
	// sout := string(out)
	// fmt.Println(clean(sout))
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func exec(w string, params ...interface{}) {
	if err := tryexec(w, params...); err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>package rest

import (
	"encoding/json"
	"fmt"
	"socialapi/models"
	"socialapi/request"

	"github.com/google/go-querystring/query"
)

func GetPostWithRelatedData(id int64, q *request.Query, token string) (*models.ChannelMessageContainer, error) {
	v, err := query.Values(q)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("/message/%d/related?%s", id, v.Encode())
	cm := models.NewChannelMessageContainer()
	cmI, err := sendModelWithAuth("GET", url, cm, token)
	if err != nil {
		return nil, err
	}
	return cmI.(*models.ChannelMessageContainer), nil
}

func GetReplies(postId int64, accountId int64, groupName string) ([]*models.ChannelMessage, error) {
	url := fmt.Sprintf("/message/%d/reply?accountId=%d&groupName=%s", postId, accountId, groupName)
	res, err := sendRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	var replies []*models.ChannelMessage
	err = json.Unmarshal(res, &replies)
	if err != nil {
		return nil, err
	}

	return replies, nil
}

func AddReply(postId, channelId int64, token string) (*models.ChannelMessage, error) {
	cm := models.NewChannelMessage()
	cm.Body = "reply body"
	cm.InitialChannelId = channelId

	url := fmt.Sprintf("/message/%d/reply", postId)
	res, err := marshallAndSendRequestWithAuth("POST", url, cm, token)
	if err != nil {
		return nil, err
	}

	model := models.NewChannelMessageContainer()
	err = json.Unmarshal(res, model)
	if err != nil {
		return nil, err
	}

	return model.Message, nil
}

func DeleteReply(postId, replyId int64) error {
	url := fmt.Sprintf("/message/%d/reply/%d/delete", postId, replyId)
	_, err := sendRequest("DELETE", url, nil)
	if err != nil {
		return err
	}
	return nil
}
<commit_msg>go: remove socialapi replies<commit_after><|endoftext|>


<commit_before>package toolbox_test

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/viant/toolbox"
)

func Test_IsCompleteJSON(t *testing.T) {
	/*
	   	{
	   		input := `{"a":1, "b":2}`
	   		assert.True(t, toolbox.IsCompleteJSON(input))
	   	}
	   	{
	   		input := `{"a":1, "b":2}
	   {"a2":2, "b3":21}
	   {"a3":3, "b4:22
	   `
	   		assert.False(t, toolbox.IsCompleteJSON(input))
	   	}*/
	{
		input := `{"name":"abc"},{"id":"10}"`
		assert.True(t, toolbox.IsCompleteJSON(input))
	}

}

func Test_IsNewDelimitedJSON(t *testing.T) {

	{
		input := `{"a":1, "b":2}`
		assert.False(t, toolbox.IsNewLineDelimitedJSON(input))
	}
	{
		input := `{"a":1, "b":2}
{"a2":2, "b3":21}
{"a3":3, "b4:22}
`
		assert.True(t, toolbox.IsNewLineDelimitedJSON(input))
	}
	{
		input := `{"a":1, "b":2}
{"a2":2, "b3":21
{"a3":3, "b4:22}
`
		assert.False(t, toolbox.IsNewLineDelimitedJSON(input))
	}

}

func Test_JSONToMap(t *testing.T) {
	{
		input := `{"a":1, "b":2}`
		aMAp, err := toolbox.JSONToMap(input)
		assert.Nil(t, err)
		assert.True(t, len(aMAp) > 0)
	}
	{
		input := `{"a":1, "b":2}`
		aMAp, err := toolbox.JSONToMap([]byte(input))
		assert.Nil(t, err)
		assert.True(t, len(aMAp) > 0)
	}
	{
		input := `{"a":1, "b":2}`
		aMAp, err := toolbox.JSONToMap(strings.NewReader(input))
		assert.Nil(t, err)
		assert.True(t, len(aMAp) > 0)
	}
	{
		//error case
		_, err := toolbox.JSONToMap(1)
		assert.NotNil(t, err)
	}
	{
		//error case
		input := `{"a":1, "b":2`
		_, err := toolbox.JSONToMap(input)
		assert.NotNil(t, err)
	}

}

func Test_AsJSONText(t *testing.T) {
	{
		var soure = map[string]interface{}{
			"k": 1,
		}
		text, err := toolbox.AsJSONText(soure)
		assert.Nil(t, err)
		assert.EqualValues(t, "{\"k\":1}\n", text)
	}
	{
		type source struct {
			K int
		}
		text, err := toolbox.AsJSONText(&source{K: 1})
		assert.Nil(t, err)
		assert.EqualValues(t, "{\"K\":1}\n", text)
	}

	{

		text, err := toolbox.AsJSONText([]int{1, 3})
		assert.Nil(t, err)
		assert.EqualValues(t, "[1,3]\n", text)
	}

	{

		_, err := toolbox.AsJSONText(1)
		assert.NotNil(t, err)
	}

}

func Test_JSONToInterface(t *testing.T) {
	{
		input := `{"a":1, "b":2}`
		output, err := toolbox.JSONToInterface(input)
		if assert.Nil(t, err) {
			assert.NotNil(t, output)
			assert.True(t, toolbox.IsMap(output))
			aMap := toolbox.AsMap(output)
			assert.EqualValues(t, 1, aMap["a"])
			assert.EqualValues(t, 2, aMap["b"])
		}
	}
	{
		input := `[1,2]`
		output, err := toolbox.JSONToInterface(input)
		if assert.Nil(t, err) {
			assert.NotNil(t, output)
			assert.True(t, toolbox.IsSlice(output))
			aSlice := toolbox.AsSlice(output)
			assert.EqualValues(t, []interface{}{1.0, 2.0}, aSlice)
		}
	}
}
<commit_msg>patched test<commit_after>package toolbox_test

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/viant/toolbox"
)

func Test_IsCompleteJSON(t *testing.T) {
	{
		input := `{"a":1, "b":2}`
		assert.True(t, toolbox.IsCompleteJSON(input))
	}
	{
		input := `{"a":1, "b":2}
	   {"a2":2, "b3":21}
	   {"a3":3, "b4:22
	   `
		assert.False(t, toolbox.IsCompleteJSON(input))
	}
	{
		input := `{"name":"abc"},{"id":"10}"`
		assert.False(t, toolbox.IsCompleteJSON(input))
	}

}

func Test_IsNewDelimitedJSON(t *testing.T) {

	{
		input := `{"a":1, "b":2}`
		assert.False(t, toolbox.IsNewLineDelimitedJSON(input))
	}
	{
		input := `{"a":1, "b":2}
{"a2":2, "b3":21}
{"a3":3, "b4:22}
`
		assert.True(t, toolbox.IsNewLineDelimitedJSON(input))
	}
	{
		input := `{"a":1, "b":2}
{"a2":2, "b3":21
{"a3":3, "b4:22}
`
		assert.False(t, toolbox.IsNewLineDelimitedJSON(input))
	}

}

func Test_JSONToMap(t *testing.T) {
	{
		input := `{"a":1, "b":2}`
		aMAp, err := toolbox.JSONToMap(input)
		assert.Nil(t, err)
		assert.True(t, len(aMAp) > 0)
	}
	{
		input := `{"a":1, "b":2}`
		aMAp, err := toolbox.JSONToMap([]byte(input))
		assert.Nil(t, err)
		assert.True(t, len(aMAp) > 0)
	}
	{
		input := `{"a":1, "b":2}`
		aMAp, err := toolbox.JSONToMap(strings.NewReader(input))
		assert.Nil(t, err)
		assert.True(t, len(aMAp) > 0)
	}
	{
		//error case
		_, err := toolbox.JSONToMap(1)
		assert.NotNil(t, err)
	}
	{
		//error case
		input := `{"a":1, "b":2`
		_, err := toolbox.JSONToMap(input)
		assert.NotNil(t, err)
	}

}

func Test_AsJSONText(t *testing.T) {
	{
		var soure = map[string]interface{}{
			"k": 1,
		}
		text, err := toolbox.AsJSONText(soure)
		assert.Nil(t, err)
		assert.EqualValues(t, "{\"k\":1}\n", text)
	}
	{
		type source struct {
			K int
		}
		text, err := toolbox.AsJSONText(&source{K: 1})
		assert.Nil(t, err)
		assert.EqualValues(t, "{\"K\":1}\n", text)
	}

	{

		text, err := toolbox.AsJSONText([]int{1, 3})
		assert.Nil(t, err)
		assert.EqualValues(t, "[1,3]\n", text)
	}

	{

		_, err := toolbox.AsJSONText(1)
		assert.NotNil(t, err)
	}

}

func Test_JSONToInterface(t *testing.T) {
	{
		input := `{"a":1, "b":2}`
		output, err := toolbox.JSONToInterface(input)
		if assert.Nil(t, err) {
			assert.NotNil(t, output)
			assert.True(t, toolbox.IsMap(output))
			aMap := toolbox.AsMap(output)
			assert.EqualValues(t, 1, aMap["a"])
			assert.EqualValues(t, 2, aMap["b"])
		}
	}
	{
		input := `[1,2]`
		output, err := toolbox.JSONToInterface(input)
		if assert.Nil(t, err) {
			assert.NotNil(t, output)
			assert.True(t, toolbox.IsSlice(output))
			aSlice := toolbox.AsSlice(output)
			assert.EqualValues(t, []interface{}{1.0, 2.0}, aSlice)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"io/ioutil"
	"koding/db/mongodb/modelhelper"
	"koding/tools/config"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/koding/kite"
	kiteconfig "github.com/koding/kite/config"
	"github.com/koding/kite/kontrol"
)

const version = "0.0.5"

var (
	profile     = flag.String("c", "", "Configuration profile")
	region      = flag.String("r", "", "Region")
	peersString = flag.String("p", "", "Peers (comma seperated)")
)

func main() {
	flag.Parse()
	if *profile == "" {
		log.Fatal("Please specify profile via -c. Aborting.")
	}
	if *region == "" {
		log.Fatal("Please specify region via -r. Aborting.")
	}

	conf := config.MustConfig(*profile)
	modelhelper.Initialize(conf.Mongo)

	publicKey, err := ioutil.ReadFile(conf.NewKontrol.PublicKeyFile)
	if err != nil {
		log.Fatalln(err.Error())
	}

	privateKey, err := ioutil.ReadFile(conf.NewKontrol.PrivateKeyFile)
	if err != nil {
		log.Fatalln(err.Error())
	}

	hostname, err := os.Hostname()
	if err != nil {
		log.Fatalln(err.Error())
	}

	cwd, err := os.Getwd()
	if err != nil {
		log.Fatalln(err.Error())
	}

	datadir := filepath.Join(cwd, "kontrol-data-"+hostname)

	var peers []string
	if *peersString != "" {
		peers = strings.Split(*peersString, ",")
	}

	kiteConf := kiteconfig.MustGet()
	kiteConf.Port = conf.NewKontrol.Port
	kiteConf.Environment = conf.Environment
	kiteConf.Region = *region

	kon := kontrol.New(kiteConf, version, string(publicKey), string(privateKey))
	kon.Peers = peers
	kon.DataDir = datadir

	kon.AddAuthenticator("sessionID", authenticateFromSessionID)
	kon.MachineAuthenticate = authenticateFromKodingPassword

	if conf.NewKontrol.UseTLS {
		kon.Kite.UseTLSFile(conf.NewKontrol.CertFile, conf.NewKontrol.KeyFile)
		kon.Kite.Config.Port = conf.NewKontrol.Port
	}

	kon.Run()
}

func authenticateFromSessionID(r *kite.Request) error {
	username, err := findUsernameFromSessionID(r.Authentication.Key)
	if err != nil {
		return err
	}

	r.Username = username

	return nil
}

func findUsernameFromSessionID(sessionID string) (string, error) {
	session, err := modelhelper.GetSession(sessionID)
	if err != nil {
		return "", err
	}

	return session.Username, nil
}

func authenticateFromKodingPassword(r *kite.Request) error {
	password, err := r.Client.TellWithTimeout(
		"kite.getPass",
		10*time.Minute,
		"Enter password: ",
	)

	if err != nil {
		return err
	}

	_, err = modelhelper.CheckAndGetUser(r.Client.Kite.Username, password.MustString())
	return err
}
<commit_msg>kites/kontrol: change authentication type<commit_after>package main

import (
	"flag"
	"io/ioutil"
	"koding/db/mongodb/modelhelper"
	"koding/tools/config"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/koding/kite"
	kiteconfig "github.com/koding/kite/config"
	"github.com/koding/kite/kontrol"
)

const version = "0.0.5"

var (
	profile     = flag.String("c", "", "Configuration profile")
	region      = flag.String("r", "", "Region")
	peersString = flag.String("p", "", "Peers (comma seperated)")
)

func main() {
	flag.Parse()
	if *profile == "" {
		log.Fatal("Please specify profile via -c. Aborting.")
	}
	if *region == "" {
		log.Fatal("Please specify region via -r. Aborting.")
	}

	conf := config.MustConfig(*profile)
	modelhelper.Initialize(conf.Mongo)

	publicKey, err := ioutil.ReadFile(conf.NewKontrol.PublicKeyFile)
	if err != nil {
		log.Fatalln(err.Error())
	}

	privateKey, err := ioutil.ReadFile(conf.NewKontrol.PrivateKeyFile)
	if err != nil {
		log.Fatalln(err.Error())
	}

	hostname, err := os.Hostname()
	if err != nil {
		log.Fatalln(err.Error())
	}

	cwd, err := os.Getwd()
	if err != nil {
		log.Fatalln(err.Error())
	}

	datadir := filepath.Join(cwd, "kontrol-data-"+hostname)

	var peers []string
	if *peersString != "" {
		peers = strings.Split(*peersString, ",")
	}

	kiteConf := kiteconfig.MustGet()
	kiteConf.Port = conf.NewKontrol.Port
	kiteConf.Environment = conf.Environment
	kiteConf.Region = *region

	kon := kontrol.New(kiteConf, version, string(publicKey), string(privateKey))
	kon.Peers = peers
	kon.DataDir = datadir

	kon.AddAuthenticator("sessionID", authenticateFromSessionID)
	kon.MachineAuthenticate = authenticateFromKodingPassword

	if conf.NewKontrol.UseTLS {
		kon.Kite.UseTLSFile(conf.NewKontrol.CertFile, conf.NewKontrol.KeyFile)
		kon.Kite.Config.Port = conf.NewKontrol.Port
	}

	kon.Run()
}

func authenticateFromSessionID(r *kite.Request) error {
	username, err := findUsernameFromSessionID(r.Auth.Key)
	if err != nil {
		return err
	}

	r.Username = username

	return nil
}

func findUsernameFromSessionID(sessionID string) (string, error) {
	session, err := modelhelper.GetSession(sessionID)
	if err != nil {
		return "", err
	}

	return session.Username, nil
}

func authenticateFromKodingPassword(r *kite.Request) error {
	password, err := r.Client.TellWithTimeout(
		"kite.getPass",
		10*time.Minute,
		"Enter password: ",
	)

	if err != nil {
		return err
	}

	_, err = modelhelper.CheckAndGetUser(r.Client.Kite.Username, password.MustString())
	return err
}
<|endoftext|>


<commit_before>package bc

import (
	"testing"
	"math/rand"
	"time"
	"reflect"
)

func TestSerialization(t *testing.T) {

	var fundsTxData []fundsTx
	var accTxData []accTx
	b := newBlock()

	rand := rand.New(rand.NewSource(time.Now().Unix()))
	loopMax := int(rand.Uint32()%1000)
	for cnt := 0; cnt < loopMax; cnt++ {
		tx,_ := ConstrFundsTx(0x01, rand.Uint64()%100+1, rand.Uint64()%100+1, uint32(cnt), accA.Hash, accB.Hash, &PrivKeyA)
		b.addTx(&tx)
		fundsTxData = append(fundsTxData, tx)
	}

	loopMax = int(rand.Uint32()%1000)
	for cnt := 0; cnt < loopMax; cnt++ {
		tx,_ := ConstrAccTx(rand.Uint64()%123435, &RootPrivKey)
		b.addTx(&tx)
		accTxData = append(accTxData, tx)
	}

	b.finalizeBlock()
	encodedBlock := encodeBlock(*b)
	decodedBlock := decodeBlock(encodedBlock)

	validateBlock(decodedBlock)

	b.stateCopy = nil
	decodedBlock.stateCopy = nil

	if reflect.DeepEqual(b, decodedBlock) == false {
		t.Error("Either serialization or deserialization failed, blocks are not equal!")
	}
}<commit_msg>Fixed the test, such that the tx slices can be deeply compared.<commit_after>package bc

import (
	"testing"
	"math/rand"
	"time"
	"reflect"
)

//Tests block adding, verification, serialization and deserialization
func TestBlock(t *testing.T) {

	var fundsTxData []*fundsTx
	var accTxData []*accTx
	b := newBlock()

	rand := rand.New(rand.NewSource(time.Now().Unix()))
	loopMax := int(rand.Uint32()%10000)
	for cnt := 0; cnt < loopMax; cnt++ {
		tx,_ := ConstrFundsTx(0x01, rand.Uint64()%100+1, rand.Uint64()%100+1, uint32(cnt), accA.Hash, accB.Hash, &PrivKeyA)
		b.addTx(&tx)
		fundsTxData = append(fundsTxData, &tx)
	}

	loopMax = int(rand.Uint32()%10000)
	for cnt := 0; cnt < loopMax; cnt++ {
		tx,_ := ConstrAccTx(rand.Uint64()%123435, &RootPrivKey)
		b.addTx(&tx)
		accTxData = append(accTxData, &tx)
	}

	b.finalizeBlock()
	encodedBlock := encodeBlock(*b)
	decodedBlock := decodeBlock(encodedBlock)

	err := validateBlock(decodedBlock)

	if err != nil {
		t.Errorf("Block validation failed (%v)\n", err)
	}

	b.stateCopy = nil
	decodedBlock.stateCopy = nil

	if !reflect.DeepEqual(fundsTxData,decodedBlock.FundsTxData) {
		t.Error("FundsTx data is not properly serialized!")
	}

	if !reflect.DeepEqual(accTxData,decodedBlock.AccTxData) {
		t.Error("AccTx data is not properly serialized!")
	}

	if !reflect.DeepEqual(b, decodedBlock) {
		t.Error("Either serialization or deserialization failed, blocks are not equal!")
	}
}<|endoftext|>


<commit_before>package config

import "github.com/koding/runner"

type (
	// Config holds all the configuration variables of socialapi
	Config struct {
		// extend config with runner's
		runner.Config

		// Mongo holds full connection string
		Mongo string `env:"key=KONFIG_SOCIALAPI_MONGO                                 required"`

		// Protocol holds used protocol information
		Protocol string `env:"key=KONFIG_SOCIALAPI_PROTOCOL                           required"`

		Segment string `env:"key=KONFIG_SOCIALAPI_SEGMENT                             required"`

		// Email holds the required configuration data for email related workers
		Email Email

		// Sitemap holds configuration for Sitemap workers
		Sitemap Sitemap

		// Algolia holds configuration parameters for Aloglia search engine
		Algolia Algolia

		// Mixpanel holds configuration parameters for mixpanel
		Mixpanel Mixpanel

		// Limits holds limits for various cases
		Limits Limits

		Stripe Stripe

		Paypal Paypal

		// Holds access information for realtime message authenticator
		GateKeeper GateKeeper

		// Holds host and port information for integration worker
		Integration Integration

		// Holds host and port information for webhook middleware
		WebhookMiddleware WebhookMiddleware

		Kloud Kloud

		PaymentWebhook PaymentWebhook

		ProxyURL string

		CustomDomain CustomDomain

		GoogleapiServiceAccount GoogleapiServiceAccount

		Geoipdbpath string

		DisabledFeatures DisabledFeatures

		Janitor Janitor

		Github Github
	}

	// Email holds Email Workers' config
	Email struct {
		Host                    string `env:"key=KONFIG_SOCIALAPI_EMAIL_HOST                     required"`
		Protocol                string `env:"key=KONFIG_SOCIALAPI_EMAIL_PROTOCOL                 required"`
		DefaultFromMail         string `env:"key=KONFIG_SOCIALAPI_EMAIL_DEFAULTFROMMAIL          required"`
		DefaultFromName         string `env:"key=KONFIG_SOCIALAPI_EMAIL_DEFAULTFROMNAME          required"`
		ForcedRecipientEmail    string `env:"key=KONFIG_SOCIALAPI_EMAIL_FORCEDRECIPIENTEMAIL"`
		ForcedRecipientUsername string `env:"key=KONFIG_SOCIALAPI_EMAIL_FORCEDRECIPIENTUSERNAME"`
		Username                string `env:"key=KONFIG_SOCIALAPI_EMAIL_USERNAME                 required"`
		Password                string `env:"key=KONFIG_SOCIALAPI_EMAIL_PASSWORD                 required"`
		TemplateRoot            string `env:"key=KONFIG_SOCIALAPI_EMAIL_TEMPLATEROOT 	        default=workers/sitemap/files/"`
	}

	// Sitemap holds Sitemap Workers' config
	Sitemap struct {
		RedisDB        int    `env:"key=KONFIG_SOCIALAPI_SITEMAP_REDISDB"`
		UpdateInterval string `env:"key=KONFIG_SOCIALAPI_SITEMAP_UPDATEINTERVAL"`
	}

	// Algolia holds Algolia service credentials
	Algolia struct {
		AppId            string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APPID                        required"`
		ApiKey           string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APIKEY                       required"`
		ApiSecretKey     string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APISECRETKEY                 required"`
		IndexSuffix      string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_INDEXSUFFIX                  required"`
		ApiSearchOnlyKey string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APISEARCHONLYKEY             required"`
	}

	// Mixpanel holds mixpanel credentials
	Mixpanel struct {
		Token   string `env:"key=KONFIG_SOCIALAPI_MIXPANEL_TOKEN                            required"`
		Enabled bool   `env:"key=KONFIG_SOCIALAPI_MIXPANEL_ENABLED"`
	}

	// Limits holds application's various limits
	Limits struct {
		MessageBodyMinLen    int    `env:"key=KONFIG_SOCIALAPI_LIMITS_MESSAGEBODYMINLEN     required default=1"`
		PostThrottleDuration string `env:"key=KONFIG_SOCIALAPI_LIMITS_POSTTHROTTLEDURATION  required default=5s"`
		PostThrottleCount    int    `env:"key=KONFIG_SOCIALAPI_LIMITS_POSTTHROTTLECOUNT     required default=20"`
	}

	Stripe struct {
		SecretToken string `env:"key=KONFIG_SOCIALAPI_STRIPE_SECRETTOKEN"`
	}

	Paypal struct {
		Username  string `env:"key=KONFIG_SOCIALAPI_PAYPAL_USERNAME"`
		Password  string `env:"key=KONFIG_SOCIALAPI_PAYPAL_PASSWORD"`
		Signature string `env:"key=KONFIG_SOCIALAPI_PAYPAL_SIGNATURE"`
		ReturnUrl string `env:"key=KONFIG_SOCIALAPI_PAYPAL_RETURNURL"`
		CancelUrl string `env:"key=KONFIG_SOCIALAPI_PAYPAL_CANCELURL"`
		IsSandbox bool   `env:"key=KONFIG_SOCIALAPI_PAYPAL_ISANDBOX"`
	}

	GateKeeper struct {
		Host   string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_HOST"`
		Port   string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PORT"`
		Pubnub Pubnub
	}

	Integration struct {
		Host string `env:"key=KONFIG_SOCIALAPI_INTEGRATION_HOST"`
		Port string `env:"key=KONFIG_SOCIALAPI_INTEGRATION_PORT"`
	}

	WebhookMiddleware struct {
		Host string `env:"key=KONFIG_SOCIALAPI_WEBHOOKMIDDLEWARE_HOST"`
		Port string `env:"key=KONFIG_SOCIALAPI_WEBHOOKMIDDLEWARE_PORT"`
	}

	Pubnub struct {
		PublishKey    string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_PUBLISHKEY"`
		SubscribeKey  string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_SUBSCRIBEKEY"`
		SecretKey     string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_SECRETKEY"`
		ServerAuthKey string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_SERVERAUTHKEY"`
		Enabled       bool   `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_ENABLED"`
		Origin        string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_ORIGIN"`
	}

	CustomDomain struct {
		Public string `env:"key=KONFIG_SOCIALAPI_CUSTOMDOMAIN_PUBLIC"`
		Local  string `env:"key=KONFIG_SOCIALAPI_CUSTOMDOMAIN_LOCAL"`
	}

	Kloud struct {
		SecretKey string `env:"key=KONFIG_SOCIALAPI_KLOUD_SECRETKEY"`
		Address   string `env:"key=KONFIG_SOCIALAPI_KLOUD_ADDRESS"`
	}

	PaymentWebhook struct {
		Port      string `env:"key=KONFIG_SOCIALAPI_PAYMENTWEBHOOK_PORT"`
		Debug     bool   `env:"key=KONFIG_SOCIALAPI_PAYMENTWEBHOOK_DEBUG"`
		SecretKey string `env:"key=KONFIG_SOCIALAPI_PAYMENTWEBHOOK_SECRETKEY"`
	}

	GoogleapiServiceAccount struct {
		ClientId              string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_CLIENTID"`
		ClientSecret          string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_CLIENTSECRET"`
		ServiceAccountEmail   string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_SERVICEACCOUNTEMAIL"`
		ServiceAccountKeyFile string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_SERVICEACCOUNTKEYFILE"`
	}

	DisabledFeatures struct {
		Moderation bool
		BotChannel bool
	}

	Janitor struct {
		Port      string `env:"key=KONFIG_SOCIALAPI_JANITOR_PORT"`
		SecretKey string `env:"key=KONFIG_SOCIALAPI_JANITOR_SECRETKEY"`
	}

	Github struct {
		ClientId     string `env:"key=KONFIG_SOCIALAPI_GITHUB_CLIENTID"`
		ClientSecret string `env:"key=KONFIG_SOCIALAPI_GITHUB_CLIENTSECRET"`
		RedirectUri  string `env:"key=KONFIG_SOCIALAPI_GITHUB_REDIRECTURI"`
	}
)
<commit_msg>config: add slack credentials into configTypes<commit_after>package config

import "github.com/koding/runner"

type (
	// Config holds all the configuration variables of socialapi
	Config struct {
		// extend config with runner's
		runner.Config

		// Mongo holds full connection string
		Mongo string `env:"key=KONFIG_SOCIALAPI_MONGO                                 required"`

		// Protocol holds used protocol information
		Protocol string `env:"key=KONFIG_SOCIALAPI_PROTOCOL                           required"`

		Segment string `env:"key=KONFIG_SOCIALAPI_SEGMENT                             required"`

		// Email holds the required configuration data for email related workers
		Email Email

		// Sitemap holds configuration for Sitemap workers
		Sitemap Sitemap

		// Algolia holds configuration parameters for Aloglia search engine
		Algolia Algolia

		// Mixpanel holds configuration parameters for mixpanel
		Mixpanel Mixpanel

		// Limits holds limits for various cases
		Limits Limits

		Stripe Stripe

		Paypal Paypal

		// Holds access information for realtime message authenticator
		GateKeeper GateKeeper

		// Holds host and port information for integration worker
		Integration Integration

		// Holds host and port information for webhook middleware
		WebhookMiddleware WebhookMiddleware

		Kloud Kloud

		PaymentWebhook PaymentWebhook

		ProxyURL string

		CustomDomain CustomDomain

		GoogleapiServiceAccount GoogleapiServiceAccount

		Geoipdbpath string

		DisabledFeatures DisabledFeatures

		Janitor Janitor

		Github Github

		Slack Slack
	}

	// Email holds Email Workers' config
	Email struct {
		Host                    string `env:"key=KONFIG_SOCIALAPI_EMAIL_HOST                     required"`
		Protocol                string `env:"key=KONFIG_SOCIALAPI_EMAIL_PROTOCOL                 required"`
		DefaultFromMail         string `env:"key=KONFIG_SOCIALAPI_EMAIL_DEFAULTFROMMAIL          required"`
		DefaultFromName         string `env:"key=KONFIG_SOCIALAPI_EMAIL_DEFAULTFROMNAME          required"`
		ForcedRecipientEmail    string `env:"key=KONFIG_SOCIALAPI_EMAIL_FORCEDRECIPIENTEMAIL"`
		ForcedRecipientUsername string `env:"key=KONFIG_SOCIALAPI_EMAIL_FORCEDRECIPIENTUSERNAME"`
		Username                string `env:"key=KONFIG_SOCIALAPI_EMAIL_USERNAME                 required"`
		Password                string `env:"key=KONFIG_SOCIALAPI_EMAIL_PASSWORD                 required"`
		TemplateRoot            string `env:"key=KONFIG_SOCIALAPI_EMAIL_TEMPLATEROOT 	        default=workers/sitemap/files/"`
	}

	// Sitemap holds Sitemap Workers' config
	Sitemap struct {
		RedisDB        int    `env:"key=KONFIG_SOCIALAPI_SITEMAP_REDISDB"`
		UpdateInterval string `env:"key=KONFIG_SOCIALAPI_SITEMAP_UPDATEINTERVAL"`
	}

	// Algolia holds Algolia service credentials
	Algolia struct {
		AppId            string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APPID                        required"`
		ApiKey           string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APIKEY                       required"`
		ApiSecretKey     string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APISECRETKEY                 required"`
		IndexSuffix      string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_INDEXSUFFIX                  required"`
		ApiSearchOnlyKey string `env:"key=KONFIG_SOCIALAPI_ALGOLIA_APISEARCHONLYKEY             required"`
	}

	// Mixpanel holds mixpanel credentials
	Mixpanel struct {
		Token   string `env:"key=KONFIG_SOCIALAPI_MIXPANEL_TOKEN                            required"`
		Enabled bool   `env:"key=KONFIG_SOCIALAPI_MIXPANEL_ENABLED"`
	}

	// Limits holds application's various limits
	Limits struct {
		MessageBodyMinLen    int    `env:"key=KONFIG_SOCIALAPI_LIMITS_MESSAGEBODYMINLEN     required default=1"`
		PostThrottleDuration string `env:"key=KONFIG_SOCIALAPI_LIMITS_POSTTHROTTLEDURATION  required default=5s"`
		PostThrottleCount    int    `env:"key=KONFIG_SOCIALAPI_LIMITS_POSTTHROTTLECOUNT     required default=20"`
	}

	Stripe struct {
		SecretToken string `env:"key=KONFIG_SOCIALAPI_STRIPE_SECRETTOKEN"`
	}

	Paypal struct {
		Username  string `env:"key=KONFIG_SOCIALAPI_PAYPAL_USERNAME"`
		Password  string `env:"key=KONFIG_SOCIALAPI_PAYPAL_PASSWORD"`
		Signature string `env:"key=KONFIG_SOCIALAPI_PAYPAL_SIGNATURE"`
		ReturnUrl string `env:"key=KONFIG_SOCIALAPI_PAYPAL_RETURNURL"`
		CancelUrl string `env:"key=KONFIG_SOCIALAPI_PAYPAL_CANCELURL"`
		IsSandbox bool   `env:"key=KONFIG_SOCIALAPI_PAYPAL_ISANDBOX"`
	}

	GateKeeper struct {
		Host   string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_HOST"`
		Port   string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PORT"`
		Pubnub Pubnub
	}

	Integration struct {
		Host string `env:"key=KONFIG_SOCIALAPI_INTEGRATION_HOST"`
		Port string `env:"key=KONFIG_SOCIALAPI_INTEGRATION_PORT"`
	}

	WebhookMiddleware struct {
		Host string `env:"key=KONFIG_SOCIALAPI_WEBHOOKMIDDLEWARE_HOST"`
		Port string `env:"key=KONFIG_SOCIALAPI_WEBHOOKMIDDLEWARE_PORT"`
	}

	Pubnub struct {
		PublishKey    string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_PUBLISHKEY"`
		SubscribeKey  string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_SUBSCRIBEKEY"`
		SecretKey     string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_SECRETKEY"`
		ServerAuthKey string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_SERVERAUTHKEY"`
		Enabled       bool   `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_ENABLED"`
		Origin        string `env:"key=KONFIG_SOCIALAPI_GATEKEEPER_PUBNUB_ORIGIN"`
	}

	CustomDomain struct {
		Public string `env:"key=KONFIG_SOCIALAPI_CUSTOMDOMAIN_PUBLIC"`
		Local  string `env:"key=KONFIG_SOCIALAPI_CUSTOMDOMAIN_LOCAL"`
	}

	Kloud struct {
		SecretKey string `env:"key=KONFIG_SOCIALAPI_KLOUD_SECRETKEY"`
		Address   string `env:"key=KONFIG_SOCIALAPI_KLOUD_ADDRESS"`
	}

	PaymentWebhook struct {
		Port      string `env:"key=KONFIG_SOCIALAPI_PAYMENTWEBHOOK_PORT"`
		Debug     bool   `env:"key=KONFIG_SOCIALAPI_PAYMENTWEBHOOK_DEBUG"`
		SecretKey string `env:"key=KONFIG_SOCIALAPI_PAYMENTWEBHOOK_SECRETKEY"`
	}

	GoogleapiServiceAccount struct {
		ClientId              string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_CLIENTID"`
		ClientSecret          string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_CLIENTSECRET"`
		ServiceAccountEmail   string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_SERVICEACCOUNTEMAIL"`
		ServiceAccountKeyFile string `env:"key=KONFIG_SOCIALAPI_GOOGLEAPISERVICEACCOUNT_SERVICEACCOUNTKEYFILE"`
	}

	DisabledFeatures struct {
		Moderation bool
		BotChannel bool
	}

	Janitor struct {
		Port      string `env:"key=KONFIG_SOCIALAPI_JANITOR_PORT"`
		SecretKey string `env:"key=KONFIG_SOCIALAPI_JANITOR_SECRETKEY"`
	}

	Github struct {
		ClientId     string `env:"key=KONFIG_SOCIALAPI_GITHUB_CLIENTID"`
		ClientSecret string `env:"key=KONFIG_SOCIALAPI_GITHUB_CLIENTSECRET"`
		RedirectUri  string `env:"key=KONFIG_SOCIALAPI_GITHUB_REDIRECTURI"`
	}

	Slack struct {
		ClientId     string `env:"key=KONFIG_SOCIALAPI_SLACK_CLIENTID"`
		ClientSecret string `env:"key=KONFIG_SOCIALAPI_SLACK_CLIENTSECRET"`
		RedirectUri  string `env:"key=KONFIG_SOCIALAPI_SLACK_REDIRECTURI"`
	}
)
<|endoftext|>