<commit_before>package xslt

import (
	"container/list"
	"fmt"
	"github.com/moovweb/gokogiri/xml"
	"github.com/moovweb/gokogiri/xpath"
	"strings"
	"unicode/utf8"
)

type CompiledStep interface {
	Compile(node xml.Node)
	Apply(node xml.Node, context *ExecutionContext)
}

type Template struct {
	Name     string
	Mode     string
	Match    string
	Priority float64
	Children []CompiledStep
	Node     xml.Node
}

// Literal result elements are any elements in a template
// that are not in the xsl namespace.
// They are copied to the output document.
type LiteralResultElement struct {
	Node     xml.Node
	Children []CompiledStep
}

// Stylesheet text nodes
type TextOutput struct {
	Content string
}

// Used to represent an xsl:variable or xsl:param
type Variable struct {
	Name     string
	Node     xml.Node
	Children []CompiledStep
	Value    interface{}
}

// Compile the variable.
//
// TODO: compile the XPath expression and determine if it is a constant
func (i *Variable) Compile(node xml.Node) {
	i.Name = i.Node.Attr("name")
	for cur := node.FirstChild(); cur != nil; cur = cur.NextSibling() {
		res := CompileSingleNode(cur)
		if res != nil {
			res.Compile(cur)
			i.Children = append(i.Children, res)
		}
	}
}

// Applying a variable node is calculating its value.
func (i *Variable) Apply(node xml.Node, context *ExecutionContext) {
	scope := i.Node.Attr("select")
	// if @select
	if scope != "" {
		e := xpath.Compile(scope)
		var err error
		context.RegisterXPathNamespaces(i.Node)
		i.Value, err = context.EvalXPath(node, e)
		if err != nil {
			fmt.Println("Error evaluating variable", i.Name, err)
		}
		//fmt.Println("VARIABLE SELECT", i.Name, i.Value)
		return
	}

	if len(i.Children) == 0 {
		//fmt.Println("VARIABLE NIL", name, i.Value)
		i.Value = nil
		return
	}

	// if multiple children, return nodeset
	curOutput := context.OutputNode
	context.OutputNode = context.Output.CreateElementNode("RVT")
	context.PushStack()
	for _, c := range i.Children {
		c.Apply(node, context)
		switch v := c.(type) {
		case *Variable:
			_ = context.DeclareLocalVariable(v.Name, "", v)
		}
	}
	context.PopStack()
	i.Value = nil
	var outNodes xml.Nodeset
	for cur := context.OutputNode.FirstChild(); cur != nil; cur = cur.NextSibling() {
		outNodes = append(outNodes, cur)
	}
	i.Value = outNodes
	context.OutputNode = curOutput
	//fmt.Println("VARIABLE NODES", name, i.Value)
}

func (e *LiteralResultElement) Compile(node xml.Node) {
	for cur := node.FirstChild(); cur != nil; cur = cur.NextSibling() {
		res := CompileSingleNode(cur)
		if res != nil {
			res.Compile(cur)
			e.Children = append(e.Children, res)
		}
	}
}

func ResolveAlias(style *Stylesheet, alias, auri string) (prefix, uri string) {
	k, ok := style.NamespaceAlias[alias]
	//short circuit if prefix is not aliased
	if !ok {
		return alias, auri
	}
	for uri, prefix = range style.NamespaceMapping {
		if k == prefix {
			return
		}
	}
	return "", ""
}

func (e *LiteralResultElement) IsExtension(node xml.Node, context *ExecutionContext) bool {
	ns := e.Node.Namespace()
	prefix, _ := context.Style.NamespaceMapping[ns]
	if prefix != "" {
		for _, ex := range context.Style.ExtensionPrefixes {
			if ex == prefix {
				return true
			}
		}
	}
	return false
}

func (e *LiteralResultElement) Apply(node xml.Node, context *ExecutionContext) {
	//TODO: recognize extension elements at compile time
	if e.IsExtension(node, context) {
		for _, c := range e.Children {
			inst := c.(*XsltInstruction)
			if inst != nil && inst.Name == "fallback" {
				c.Apply(node, context)
			}
		}
		return
	}

	r := context.Output.CreateElementNode(e.Node.Name())
	context.OutputNode.AddChild(r)
	context.DeclareStylesheetNamespacesIfRoot(r)
	ns := e.Node.Namespace()
	if ns != "" {
		prefix, _ := context.Style.NamespaceMapping[ns]
		_, ns = ResolveAlias(context.Style, prefix, ns)
		//TODO: handle aliases
		r.SetNamespace(prefix, ns)
	}

	attsets := ""
	for _, attr := range e.Node.Attributes() {
		//fmt.Println(attr.Namespace(), attr.Name(), attr.Content())
		txt := attr.Content()
		if strings.ContainsRune(txt, '{') {
			txt = evalAVT(txt, node, context)
		}
		if attr.Namespace() != "" {
			if attr.Namespace() == XSLT_NAMESPACE {
				if attr.Name() == "use-attribute-sets" {
					attsets = txt
				}
			} else {
				r.SetNsAttr(attr.Namespace(), attr.Name(), txt)
			}
		} else {
			r.SetAttr(attr.Name(), txt)
		}
	}

	old := context.OutputNode
	context.OutputNode = r

	if attsets != "" {
		asets := strings.Fields(attsets)
		for _, attsetname := range asets {
			a := context.Style.LookupAttributeSet(attsetname)
			if a != nil {
				a.Apply(node, context)
			}
		}
	}
	for _, c := range e.Children {
		c.Apply(node, context)
		switch v := c.(type) {
		case *Variable:
			_ = context.DeclareLocalVariable(v.Name, "", v)
		}
	}
	context.OutputNode = old
}

// Evaluate an attribute value template
func evalAVT(input string, node xml.Node, context *ExecutionContext) (out string) {
	var start, pos int
	var inSQlit, inDQlit bool
	for {
		if pos >= len(input) {
			break
		}
		r, width := utf8.DecodeRuneInString(input[pos:])
		pos += width
		if r == '\'' {
			inSQlit = !inSQlit
		}
		if r == '"' {
			inDQlit = !inDQlit
		}
		if r == '{' {
			// if we're not the last character
			if pos < len(input) {
				// check for doubled opening brace
				peek, w := utf8.DecodeRuneInString(input[pos:])
				if peek == '{' {
					out = out + input[start:pos]
					pos += w
					start = pos
					continue
				}
			}
			out = out + input[start:pos-width]
			start = pos
		}
		if r == '}' {
			if inSQlit || inDQlit {
				continue
			}
			// if we're not the last character
			if pos < len(input) {
				// check for doubled closing brace
				peek, w := utf8.DecodeRuneInString(input[pos:])
				if peek == '}' {
					out = out + input[start:pos]
					pos += w
					start = pos
					continue
				}
			}
			expr := input[start : pos-width]
			ret, _ := context.EvalXPath(node, expr)
			switch val := ret.(type) {
			case []xml.Node:
				for _, n := range val {
					out = out + n.Content()
				}
			case float64:
				out = out + fmt.Sprintf("%v", val)
			case string:
				out = out + val
			}
			start = pos
		}
	}
	out = out + input[start:pos]
	return
}

func (t *TextOutput) Compile(node xml.Node) {
}

func (t *TextOutput) Apply(node xml.Node, context *ExecutionContext) {
	if context.UseCDataSection(context.OutputNode) {
		r := context.Output.CreateCDataNode(t.Content)
		context.OutputNode.AddChild(r)
	} else {
		r := context.Output.CreateTextNode(t.Content)
		context.OutputNode.AddChild(r)
	}
}

func (template *Template) AddChild(child CompiledStep) {
	template.Children = append(template.Children, child)
}

func (template *Template) CompileContent(node xml.Node) {
	//parse the content and register the match pattern
	for cur := node.FirstChild(); cur != nil; cur = cur.NextSibling() {
		res := CompileSingleNode(cur)
		if res != nil {
			res.Compile(cur)
			template.AddChild(res)
		}
	}
}

func CompileSingleNode(node xml.Node) (step CompiledStep) {
	switch node.NodeType() {
	case xml.XML_ELEMENT_NODE:
		ns := node.Namespace()
		// element, extension namespace = extension
		if ns == XSLT_NAMESPACE {
			// element, XSLT namespace = instruction
			switch node.Name() {
			case "variable":
				step = &Variable{Node: node}
			case "param", "with-param":
				step = &Variable{Node: node}
			default:
				step = &XsltInstruction{Name: node.Name(), Node: node}
			}
		} else {
			// element other namespace = LRE
			step = &LiteralResultElement{Node: node}
		}
	// text, CDATA node
	case xml.XML_TEXT_NODE, xml.XML_CDATA_SECTION_NODE:
		if !IsBlank(node) {
			step = &TextOutput{Content: node.Content()}
		}
	}
	return
}

func selectParamValue(param *Variable, withParams []*Variable) (out *Variable) {
	for _, p := range withParams {
		if param.Name == p.Name {
			return p
		}
	}
	return param
}

func (template *Template) Apply(node xml.Node, context *ExecutionContext, params []*Variable) {
	//init local scope
	oldStack := context.Stack
	context.Stack = *new(list.List)
	context.PushStack()

	//for each node in compiled template body
	// if xsl:message
	// if forwards-compatible
	//   apply fallback
	for _, c := range template.Children {
		c.Apply(node, context)
		switch v := c.(type) {
		case *Variable:
			//populate any params (including those passed via with-params)
			if IsXsltName(v.Node, "param") {
				v = selectParamValue(v, params)
			}
			_ = context.DeclareLocalVariable(v.Name, "", v)
		}
	}
	// break out of loop if terminated by xsl:message
	//restore any existing stack
	context.PopStack()
	context.Stack = oldStack
}
<commit_msg>Better for loop syntax<commit_after>package xslt

import (
	"container/list"
	"fmt"
	"github.com/moovweb/gokogiri/xml"
	"github.com/moovweb/gokogiri/xpath"
	"strings"
	"unicode/utf8"
)

type CompiledStep interface {
	Compile(node xml.Node)
	Apply(node xml.Node, context *ExecutionContext)
}

type Template struct {
	Name     string
	Mode     string
	Match    string
	Priority float64
	Children []CompiledStep
	Node     xml.Node
}

// Literal result elements are any elements in a template
// that are not in the xsl namespace.
// They are copied to the output document.
type LiteralResultElement struct {
	Node     xml.Node
	Children []CompiledStep
}

// Stylesheet text nodes
type TextOutput struct {
	Content string
}

// Used to represent an xsl:variable or xsl:param
type Variable struct {
	Name     string
	Node     xml.Node
	Children []CompiledStep
	Value    interface{}
}

// Compile the variable.
//
// TODO: compile the XPath expression and determine if it is a constant
func (i *Variable) Compile(node xml.Node) {
	i.Name = i.Node.Attr("name")
	for cur := node.FirstChild(); cur != nil; cur = cur.NextSibling() {
		res := CompileSingleNode(cur)
		if res != nil {
			res.Compile(cur)
			i.Children = append(i.Children, res)
		}
	}
}

// Applying a variable node is calculating its value.
func (i *Variable) Apply(node xml.Node, context *ExecutionContext) {
	scope := i.Node.Attr("select")
	// if @select
	if scope != "" {
		e := xpath.Compile(scope)
		var err error
		context.RegisterXPathNamespaces(i.Node)
		i.Value, err = context.EvalXPath(node, e)
		if err != nil {
			fmt.Println("Error evaluating variable", i.Name, err)
		}
		//fmt.Println("VARIABLE SELECT", i.Name, i.Value)
		return
	}

	if len(i.Children) == 0 {
		//fmt.Println("VARIABLE NIL", name, i.Value)
		i.Value = nil
		return
	}

	// if multiple children, return nodeset
	curOutput := context.OutputNode
	context.OutputNode = context.Output.CreateElementNode("RVT")
	context.PushStack()
	for _, c := range i.Children {
		c.Apply(node, context)
		switch v := c.(type) {
		case *Variable:
			_ = context.DeclareLocalVariable(v.Name, "", v)
		}
	}
	context.PopStack()
	i.Value = nil
	var outNodes xml.Nodeset
	for cur := context.OutputNode.FirstChild(); cur != nil; cur = cur.NextSibling() {
		outNodes = append(outNodes, cur)
	}
	i.Value = outNodes
	context.OutputNode = curOutput
	//fmt.Println("VARIABLE NODES", name, i.Value)
}

func (e *LiteralResultElement) Compile(node xml.Node) {
	for cur := node.FirstChild(); cur != nil; cur = cur.NextSibling() {
		res := CompileSingleNode(cur)
		if res != nil {
			res.Compile(cur)
			e.Children = append(e.Children, res)
		}
	}
}

func ResolveAlias(style *Stylesheet, alias, auri string) (prefix, uri string) {
	k, ok := style.NamespaceAlias[alias]
	//short circuit if prefix is not aliased
	if !ok {
		return alias, auri
	}
	for uri, prefix = range style.NamespaceMapping {
		if k == prefix {
			return
		}
	}
	return "", ""
}

func (e *LiteralResultElement) IsExtension(node xml.Node, context *ExecutionContext) bool {
	ns := e.Node.Namespace()
	prefix, _ := context.Style.NamespaceMapping[ns]
	if prefix != "" {
		for _, ex := range context.Style.ExtensionPrefixes {
			if ex == prefix {
				return true
			}
		}
	}
	return false
}

func (e *LiteralResultElement) Apply(node xml.Node, context *ExecutionContext) {
	//TODO: recognize extension elements at compile time
	if e.IsExtension(node, context) {
		for _, c := range e.Children {
			inst := c.(*XsltInstruction)
			if inst != nil && inst.Name == "fallback" {
				c.Apply(node, context)
			}
		}
		return
	}

	r := context.Output.CreateElementNode(e.Node.Name())
	context.OutputNode.AddChild(r)
	context.DeclareStylesheetNamespacesIfRoot(r)
	ns := e.Node.Namespace()
	if ns != "" {
		prefix, _ := context.Style.NamespaceMapping[ns]
		_, ns = ResolveAlias(context.Style, prefix, ns)
		//TODO: handle aliases
		r.SetNamespace(prefix, ns)
	}

	attsets := ""
	for _, attr := range e.Node.Attributes() {
		//fmt.Println(attr.Namespace(), attr.Name(), attr.Content())
		txt := attr.Content()
		if strings.ContainsRune(txt, '{') {
			txt = evalAVT(txt, node, context)
		}
		if attr.Namespace() != "" {
			if attr.Namespace() == XSLT_NAMESPACE {
				if attr.Name() == "use-attribute-sets" {
					attsets = txt
				}
			} else {
				r.SetNsAttr(attr.Namespace(), attr.Name(), txt)
			}
		} else {
			r.SetAttr(attr.Name(), txt)
		}
	}

	old := context.OutputNode
	context.OutputNode = r

	if attsets != "" {
		asets := strings.Fields(attsets)
		for _, attsetname := range asets {
			a := context.Style.LookupAttributeSet(attsetname)
			if a != nil {
				a.Apply(node, context)
			}
		}
	}
	for _, c := range e.Children {
		c.Apply(node, context)
		switch v := c.(type) {
		case *Variable:
			_ = context.DeclareLocalVariable(v.Name, "", v)
		}
	}
	context.OutputNode = old
}

// Evaluate an attribute value template
func evalAVT(input string, node xml.Node, context *ExecutionContext) (out string) {
	var start, pos int
	var inSQlit, inDQlit bool
	for pos < len(input) {
		r, width := utf8.DecodeRuneInString(input[pos:])
		pos += width
		if r == '\'' {
			inSQlit = !inSQlit
		}
		if r == '"' {
			inDQlit = !inDQlit
		}
		if r == '{' {
			// if we're not the last character
			if pos < len(input) {
				// check for doubled opening brace
				peek, w := utf8.DecodeRuneInString(input[pos:])
				if peek == '{' {
					out = out + input[start:pos]
					pos += w
					start = pos
					continue
				}
			}
			out = out + input[start:pos-width]
			start = pos
		}
		if r == '}' {
			if inSQlit || inDQlit {
				continue
			}
			// if we're not the last character
			if pos < len(input) {
				// check for doubled closing brace
				peek, w := utf8.DecodeRuneInString(input[pos:])
				if peek == '}' {
					out = out + input[start:pos]
					pos += w
					start = pos
					continue
				}
			}
			expr := input[start : pos-width]
			ret, _ := context.EvalXPath(node, expr)
			switch val := ret.(type) {
			case []xml.Node:
				for _, n := range val {
					out = out + n.Content()
				}
			case float64:
				out = out + fmt.Sprintf("%v", val)
			case string:
				out = out + val
			}
			start = pos
		}
	}
	out = out + input[start:pos]
	return
}

func (t *TextOutput) Compile(node xml.Node) {
}

func (t *TextOutput) Apply(node xml.Node, context *ExecutionContext) {
	if context.UseCDataSection(context.OutputNode) {
		r := context.Output.CreateCDataNode(t.Content)
		context.OutputNode.AddChild(r)
	} else {
		r := context.Output.CreateTextNode(t.Content)
		context.OutputNode.AddChild(r)
	}
}

func (template *Template) AddChild(child CompiledStep) {
	template.Children = append(template.Children, child)
}

func (template *Template) CompileContent(node xml.Node) {
	//parse the content and register the match pattern
	for cur := node.FirstChild(); cur != nil; cur = cur.NextSibling() {
		res := CompileSingleNode(cur)
		if res != nil {
			res.Compile(cur)
			template.AddChild(res)
		}
	}
}

func CompileSingleNode(node xml.Node) (step CompiledStep) {
	switch node.NodeType() {
	case xml.XML_ELEMENT_NODE:
		ns := node.Namespace()
		// element, extension namespace = extension
		if ns == XSLT_NAMESPACE {
			// element, XSLT namespace = instruction
			switch node.Name() {
			case "variable":
				step = &Variable{Node: node}
			case "param", "with-param":
				step = &Variable{Node: node}
			default:
				step = &XsltInstruction{Name: node.Name(), Node: node}
			}
		} else {
			// element other namespace = LRE
			step = &LiteralResultElement{Node: node}
		}
	// text, CDATA node
	case xml.XML_TEXT_NODE, xml.XML_CDATA_SECTION_NODE:
		if !IsBlank(node) {
			step = &TextOutput{Content: node.Content()}
		}
	}
	return
}

func selectParamValue(param *Variable, withParams []*Variable) (out *Variable) {
	for _, p := range withParams {
		if param.Name == p.Name {
			return p
		}
	}
	return param
}

func (template *Template) Apply(node xml.Node, context *ExecutionContext, params []*Variable) {
	//init local scope
	oldStack := context.Stack
	context.Stack = *new(list.List)
	context.PushStack()

	//for each node in compiled template body
	// if xsl:message
	// if forwards-compatible
	//   apply fallback
	for _, c := range template.Children {
		c.Apply(node, context)
		switch v := c.(type) {
		case *Variable:
			//populate any params (including those passed via with-params)
			if IsXsltName(v.Node, "param") {
				v = selectParamValue(v, params)
			}
			_ = context.DeclareLocalVariable(v.Name, "", v)
		}
	}
	// break out of loop if terminated by xsl:message
	//restore any existing stack
	context.PopStack()
	context.Stack = oldStack
}
<|endoftext|>


<commit_before>// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zapcore

import (
	"bytes"
	"fmt"
	"math"
	"reflect"
	"time"
)

// A FieldType indicates which member of the Field union struct should be used
// and how it should be serialized.
type FieldType uint8

const (
	// UnknownType is the default field type. Attempting to add it to an encoder will panic.
	UnknownType FieldType = iota
	// ArrayMarshalerType indicates that the field carries an ArrayMarshaler.
	ArrayMarshalerType
	// ObjectMarshalerType indicates that the field carries an ObjectMarshaler.
	ObjectMarshalerType
	// InlineMarshalerType indicates that the field carries an ObjectMarshaler
	// that should be inlined.
	InlineMarshalerType
	// BinaryType indicates that the field carries an opaque binary blob.
	BinaryType
	// BoolType indicates that the field carries a bool.
	BoolType
	// ByteStringType indicates that the field carries UTF-8 encoded bytes.
	ByteStringType
	// Complex128Type indicates that the field carries a complex128.
	Complex128Type
	// Complex64Type indicates that the field carries a complex128.
	Complex64Type
	// DurationType indicates that the field carries a time.Duration.
	DurationType
	// Float64Type indicates that the field carries a float64.
	Float64Type
	// Float32Type indicates that the field carries a float32.
	Float32Type
	// Int64Type indicates that the field carries an int64.
	Int64Type
	// Int32Type indicates that the field carries an int32.
	Int32Type
	// Int16Type indicates that the field carries an int16.
	Int16Type
	// Int8Type indicates that the field carries an int8.
	Int8Type
	// StringType indicates that the field carries a string.
	StringType
	// TimeType indicates that the field carries a time.Time that is
	// representable by a UnixNano() stored as an int64.
	TimeType
	// TimeFullType indicates that the field carries a time.Time stored as-is.
	TimeFullType
	// Uint64Type indicates that the field carries a uint64.
	Uint64Type
	// Uint32Type indicates that the field carries a uint32.
	Uint32Type
	// Uint16Type indicates that the field carries a uint16.
	Uint16Type
	// Uint8Type indicates that the field carries a uint8.
	Uint8Type
	// UintptrType indicates that the field carries a uintptr.
	UintptrType
	// ReflectType indicates that the field carries an interface{}, which should
	// be serialized using reflection.
	ReflectType
	// NamespaceType signals the beginning of an isolated namespace. All
	// subsequent fields should be added to the new namespace.
	NamespaceType
	// StringerType indicates that the field carries a fmt.Stringer.
	StringerType
	// ErrorType indicates that the field carries an error.
	ErrorType
	// SkipType indicates that the field is a no-op.
	SkipType
)

// A Field is a marshaling operation used to add a key-value pair to a logger's
// context. Most fields are lazily marshaled, so it's inexpensive to add fields
// to disabled debug-level log statements.
type Field struct {
	Key       string
	Type      FieldType
	Integer   int64
	String    string
	Interface interface{}
}

// AddTo exports a field through the ObjectEncoder interface. It's primarily
// useful to library authors, and shouldn't be necessary in most applications.
func (f Field) AddTo(enc ObjectEncoder) {
	var err error

	switch f.Type {
	case ArrayMarshalerType:
		err = enc.AddArray(f.Key, f.Interface.(ArrayMarshaler))
	case ObjectMarshalerType:
		err = enc.AddObject(f.Key, f.Interface.(ObjectMarshaler))
	case InlineMarshalerType:
		err = f.Interface.(ObjectMarshaler).MarshalLogObject(enc)
	case BinaryType:
		enc.AddBinary(f.Key, f.Interface.([]byte))
	case BoolType:
		enc.AddBool(f.Key, f.Integer == 1)
	case ByteStringType:
		enc.AddByteString(f.Key, f.Interface.([]byte))
	case Complex128Type:
		enc.AddComplex128(f.Key, f.Interface.(complex128))
	case Complex64Type:
		enc.AddComplex64(f.Key, f.Interface.(complex64))
	case DurationType:
		enc.AddDuration(f.Key, time.Duration(f.Integer))
	case Float64Type:
		enc.AddFloat64(f.Key, math.Float64frombits(uint64(f.Integer)))
	case Float32Type:
		enc.AddFloat32(f.Key, math.Float32frombits(uint32(f.Integer)))
	case Int64Type:
		enc.AddInt64(f.Key, f.Integer)
	case Int32Type:
		enc.AddInt32(f.Key, int32(f.Integer))
	case Int16Type:
		enc.AddInt16(f.Key, int16(f.Integer))
	case Int8Type:
		enc.AddInt8(f.Key, int8(f.Integer))
	case StringType:
		enc.AddString(f.Key, f.String)
	case TimeType:
		if f.Interface != nil {
			enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)))
		} else {
			// Fall back to UTC if location is nil.
			enc.AddTime(f.Key, time.Unix(0, f.Integer))
		}
	case TimeFullType:
		enc.AddTime(f.Key, f.Interface.(time.Time))
	case Uint64Type:
		enc.AddUint64(f.Key, uint64(f.Integer))
	case Uint32Type:
		enc.AddUint32(f.Key, uint32(f.Integer))
	case Uint16Type:
		enc.AddUint16(f.Key, uint16(f.Integer))
	case Uint8Type:
		enc.AddUint8(f.Key, uint8(f.Integer))
	case UintptrType:
		enc.AddUintptr(f.Key, uintptr(f.Integer))
	case ReflectType:
		err = enc.AddReflected(f.Key, f.Interface)
	case NamespaceType:
		enc.OpenNamespace(f.Key)
	case StringerType:
		err = encodeStringer(f.Key, f.Interface, enc)
	case ErrorType:
		err = encodeError(f.Key, f.Interface.(error), enc)
	case SkipType:
		break
	default:
		panic(fmt.Sprintf("unknown field type: %v", f))
	}

	if err != nil {
		enc.AddString(fmt.Sprintf("%sError", f.Key), err.Error())
	}
}

// Equals returns whether two fields are equal. For non-primitive types such as
// errors, marshalers, or reflect types, it uses reflect.DeepEqual.
func (f Field) Equals(other Field) bool {
	if f.Type != other.Type {
		return false
	}
	if f.Key != other.Key {
		return false
	}

	switch f.Type {
	case BinaryType, ByteStringType:
		return bytes.Equal(f.Interface.([]byte), other.Interface.([]byte))
	case ArrayMarshalerType, ObjectMarshalerType, ErrorType, ReflectType:
		return reflect.DeepEqual(f.Interface, other.Interface)
	default:
		return f == other
	}
}

func addFields(enc ObjectEncoder, fields []Field) {
	for i := range fields {
		fields[i].AddTo(enc)
	}
}

func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (retErr error) {
	// Try to capture panics (from nil references or otherwise) when calling
	// the String() method, similar to https://golang.org/src/fmt/print.go#L540
	defer func() {
		if err := recover(); err != nil {
			// If it's a nil pointer, just say "<nil>". The likeliest causes are a
			// Stringer that fails to guard against nil or a nil pointer for a
			// value receiver, and in either case, "<nil>" is a nice result.
			if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() {
				enc.AddString(key, "<nil>")
				return
			}

			retErr = fmt.Errorf("PANIC=%v", err)
		}
	}()

	enc.AddString(key, stringer.(fmt.Stringer).String())
	return nil
}
<commit_msg>zapcore/FieldType: Don't change enum values (#955)<commit_after>// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zapcore

import (
	"bytes"
	"fmt"
	"math"
	"reflect"
	"time"
)

// A FieldType indicates which member of the Field union struct should be used
// and how it should be serialized.
type FieldType uint8

const (
	// UnknownType is the default field type. Attempting to add it to an encoder will panic.
	UnknownType FieldType = iota
	// ArrayMarshalerType indicates that the field carries an ArrayMarshaler.
	ArrayMarshalerType
	// ObjectMarshalerType indicates that the field carries an ObjectMarshaler.
	ObjectMarshalerType
	// BinaryType indicates that the field carries an opaque binary blob.
	BinaryType
	// BoolType indicates that the field carries a bool.
	BoolType
	// ByteStringType indicates that the field carries UTF-8 encoded bytes.
	ByteStringType
	// Complex128Type indicates that the field carries a complex128.
	Complex128Type
	// Complex64Type indicates that the field carries a complex128.
	Complex64Type
	// DurationType indicates that the field carries a time.Duration.
	DurationType
	// Float64Type indicates that the field carries a float64.
	Float64Type
	// Float32Type indicates that the field carries a float32.
	Float32Type
	// Int64Type indicates that the field carries an int64.
	Int64Type
	// Int32Type indicates that the field carries an int32.
	Int32Type
	// Int16Type indicates that the field carries an int16.
	Int16Type
	// Int8Type indicates that the field carries an int8.
	Int8Type
	// StringType indicates that the field carries a string.
	StringType
	// TimeType indicates that the field carries a time.Time that is
	// representable by a UnixNano() stored as an int64.
	TimeType
	// TimeFullType indicates that the field carries a time.Time stored as-is.
	TimeFullType
	// Uint64Type indicates that the field carries a uint64.
	Uint64Type
	// Uint32Type indicates that the field carries a uint32.
	Uint32Type
	// Uint16Type indicates that the field carries a uint16.
	Uint16Type
	// Uint8Type indicates that the field carries a uint8.
	Uint8Type
	// UintptrType indicates that the field carries a uintptr.
	UintptrType
	// ReflectType indicates that the field carries an interface{}, which should
	// be serialized using reflection.
	ReflectType
	// NamespaceType signals the beginning of an isolated namespace. All
	// subsequent fields should be added to the new namespace.
	NamespaceType
	// StringerType indicates that the field carries a fmt.Stringer.
	StringerType
	// ErrorType indicates that the field carries an error.
	ErrorType
	// SkipType indicates that the field is a no-op.
	SkipType

	// InlineMarshalerType indicates that the field carries an ObjectMarshaler
	// that should be inlined.
	InlineMarshalerType
)

// A Field is a marshaling operation used to add a key-value pair to a logger's
// context. Most fields are lazily marshaled, so it's inexpensive to add fields
// to disabled debug-level log statements.
type Field struct {
	Key       string
	Type      FieldType
	Integer   int64
	String    string
	Interface interface{}
}

// AddTo exports a field through the ObjectEncoder interface. It's primarily
// useful to library authors, and shouldn't be necessary in most applications.
func (f Field) AddTo(enc ObjectEncoder) {
	var err error

	switch f.Type {
	case ArrayMarshalerType:
		err = enc.AddArray(f.Key, f.Interface.(ArrayMarshaler))
	case ObjectMarshalerType:
		err = enc.AddObject(f.Key, f.Interface.(ObjectMarshaler))
	case InlineMarshalerType:
		err = f.Interface.(ObjectMarshaler).MarshalLogObject(enc)
	case BinaryType:
		enc.AddBinary(f.Key, f.Interface.([]byte))
	case BoolType:
		enc.AddBool(f.Key, f.Integer == 1)
	case ByteStringType:
		enc.AddByteString(f.Key, f.Interface.([]byte))
	case Complex128Type:
		enc.AddComplex128(f.Key, f.Interface.(complex128))
	case Complex64Type:
		enc.AddComplex64(f.Key, f.Interface.(complex64))
	case DurationType:
		enc.AddDuration(f.Key, time.Duration(f.Integer))
	case Float64Type:
		enc.AddFloat64(f.Key, math.Float64frombits(uint64(f.Integer)))
	case Float32Type:
		enc.AddFloat32(f.Key, math.Float32frombits(uint32(f.Integer)))
	case Int64Type:
		enc.AddInt64(f.Key, f.Integer)
	case Int32Type:
		enc.AddInt32(f.Key, int32(f.Integer))
	case Int16Type:
		enc.AddInt16(f.Key, int16(f.Integer))
	case Int8Type:
		enc.AddInt8(f.Key, int8(f.Integer))
	case StringType:
		enc.AddString(f.Key, f.String)
	case TimeType:
		if f.Interface != nil {
			enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)))
		} else {
			// Fall back to UTC if location is nil.
			enc.AddTime(f.Key, time.Unix(0, f.Integer))
		}
	case TimeFullType:
		enc.AddTime(f.Key, f.Interface.(time.Time))
	case Uint64Type:
		enc.AddUint64(f.Key, uint64(f.Integer))
	case Uint32Type:
		enc.AddUint32(f.Key, uint32(f.Integer))
	case Uint16Type:
		enc.AddUint16(f.Key, uint16(f.Integer))
	case Uint8Type:
		enc.AddUint8(f.Key, uint8(f.Integer))
	case UintptrType:
		enc.AddUintptr(f.Key, uintptr(f.Integer))
	case ReflectType:
		err = enc.AddReflected(f.Key, f.Interface)
	case NamespaceType:
		enc.OpenNamespace(f.Key)
	case StringerType:
		err = encodeStringer(f.Key, f.Interface, enc)
	case ErrorType:
		err = encodeError(f.Key, f.Interface.(error), enc)
	case SkipType:
		break
	default:
		panic(fmt.Sprintf("unknown field type: %v", f))
	}

	if err != nil {
		enc.AddString(fmt.Sprintf("%sError", f.Key), err.Error())
	}
}

// Equals returns whether two fields are equal. For non-primitive types such as
// errors, marshalers, or reflect types, it uses reflect.DeepEqual.
func (f Field) Equals(other Field) bool {
	if f.Type != other.Type {
		return false
	}
	if f.Key != other.Key {
		return false
	}

	switch f.Type {
	case BinaryType, ByteStringType:
		return bytes.Equal(f.Interface.([]byte), other.Interface.([]byte))
	case ArrayMarshalerType, ObjectMarshalerType, ErrorType, ReflectType:
		return reflect.DeepEqual(f.Interface, other.Interface)
	default:
		return f == other
	}
}

func addFields(enc ObjectEncoder, fields []Field) {
	for i := range fields {
		fields[i].AddTo(enc)
	}
}

func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (retErr error) {
	// Try to capture panics (from nil references or otherwise) when calling
	// the String() method, similar to https://golang.org/src/fmt/print.go#L540
	defer func() {
		if err := recover(); err != nil {
			// If it's a nil pointer, just say "<nil>". The likeliest causes are a
			// Stringer that fails to guard against nil or a nil pointer for a
			// value receiver, and in either case, "<nil>" is a nice result.
			if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() {
				enc.AddString(key, "<nil>")
				return
			}

			retErr = fmt.Errorf("PANIC=%v", err)
		}
	}()

	enc.AddString(key, stringer.(fmt.Stringer).String())
	return nil
}
<|endoftext|>


<commit_before>package nv

import (
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"reflect"
)

const (
	extraName = "__extra"
)

func decodePreamble(r io.Reader, byteOrder binary.ByteOrder) (codec, endianness, error) {
	var err error
	enc := encoding{}
	if err = binary.Read(r, byteOrder, &enc); err != nil {
		return enc.Encoding, enc.Endianess, err
	}

	if enc.Encoding > maxCodec {
		err = fmt.Errorf("invalid encoding: %v", enc.Encoding)
	} else if enc.Endianess > maxEndian {
		err = fmt.Errorf("invalid endianess: %v", enc.Endianess)
	} else if enc.Reserved1 != 0 {
		err = fmt.Errorf("unexpected reserved1 value: %v", enc.Reserved1)
	} else if enc.Reserved2 != 0 {
		err = fmt.Errorf("unexpected reserved2 value: %v", enc.Reserved2)
	}
	return enc.Encoding, enc.Endianess, err
}

// fieldSetFunc is used to set the value if the target is a field within a
// struct
type fieldSetFunc func(reflect.Value, reflect.Value)

func decodeList(dec decoder, target reflect.Value) error {
	// Validate data header
	h, err := dec.header()
	if err != nil {
		return err
	}
	if h.Version != 0 {
		return fmt.Errorf("unexpected version: %v", h.Version)
	}
	if h.Flag < uniqueName || h.Flag > uniqueNameType {
		return fmt.Errorf("unexpected Flag: %v", h.Flag)
	}

	// If the target is a pointer, initialize and dereference.
	if target.Kind() == reflect.Ptr {
		target.Set(reflect.New(target.Type().Elem()))
		target = reflect.Indirect(target)
	}

	// maps and structs have slightly different handling
	// interface types will be handled as maps
	isMap := (target.Kind() == reflect.Map || target.Kind() == reflect.Interface)

	if isMap {
		initializeMap(&target)
	}

	// Special map alias that collects values and nvlist types
	isList := target.Type().String() == "nv.list"

	// For structs, create the lookup table for field name/tag -> field index
	var targetFieldIndexMap map[string]int
	if !isMap {
		targetFieldIndexMap, err = fieldIndexMap(target)
		if err != nil {
			return err
		}
	}

	// Set up the extra field map for fields not defined in the struct
	var extraMap reflect.Value
	if !isMap {
		if index, ok := targetFieldIndexMap[extraName]; ok {
			extraMap = target.Field(index)
			initializeMap(&extraMap)
		}
	}

	// Start decoding data
	for {
		// Reset the iteration target and map status
		iterTarget := target
		iterIsMap := isMap

		// Done when there's no more data or an error has occurred
		if end, err := dec.isEnd(); end || err != nil {
			return err
		}

		// Get next encoded data pair
		// Note: This just gets the data pair metadata. The actual value
		// data is left on the reader, to be pulled off by the decoder.
		pName, pType, err := dec.meta()
		if err != nil {
			return err
		}

		// If not dealing with a map, look up the corresponding struct field
		var targetField reflect.Value
		if !isMap {
			targetFieldIndex, ok := targetFieldIndexMap[pName]
			// If there's no corresponding struct field and no field for
			// extras, skip the data and move on to the next data pair. If
			// there is an field for extras, make that the new target for this
			// iteration.
			if !ok {
				if !extraMap.IsValid() {
					if err := dec.skip(); err != nil {
						return err
					}
					continue
				}
				iterTarget = extraMap
				iterIsMap = true
			} else {
				targetField = target.Field(targetFieldIndex)
			}
		}

		var targetType reflect.Type
		if isList {
			targetType = iterTarget.Type()
		} else if iterIsMap {
			targetType = iterTarget.Type().Elem()
		} else {
			targetType = targetField.Type()
		}

		// value can be used directly if target is a map, if it is a
		// struct we need to use fieldSetter since the struct needs to
		// be set with a non-interface type
		value, fieldSetter, err := dec.value(targetType)
		if err != nil {
			return err
		}

		// Set the value appropriately
		if iterIsMap {
			name := reflect.ValueOf(pName)
			if isList {
				value = reflect.ValueOf(map[string]interface{}{
					"type":  pType.String(),
					"value": value.Interface(),
				})
			}
			iterTarget.SetMapIndex(name, value)
		} else {
			fieldSetter(targetField, value)
		}
	}
	return nil
}

// fieldIndexMap creates a map of field names, with tag name overrides,
// to their index
func fieldIndexMap(v reflect.Value) (map[string]int, error) {
	vFieldIndexMap := make(map[string]int)
	var err error
	forEachField(v, func(i int, field reflect.Value) bool {
		// Skip fields that can't be set (e.g. unexported)
		if !field.CanSet() {
			return true
		}

		name := v.Type().Field(i).Name
		tags := getTags(i, v)
		isMap := (field.Kind() == reflect.Map || field.Kind() == reflect.Interface)

		// skip "name" if "extra" exists because "name" is irrelevant
		if len(tags) > 1 && tags[1] == "extra" && isMap {
			name = extraName
			if _, ok := vFieldIndexMap[name]; ok {
				err = errors.New("more than one field tagged 'extra'")
				return false
			}
		} else if len(tags) > 0 && tags[0] != "" {
			name = tags[0]
		}
		if _, ok := vFieldIndexMap[name]; ok {
			err = fmt.Errorf("more than one field with tag/name: %s", name)
			return false
		}
		vFieldIndexMap[name] = i
		return true
	})
	return vFieldIndexMap, err
}

func initializeMap(target *reflect.Value) {
	// Initialize the map. Can't add keys without this.
	if target.Kind() == reflect.Interface {
		target.Set(reflect.MakeMap(reflect.TypeOf(map[string]interface{}{})))
		*target = target.Elem()
	} else {
		target.Set(reflect.MakeMap(target.Type()))
	}
}
<commit_msg>Remove unreachable code<commit_after>package nv

import (
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"reflect"
)

const (
	extraName = "__extra"
)

func decodePreamble(r io.Reader, byteOrder binary.ByteOrder) (codec, endianness, error) {
	var err error
	enc := encoding{}
	if err = binary.Read(r, byteOrder, &enc); err != nil {
		return enc.Encoding, enc.Endianess, err
	}

	if enc.Encoding > maxCodec {
		err = fmt.Errorf("invalid encoding: %v", enc.Encoding)
	} else if enc.Endianess > maxEndian {
		err = fmt.Errorf("invalid endianess: %v", enc.Endianess)
	} else if enc.Reserved1 != 0 {
		err = fmt.Errorf("unexpected reserved1 value: %v", enc.Reserved1)
	} else if enc.Reserved2 != 0 {
		err = fmt.Errorf("unexpected reserved2 value: %v", enc.Reserved2)
	}
	return enc.Encoding, enc.Endianess, err
}

// fieldSetFunc is used to set the value if the target is a field within a
// struct
type fieldSetFunc func(reflect.Value, reflect.Value)

func decodeList(dec decoder, target reflect.Value) error {
	// Validate data header
	h, err := dec.header()
	if err != nil {
		return err
	}
	if h.Version != 0 {
		return fmt.Errorf("unexpected version: %v", h.Version)
	}
	if h.Flag < uniqueName || h.Flag > uniqueNameType {
		return fmt.Errorf("unexpected Flag: %v", h.Flag)
	}

	// If the target is a pointer, initialize and dereference.
	if target.Kind() == reflect.Ptr {
		target.Set(reflect.New(target.Type().Elem()))
		target = reflect.Indirect(target)
	}

	// maps and structs have slightly different handling
	// interface types will be handled as maps
	isMap := (target.Kind() == reflect.Map || target.Kind() == reflect.Interface)

	if isMap {
		initializeMap(&target)
	}

	// Special map alias that collects values and nvlist types
	isList := target.Type().String() == "nv.list"

	// For structs, create the lookup table for field name/tag -> field index
	var targetFieldIndexMap map[string]int
	if !isMap {
		targetFieldIndexMap, err = fieldIndexMap(target)
		if err != nil {
			return err
		}
	}

	// Set up the extra field map for fields not defined in the struct
	var extraMap reflect.Value
	if !isMap {
		if index, ok := targetFieldIndexMap[extraName]; ok {
			extraMap = target.Field(index)
			initializeMap(&extraMap)
		}
	}

	// Start decoding data
	for {
		// Reset the iteration target and map status
		iterTarget := target
		iterIsMap := isMap

		// Done when there's no more data or an error has occurred
		if end, err := dec.isEnd(); end || err != nil {
			return err
		}

		// Get next encoded data pair
		// Note: This just gets the data pair metadata. The actual value
		// data is left on the reader, to be pulled off by the decoder.
		pName, pType, err := dec.meta()
		if err != nil {
			return err
		}

		// If not dealing with a map, look up the corresponding struct field
		var targetField reflect.Value
		if !isMap {
			targetFieldIndex, ok := targetFieldIndexMap[pName]
			// If there's no corresponding struct field and no field for
			// extras, skip the data and move on to the next data pair. If
			// there is an field for extras, make that the new target for this
			// iteration.
			if !ok {
				if !extraMap.IsValid() {
					if err := dec.skip(); err != nil {
						return err
					}
					continue
				}
				iterTarget = extraMap
				iterIsMap = true
			} else {
				targetField = target.Field(targetFieldIndex)
			}
		}

		var targetType reflect.Type
		if isList {
			targetType = iterTarget.Type()
		} else if iterIsMap {
			targetType = iterTarget.Type().Elem()
		} else {
			targetType = targetField.Type()
		}

		// value can be used directly if target is a map, if it is a
		// struct we need to use fieldSetter since the struct needs to
		// be set with a non-interface type
		value, fieldSetter, err := dec.value(targetType)
		if err != nil {
			return err
		}

		// Set the value appropriately
		if iterIsMap {
			name := reflect.ValueOf(pName)
			if isList {
				value = reflect.ValueOf(map[string]interface{}{
					"type":  pType.String(),
					"value": value.Interface(),
				})
			}
			iterTarget.SetMapIndex(name, value)
		} else {
			fieldSetter(targetField, value)
		}
	}
}

// fieldIndexMap creates a map of field names, with tag name overrides,
// to their index
func fieldIndexMap(v reflect.Value) (map[string]int, error) {
	vFieldIndexMap := make(map[string]int)
	var err error
	forEachField(v, func(i int, field reflect.Value) bool {
		// Skip fields that can't be set (e.g. unexported)
		if !field.CanSet() {
			return true
		}

		name := v.Type().Field(i).Name
		tags := getTags(i, v)
		isMap := (field.Kind() == reflect.Map || field.Kind() == reflect.Interface)

		// skip "name" if "extra" exists because "name" is irrelevant
		if len(tags) > 1 && tags[1] == "extra" && isMap {
			name = extraName
			if _, ok := vFieldIndexMap[name]; ok {
				err = errors.New("more than one field tagged 'extra'")
				return false
			}
		} else if len(tags) > 0 && tags[0] != "" {
			name = tags[0]
		}
		if _, ok := vFieldIndexMap[name]; ok {
			err = fmt.Errorf("more than one field with tag/name: %s", name)
			return false
		}
		vFieldIndexMap[name] = i
		return true
	})
	return vFieldIndexMap, err
}

func initializeMap(target *reflect.Value) {
	// Initialize the map. Can't add keys without this.
	if target.Kind() == reflect.Interface {
		target.Set(reflect.MakeMap(reflect.TypeOf(map[string]interface{}{})))
		*target = target.Elem()
	} else {
		target.Set(reflect.MakeMap(target.Type()))
	}
}
<|endoftext|>


<commit_before>package main

import (
	"crypto/sha1"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path"
	"time"
)

const (
	resultsDir = "results"

	modelFilename    = "model.zpl"
	solutionFilename = "scip.sol"
	outputFilename   = "output.log"
)

func runSolver(dir string) {
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		return
	}

	// TODO: add limit on number of parallel solver runs

	commands := fmt.Sprintf("read %s  opt  write solution %s  quit",
		modelFilename, solutionFilename)
	cmd := exec.Command("scip", "-c", commands, "-l", outputFilename)
	cmd.Dir = dir
	_ = cmd.Run()
}

func solve(dir string) (err error) {
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		return err
	}

	// for now, just start new process for every call
	go runSolver(dir)

	return nil
}

func inputHandler(w http.ResponseWriter, r *http.Request) {
	err := inputTemplate.Execute(w, nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func solveHandler(w http.ResponseWriter, r *http.Request) {
	model := r.FormValue("model")
	hash := fmt.Sprintf("%x", sha1.Sum([]byte(model)))

	dir := path.Join(resultsDir, hash)
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		err = os.MkdirAll(dir, 0755)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		filename := path.Join(dir, modelFilename)
		err = ioutil.WriteFile(filename, []byte(model), 0644)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		err = solve(dir)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	// add short sleep so that result might already exist when we
	// finally redirect.
	time.Sleep(100 * time.Millisecond)

	http.Redirect(w, r, "/result/"+hash, http.StatusFound)
}

type Result struct {
	Hash     string
	Model    string
	Solution string
	Output   string
}

func resultHandler(w http.ResponseWriter, r *http.Request) {
	hash := r.URL.Path[len("/result/"):]

	dir := path.Join(resultsDir, hash)
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	res := &Result{Hash: hash, Model: "", Solution: "", Output: ""}

	model := path.Join(dir, modelFilename)
	if _, err := os.Stat(model); err == nil {
		content, err := ioutil.ReadFile(model)
		if err == nil {
			res.Model = string(content)
		}
	}

	sol := path.Join(dir, solutionFilename)
	if _, err := os.Stat(sol); err == nil {
		content, err := ioutil.ReadFile(sol)
		if err == nil {
			res.Solution = string(content)
		}
	}

	out := path.Join(dir, outputFilename)
	if _, err := os.Stat(out); err == nil {
		content, err := ioutil.ReadFile(out)
		if err == nil {
			res.Output = string(content)
		}
	}

	if err := resultTemplate.Execute(w, res); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func main() {
	http.HandleFunc("/", inputHandler)
	http.HandleFunc("/solve/", solveHandler)
	http.HandleFunc("/result/", resultHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
<commit_msg>add limits to SCIP<commit_after>package main

import (
	"crypto/sha1"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path"
	"time"
)

const (
	resultsDir = "results"

	modelFilename    = "model.zpl"
	solutionFilename = "scip.sol"
	outputFilename   = "output.log"

	timeLimitSec  = 3 * 60
	memoryLimitMB = 100
)

func runSolver(dir string) {
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		return
	}

	// TODO: add limit on number of parallel solver runs

	commands := fmt.Sprintf("set limits time %d "+
		"set limits memory %d "+
		"read %s  opt  write solution %s  quit",
		timeLimitSec, memoryLimitMB,
		modelFilename, solutionFilename)
	cmd := exec.Command("scip", "-c", commands, "-l", outputFilename)
	cmd.Dir = dir
	_ = cmd.Run()
}

func solve(dir string) (err error) {
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		return err
	}

	// for now, just start new process for every call
	go runSolver(dir)

	return nil
}

func inputHandler(w http.ResponseWriter, r *http.Request) {
	err := inputTemplate.Execute(w, nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func solveHandler(w http.ResponseWriter, r *http.Request) {
	model := r.FormValue("model")
	hash := fmt.Sprintf("%x", sha1.Sum([]byte(model)))

	dir := path.Join(resultsDir, hash)
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		err = os.MkdirAll(dir, 0755)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		filename := path.Join(dir, modelFilename)
		err = ioutil.WriteFile(filename, []byte(model), 0644)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		err = solve(dir)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	// add short sleep so that result might already exist when we
	// finally redirect.
	time.Sleep(100 * time.Millisecond)

	http.Redirect(w, r, "/result/"+hash, http.StatusFound)
}

type Result struct {
	Hash     string
	Model    string
	Solution string
	Output   string
}

func resultHandler(w http.ResponseWriter, r *http.Request) {
	hash := r.URL.Path[len("/result/"):]

	dir := path.Join(resultsDir, hash)
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	res := &Result{Hash: hash, Model: "", Solution: "", Output: ""}

	model := path.Join(dir, modelFilename)
	if _, err := os.Stat(model); err == nil {
		content, err := ioutil.ReadFile(model)
		if err == nil {
			res.Model = string(content)
		}
	}

	sol := path.Join(dir, solutionFilename)
	if _, err := os.Stat(sol); err == nil {
		content, err := ioutil.ReadFile(sol)
		if err == nil {
			res.Solution = string(content)
		}
	}

	out := path.Join(dir, outputFilename)
	if _, err := os.Stat(out); err == nil {
		content, err := ioutil.ReadFile(out)
		if err == nil {
			res.Output = string(content)
		}
	}

	if err := resultTemplate.Execute(w, res); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func main() {
	http.HandleFunc("/", inputHandler)
	http.HandleFunc("/solve/", solveHandler)
	http.HandleFunc("/result/", resultHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
<|endoftext|>


<commit_before>package zog

import "fmt"

type Table struct {
	inCh   chan byte
	wantIX bool
	wantIY bool
}

// These tables participate in IX/IY replacement
var baseTableR []Loc8 = []Loc8{B, C, D, E, H, L, Contents{HL}, A}
var baseTableRP []Loc16 = []Loc16{BC, DE, HL, SP}
var baseTableRP2 []Loc16 = []Loc16{BC, DE, HL, AF}

var tableCC []Conditional = []Conditional{Not{FT_Z}, FT_Z, Not{FT_C}, FT_C, FT_PO, FT_PE, FT_P, FT_M}

func NewTable(inCh chan byte) *Table {
	return &Table{inCh: inCh}
}

func (t *Table) ResetPrefix(n byte) {
	t.wantIX = false
	t.wantIY = false
	switch n {
	case 0xDD:
		t.wantIX = true
	case 0xFD:
		t.wantIY = true
	}
}

func (t *Table) LookupR(i byte) Loc8 {
	l := baseTableR[i]
	if !t.wantIX && !t.wantIY {
		return l
	}

	switch i {
	case 4: // H
		l = IXH
		if t.wantIY {
			l = IYH
		}
	case 5: // L
		l = IXL
		if t.wantIY {
			l = IYL
		}
	case 6: // (HL)
		d, err := getImmd(t.inCh)
		// TODO: panic is messy here- opens us up to panic on decode
		if err != nil {
			panic(fmt.Errorf("Can't get index displacemnt: %s", err))
		}
		l = IndexedContents{IX, d}
		if t.wantIY {
			l = IndexedContents{IY, d}
		}
	}

	return l
}

func (t *Table) LookupRP(i byte) Loc16 {
	l := baseTableRP[i]
	if l == HL {
		if t.wantIX {
			l = IX
		}
		if t.wantIY {
			l = IY
		}
	}
	return l
}

func (t *Table) LookupRP2(i byte) Loc16 {
	l := baseTableRP2[i]
	if l == HL {
		if t.wantIX {
			l = IX
		}
		if t.wantIY {
			l = IY
		}
	}
	return l
}

type AccumInfo struct {
	name string
	//	f    AccumFunc
}

var tableALU []AccumInfo = []AccumInfo{
	/*
		{"ADD", AccumADD8},
		{"ADC", AccumADC8},
		{"SUB", AccumSUB8},
		{"SBC", AccumSBC8},
		{"AND", AccumAND8},
		{"XOR", AccumXOR8},
		{"OR", AccumOR8},
		{"CP", AccumCP8},
	*/
	{"ADD"},
	{"ADC"},
	{"SUB"},
	{"SBC"},
	{"AND"},
	{"XOR"},
	{"OR"},
	{"CP"},
}

type RotInfo struct {
	name string
	//	f    AccumFunc
}

var tableROT []RotInfo = []RotInfo{
	{"RLC"},
	{"RRC"},
	{"RL"},
	{"RR"},
	{"SLA"},
	{"SRA"},
	{"SLL"},
	{"SRL"},
}
<commit_msg>remove commented out func from table<commit_after>package zog

import "fmt"

type Table struct {
	inCh   chan byte
	wantIX bool
	wantIY bool
}

// These tables participate in IX/IY replacement
var baseTableR []Loc8 = []Loc8{B, C, D, E, H, L, Contents{HL}, A}
var baseTableRP []Loc16 = []Loc16{BC, DE, HL, SP}
var baseTableRP2 []Loc16 = []Loc16{BC, DE, HL, AF}

var tableCC []Conditional = []Conditional{Not{FT_Z}, FT_Z, Not{FT_C}, FT_C, FT_PO, FT_PE, FT_P, FT_M}

func NewTable(inCh chan byte) *Table {
	return &Table{inCh: inCh}
}

func (t *Table) ResetPrefix(n byte) {
	t.wantIX = false
	t.wantIY = false
	switch n {
	case 0xDD:
		t.wantIX = true
	case 0xFD:
		t.wantIY = true
	}
}

func (t *Table) LookupR(i byte) Loc8 {
	l := baseTableR[i]
	if !t.wantIX && !t.wantIY {
		return l
	}

	switch i {
	case 4: // H
		l = IXH
		if t.wantIY {
			l = IYH
		}
	case 5: // L
		l = IXL
		if t.wantIY {
			l = IYL
		}
	case 6: // (HL)
		d, err := getImmd(t.inCh)
		// TODO: panic is messy here- opens us up to panic on decode
		if err != nil {
			panic(fmt.Errorf("Can't get index displacemnt: %s", err))
		}
		l = IndexedContents{IX, d}
		if t.wantIY {
			l = IndexedContents{IY, d}
		}
	}

	return l
}

func (t *Table) LookupRP(i byte) Loc16 {
	l := baseTableRP[i]
	if l == HL {
		if t.wantIX {
			l = IX
		}
		if t.wantIY {
			l = IY
		}
	}
	return l
}

func (t *Table) LookupRP2(i byte) Loc16 {
	l := baseTableRP2[i]
	if l == HL {
		if t.wantIX {
			l = IX
		}
		if t.wantIY {
			l = IY
		}
	}
	return l
}

type AccumInfo struct {
	name string
	//	f    AccumFunc
}

var tableALU []AccumInfo = []AccumInfo{
	{"ADD"},
	{"ADC"},
	{"SUB"},
	{"SBC"},
	{"AND"},
	{"XOR"},
	{"OR"},
	{"CP"},
}

type RotInfo struct {
	name string
	//	f    AccumFunc
}

var tableROT []RotInfo = []RotInfo{
	{"RLC"},
	{"RRC"},
	{"RL"},
	{"RR"},
	{"SLA"},
	{"SRA"},
	{"SLL"},
	{"SRL"},
}
<|endoftext|>


<commit_before>package CloudForest

import (
	"math"
)

//Target abstracts the methods needed for a feature to be predictable.
type Target interface {
	NCats() (n int)
	SplitImpurity(l []int, r []int, counter *[]int) (impurityDecrease float64)
	Impurity(cases *[]int, counter *[]int) (impurity float64)
	FindPredicted(cases []int) (pred string)
}

type L1Target struct {
	*Feature
}

/*
SplitImpurity calculates the impurity of a splitinto the specified left and right
groups. This is depined as pLi*(tL)+pR*i(tR) where pL and pR are the probability of case going left or right
and i(tl) i(tR) are the left and right impurites.

Counter is only used for catagorical targets and should have the same length as the number of catagories in the target.
*/
func (target *L1Target) SplitImpurity(l []int, r []int, counter *[]int) (impurityDecrease float64) {
	nl := float64(len(l))
	nr := float64(len(r))

	impurityDecrease = nl * target.Impurity(&l, counter)
	impurityDecrease += nr * target.Impurity(&r, counter)

	impurityDecrease /= nl + nr
	return
}

//Impurity returns Gini impurity or mean squared error vs the mean for a set of cases
//depending on weather the feature is catagorical or numerical
func (target *L1Target) Impurity(cases *[]int, counter *[]int) (e float64) {
	m := target.Mean(cases)
	e = target.MeanL1Error(cases, m)
	return

}

//MeanL1Error returns the  Mean L1 norm error of the cases specifed vs the predicted
//value. Only non missing casses are considered.
func (target *L1Target) MeanL1Error(cases *[]int, predicted float64) (e float64) {
	e = 0.0
	n := 0
	for _, i := range *cases {
		if !target.Missing[i] {
			e += math.Abs(predicted - target.NumData[i])
			n += 1
		}

	}
	e = e / float64(n)
	return

}
<commit_msg>added target for weighted regret<commit_after>package CloudForest

import (
	"math"
)

//Target abstracts the methods needed for a feature to be predictable.
type Target interface {
	NCats() (n int)
	SplitImpurity(l []int, r []int, counter *[]int) (impurityDecrease float64)
	Impurity(cases *[]int, counter *[]int) (impurity float64)
	FindPredicted(cases []int) (pred string)
}

/*
RegretTarget wraps a catagorical feature for use in regret driven classification.
*/
type RegretTarget struct {
	*Feature
	costs []float64
}

/*
SplitImpurity calculates the impurity of a splitinto the specified left and right
groups. This is depined as pLi*(tL)+pR*i(tR) where pL and pR are the probability of case going left or right
and i(tl) i(tR) are the left and right impurites.

Counter is only used for catagorical targets and should have the same length as the number of catagories in the target.
*/
func (target *RegretTarget) SplitImpurity(l []int, r []int, counter *[]int) (impurityDecrease float64) {
	nl := float64(len(l))
	nr := float64(len(r))

	impurityDecrease = nl * target.Impurity(&l, counter)
	impurityDecrease += nr * target.Impurity(&r, counter)

	impurityDecrease /= nl + nr
	return
}

func (target *RegretTarget) Impurity(cases *[]int, counter *[]int) (e float64) {
	m := target.Modei(cases)
	t := 0
	for _, c := range cases {
		if target.Missing[c] == false {
			t += 1
			cat := target.CatData[c]
			if cat != m {
				e += target.costs[cat]
			}
		}

	}
	e /= float64(1)

	return

}

/*
L1Target wraps a numerical feature as a target for us in l1 norm regresion.
*/
type L1Target struct {
	*Feature
}

/*
SplitImpurity calculates the impurity of a splitinto the specified left and right
groups. This is depined as pLi*(tL)+pR*i(tR) where pL and pR are the probability of case going left or right
and i(tl) i(tR) are the left and right impurites.

Counter is only used for catagorical targets and should have the same length as the number of catagories in the target.
*/
func (target *L1Target) SplitImpurity(l []int, r []int, counter *[]int) (impurityDecrease float64) {
	nl := float64(len(l))
	nr := float64(len(r))

	impurityDecrease = nl * target.Impurity(&l, counter)
	impurityDecrease += nr * target.Impurity(&r, counter)

	impurityDecrease /= nl + nr
	return
}

//Impurity returns Gini impurity or mean squared error vs the mean for a set of cases
//depending on weather the feature is catagorical or numerical
func (target *L1Target) Impurity(cases *[]int, counter *[]int) (e float64) {
	m := target.Mean(cases)
	e = target.MeanL1Error(cases, m)
	return

}

//MeanL1Error returns the  Mean L1 norm error of the cases specifed vs the predicted
//value. Only non missing casses are considered.
func (target *L1Target) MeanL1Error(cases *[]int, predicted float64) (e float64) {
	e = 0.0
	n := 0
	for _, i := range *cases {
		if !target.Missing[i] {
			e += math.Abs(predicted - target.NumData[i])
			n += 1
		}

	}
	e = e / float64(n)
	return

}
<|endoftext|>


<commit_before>package poloniex

import "github.com/shopspring/decimal"

type Tickers struct {
	Pair map[string]Ticker
}

type Ticker struct {
	Id            int             `json:"id"`
	Last          decimal.Decimal `json:"last,string"`
	LowestAsk     decimal.Decimal `json:"lowestAsk,string"`
	HighestBid    decimal.Decimal `json:"highestBid,string"`
	PercentChange decimal.Decimal `json:"percentChange,string"`
	BaseVolume    decimal.Decimal `json:"baseVolume,string"`
	QuoteVolume   decimal.Decimal `json:"quoteVolume,string"`
	IsFrozen      int             `json:"isFrozen,string"`
	High24Hr      decimal.Decimal `json:"high24hr,string"`
	Low24Hr       decimal.Decimal `json:"low24hr,string"`
}
<commit_msg>Revert use of github.com/shopspring/decimal package<commit_after>package poloniex

type Tickers struct {
	Pair map[string]Ticker
}

type Ticker struct {
	Id            int     `json:"id"`
	Last          float64 `json:"last,string"`
	LowestAsk     float64 `json:"lowestAsk,string"`
	HighestBid    float64 `json:"highestBid,string"`
	PercentChange float64 `json:"percentChange,string"`
	BaseVolume    float64 `json:"baseVolume,string"`
	QuoteVolume   float64 `json:"quoteVolume,string"`
	IsFrozen      int     `json:"isFrozen,string"`
	High24Hr      float64 `json:"high24hr,string"`
	Low24Hr       float64 `json:"low24hr,string"`
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"regexp"

	"github.com/hashicorp/terraform/helper/schema"

	"github.com/aws/aws-sdk-go/aws"
	events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
)

func resourceAwsCloudWatchEventTarget() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsCloudWatchEventTargetCreate,
		Read:   resourceAwsCloudWatchEventTargetRead,
		Update: resourceAwsCloudWatchEventTargetUpdate,
		Delete: resourceAwsCloudWatchEventTargetDelete,

		Schema: map[string]*schema.Schema{
			"rule": &schema.Schema{
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validateCloudWatchEventRuleName,
			},

			"target_id": &schema.Schema{
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validateCloudWatchEventTargetId,
			},

			"arn": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
			},

			"input": &schema.Schema{
				Type:          schema.TypeString,
				Optional:      true,
				ConflictsWith: []string{"input_path"},
				// We could be normalizing the JSON here,
				// but for built-in targets input may not be JSON
			},

			"input_path": &schema.Schema{
				Type:          schema.TypeString,
				Optional:      true,
				ConflictsWith: []string{"input"},
			},
		},
	}
}

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

	rule := d.Get("rule").(string)
	targetId := d.Get("target_id").(string)

	input := buildPutTargetInputStruct(d)
	log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input)
	out, err := conn.PutTargets(input)
	if err != nil {
		return fmt.Errorf("Creating CloudWatch Event Target failed: %s", err)
	}

	if len(out.FailedEntries) > 0 {
		return fmt.Errorf("Creating CloudWatch Event Target failed: %s",
			out.FailedEntries)
	}

	id := rule + "-" + targetId
	d.SetId(id)

	log.Printf("[INFO] CloudWatch Event Target %q created", d.Id())

	return resourceAwsCloudWatchEventTargetRead(d, meta)
}

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

	t, err := findEventTargetById(
		d.Get("target_id").(string),
		d.Get("rule").(string),
		nil, conn)
	if err != nil {
		if regexp.MustCompile(" not found$").MatchString(err.Error()) {
			log.Printf("[WARN] Removing CloudWatch Event Target %q because it's gone.", d.Id())
			d.SetId("")
			return nil
		}
		return err
	}
	log.Printf("[DEBUG] Found Event Target: %s", t)

	d.Set("arn", t.Arn)
	d.Set("target_id", t.Id)
	d.Set("input", t.Input)
	d.Set("input_path", t.InputPath)

	return nil
}

func findEventTargetById(id, rule string, nextToken *string, conn *events.CloudWatchEvents) (
	*events.Target, error) {
	input := events.ListTargetsByRuleInput{
		Rule:      aws.String(rule),
		NextToken: nextToken,
		Limit:     aws.Int64(100), // Set limit to allowed maximum to prevent API throttling
	}
	log.Printf("[DEBUG] Reading CloudWatch Event Target: %s", input)
	out, err := conn.ListTargetsByRule(&input)
	if err != nil {
		return nil, err
	}

	for _, t := range out.Targets {
		if *t.Id == id {
			return t, nil
		}
	}

	if out.NextToken != nil {
		return findEventTargetById(id, rule, nextToken, conn)
	}

	return nil, fmt.Errorf("CloudWatch Event Target %q (%q) not found", id, rule)
}

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

	input := buildPutTargetInputStruct(d)
	log.Printf("[DEBUG] Updating CloudWatch Event Target: %s", input)
	_, err := conn.PutTargets(input)
	if err != nil {
		return fmt.Errorf("Updating CloudWatch Event Target failed: %s", err)
	}

	return resourceAwsCloudWatchEventTargetRead(d, meta)
}

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

	input := events.RemoveTargetsInput{
		Ids:  []*string{aws.String(d.Get("target_id").(string))},
		Rule: aws.String(d.Get("rule").(string)),
	}
	log.Printf("[INFO] Deleting CloudWatch Event Target: %s", input)
	_, err := conn.RemoveTargets(&input)
	if err != nil {
		return fmt.Errorf("Error deleting CloudWatch Event Target: %s", err)
	}
	log.Println("[INFO] CloudWatch Event Target deleted")

	d.SetId("")

	return nil
}

func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput {
	e := &events.Target{
		Arn: aws.String(d.Get("arn").(string)),
		Id:  aws.String(d.Get("target_id").(string)),
	}

	if v, ok := d.GetOk("input"); ok {
		e.Input = aws.String(v.(string))
	}
	if v, ok := d.GetOk("input_path"); ok {
		e.InputPath = aws.String(v.(string))
	}

	input := events.PutTargetsInput{
		Rule:    aws.String(d.Get("rule").(string)),
		Targets: []*events.Target{e},
	}

	return &input
}
<commit_msg>Allow recovering from failed CW Event Target creation in state<commit_after>package aws

import (
	"fmt"
	"log"
	"regexp"

	"github.com/hashicorp/terraform/helper/schema"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
)

func resourceAwsCloudWatchEventTarget() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsCloudWatchEventTargetCreate,
		Read:   resourceAwsCloudWatchEventTargetRead,
		Update: resourceAwsCloudWatchEventTargetUpdate,
		Delete: resourceAwsCloudWatchEventTargetDelete,

		Schema: map[string]*schema.Schema{
			"rule": &schema.Schema{
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validateCloudWatchEventRuleName,
			},

			"target_id": &schema.Schema{
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validateCloudWatchEventTargetId,
			},

			"arn": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
			},

			"input": &schema.Schema{
				Type:          schema.TypeString,
				Optional:      true,
				ConflictsWith: []string{"input_path"},
				// We could be normalizing the JSON here,
				// but for built-in targets input may not be JSON
			},

			"input_path": &schema.Schema{
				Type:          schema.TypeString,
				Optional:      true,
				ConflictsWith: []string{"input"},
			},
		},
	}
}

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

	rule := d.Get("rule").(string)
	targetId := d.Get("target_id").(string)

	input := buildPutTargetInputStruct(d)
	log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input)
	out, err := conn.PutTargets(input)
	if err != nil {
		return fmt.Errorf("Creating CloudWatch Event Target failed: %s", err)
	}

	if len(out.FailedEntries) > 0 {
		return fmt.Errorf("Creating CloudWatch Event Target failed: %s",
			out.FailedEntries)
	}

	id := rule + "-" + targetId
	d.SetId(id)

	log.Printf("[INFO] CloudWatch Event Target %q created", d.Id())

	return resourceAwsCloudWatchEventTargetRead(d, meta)
}

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

	t, err := findEventTargetById(
		d.Get("target_id").(string),
		d.Get("rule").(string),
		nil, conn)
	if err != nil {
		if regexp.MustCompile(" not found$").MatchString(err.Error()) {
			log.Printf("[WARN] Removing CloudWatch Event Target %q because it's gone.", d.Id())
			d.SetId("")
			return nil
		}
		if awsErr, ok := err.(awserr.Error); ok {
			// This should never happen, but it's useful
			// for recovering from https://github.com/hashicorp/terraform/issues/5389
			if awsErr.Code() == "ValidationException" {
				log.Printf("[WARN] Removing CloudWatch Event Target %q because it never existed.", d.Id())
				d.SetId("")
				return nil
			}
		}
		return err
	}
	log.Printf("[DEBUG] Found Event Target: %s", t)

	d.Set("arn", t.Arn)
	d.Set("target_id", t.Id)
	d.Set("input", t.Input)
	d.Set("input_path", t.InputPath)

	return nil
}

func findEventTargetById(id, rule string, nextToken *string, conn *events.CloudWatchEvents) (
	*events.Target, error) {
	input := events.ListTargetsByRuleInput{
		Rule:      aws.String(rule),
		NextToken: nextToken,
		Limit:     aws.Int64(100), // Set limit to allowed maximum to prevent API throttling
	}
	log.Printf("[DEBUG] Reading CloudWatch Event Target: %s", input)
	out, err := conn.ListTargetsByRule(&input)
	if err != nil {
		return nil, err
	}

	for _, t := range out.Targets {
		if *t.Id == id {
			return t, nil
		}
	}

	if out.NextToken != nil {
		return findEventTargetById(id, rule, nextToken, conn)
	}

	return nil, fmt.Errorf("CloudWatch Event Target %q (%q) not found", id, rule)
}

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

	input := buildPutTargetInputStruct(d)
	log.Printf("[DEBUG] Updating CloudWatch Event Target: %s", input)
	_, err := conn.PutTargets(input)
	if err != nil {
		return fmt.Errorf("Updating CloudWatch Event Target failed: %s", err)
	}

	return resourceAwsCloudWatchEventTargetRead(d, meta)
}

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

	input := events.RemoveTargetsInput{
		Ids:  []*string{aws.String(d.Get("target_id").(string))},
		Rule: aws.String(d.Get("rule").(string)),
	}
	log.Printf("[INFO] Deleting CloudWatch Event Target: %s", input)
	_, err := conn.RemoveTargets(&input)
	if err != nil {
		return fmt.Errorf("Error deleting CloudWatch Event Target: %s", err)
	}
	log.Println("[INFO] CloudWatch Event Target deleted")

	d.SetId("")

	return nil
}

func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput {
	e := &events.Target{
		Arn: aws.String(d.Get("arn").(string)),
		Id:  aws.String(d.Get("target_id").(string)),
	}

	if v, ok := d.GetOk("input"); ok {
		e.Input = aws.String(v.(string))
	}
	if v, ok := d.GetOk("input_path"); ok {
		e.InputPath = aws.String(v.(string))
	}

	input := events.PutTargetsInput{
		Rule:    aws.String(d.Get("rule").(string)),
		Targets: []*events.Target{e},
	}

	return &input
}
<|endoftext|>


<commit_before>// Copyright 2016-2022 terraform-provider-sakuracloud 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 sakuracloud

import (
	"context"
	"time"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"github.com/sacloud/libsacloud/v2/sacloud"
	"github.com/sacloud/libsacloud/v2/sacloud/types"
)

func resourceSakuraCloudDNS() *schema.Resource {
	resourceName := "DNS"
	return &schema.Resource{
		CreateContext: resourceSakuraCloudDNSCreate,
		ReadContext:   resourceSakuraCloudDNSRead,
		UpdateContext: resourceSakuraCloudDNSUpdate,
		DeleteContext: resourceSakuraCloudDNSDelete,
		Importer: &schema.ResourceImporter{
			StateContext: schema.ImportStatePassthroughContext,
		},

		Timeouts: &schema.ResourceTimeout{
			Create: schema.DefaultTimeout(5 * time.Minute),
			Update: schema.DefaultTimeout(5 * time.Minute),
			Delete: schema.DefaultTimeout(5 * time.Minute),
		},

		Schema: map[string]*schema.Schema{
			"zone": {
				Type:        schema.TypeString,
				Required:    true,
				ForceNew:    true,
				Description: "The target zone. (e.g. `example.com`)",
			},
			"dns_servers": {
				Type:        schema.TypeList,
				Computed:    true,
				Elem:        &schema.Schema{Type: schema.TypeString},
				Description: "A list of IP address of DNS server that manage this zone",
			},
			"record": {
				Type:     schema.TypeList,
				Optional: true,
				Computed: true,
				MaxItems: 1000,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"name": schemaResourceName("DNS Record"),
						"type": {
							Type:             schema.TypeString,
							Required:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(types.DNSRecordTypeStrings, false)),
							Description: descf(
								"The type of DNS Record. This must be one of [%s]",
								types.DNSRecordTypeStrings,
							),
						},
						"value": {
							Type:        schema.TypeString,
							Required:    true,
							Description: "The value of the DNS Record",
						},
						"ttl": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     defaultTTL,
							Description: "The number of the TTL",
						},
						"priority": {
							Type:             schema.TypeInt,
							Optional:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 65535)),
							Description:      descf("The priority of target DNS Record. %s", descRange(0, 65535)),
						},
						"weight": {
							Type:             schema.TypeInt,
							Optional:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 65535)),
							Description:      descf("The weight of target DNS Record. %s", descRange(0, 65535)),
						},
						"port": {
							Type:             schema.TypeInt,
							Optional:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(1, 65535)),
							Description:      descf("The number of port. %s", descRange(1, 65535)),
						},
					},
				},
			},
			"icon_id":     schemaResourceIconID(resourceName),
			"description": schemaResourceDescription(resourceName),
			"tags":        schemaResourceTags(resourceName),
		},
	}
}

func resourceSakuraCloudDNSCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)

	dnsOp := sacloud.NewDNSOp(client)
	dns, err := dnsOp.Create(ctx, expandDNSCreateRequest(d))
	if err != nil {
		return diag.Errorf("creating SakuraCloud DNS is failed: %s", err)
	}

	d.SetId(dns.ID.String())
	return resourceSakuraCloudDNSRead(ctx, d, meta)
}

func resourceSakuraCloudDNSRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)

	dnsOp := sacloud.NewDNSOp(client)
	dns, err := dnsOp.Read(ctx, sakuraCloudID(d.Id()))
	if err != nil {
		if sacloud.IsNotFoundError(err) {
			d.SetId("")
			return nil
		}
		return diag.Errorf("could not read SakuraCloud DNS[%s]: %s", d.Id(), err)
	}

	return setDNSResourceData(ctx, d, client, dns)
}

func resourceSakuraCloudDNSUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)
	dnsOp := sacloud.NewDNSOp(client)

	sakuraMutexKV.Lock(d.Id())
	defer sakuraMutexKV.Unlock(d.Id())

	dns, err := dnsOp.Read(ctx, sakuraCloudID(d.Id()))
	if err != nil {
		return diag.Errorf("could not read SakuraCloud DNS[%s]: %s", d.Id(), err)
	}

	if _, err := dnsOp.Update(ctx, dns.ID, expandDNSUpdateRequest(d, dns)); err != nil {
		return diag.Errorf("updating SakuraCloud DNS[%s] is failed: %s", d.Id(), err)
	}
	return resourceSakuraCloudDNSRead(ctx, d, meta)
}

func resourceSakuraCloudDNSDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)
	dnsOp := sacloud.NewDNSOp(client)

	sakuraMutexKV.Lock(d.Id())
	defer sakuraMutexKV.Unlock(d.Id())

	dns, err := dnsOp.Read(ctx, sakuraCloudID(d.Id()))
	if err != nil {
		if sacloud.IsNotFoundError(err) {
			d.SetId("")
			return nil
		}
		return diag.Errorf("could not read SakuraCloud DNS[%s]: %s", d.Id(), err)
	}

	if err := dnsOp.Delete(ctx, dns.ID); err != nil {
		return diag.Errorf("deleting SakuraCloud DNS[%s] is failed: %s", d.Id(), err)
	}
	return nil
}

func setDNSResourceData(ctx context.Context, d *schema.ResourceData, client *APIClient, data *sacloud.DNS) diag.Diagnostics {
	d.Set("zone", data.DNSZone)            // nolint
	d.Set("icon_id", data.IconID.String()) // nolint
	d.Set("description", data.Description) // nolint
	if err := d.Set("dns_servers", data.DNSNameServers); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("record", flattenDNSRecords(data)); err != nil {
		return diag.FromErr(err)
	}
	return diag.FromErr(d.Set("tags", flattenTags(data.Tags)))
}
<commit_msg>sakuracloud_dns: upgrade MaxItems to 2000<commit_after>// Copyright 2016-2022 terraform-provider-sakuracloud 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 sakuracloud

import (
	"context"
	"time"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"github.com/sacloud/libsacloud/v2/sacloud"
	"github.com/sacloud/libsacloud/v2/sacloud/types"
)

func resourceSakuraCloudDNS() *schema.Resource {
	resourceName := "DNS"
	return &schema.Resource{
		CreateContext: resourceSakuraCloudDNSCreate,
		ReadContext:   resourceSakuraCloudDNSRead,
		UpdateContext: resourceSakuraCloudDNSUpdate,
		DeleteContext: resourceSakuraCloudDNSDelete,
		Importer: &schema.ResourceImporter{
			StateContext: schema.ImportStatePassthroughContext,
		},

		Timeouts: &schema.ResourceTimeout{
			Create: schema.DefaultTimeout(5 * time.Minute),
			Update: schema.DefaultTimeout(5 * time.Minute),
			Delete: schema.DefaultTimeout(5 * time.Minute),
		},

		Schema: map[string]*schema.Schema{
			"zone": {
				Type:        schema.TypeString,
				Required:    true,
				ForceNew:    true,
				Description: "The target zone. (e.g. `example.com`)",
			},
			"dns_servers": {
				Type:        schema.TypeList,
				Computed:    true,
				Elem:        &schema.Schema{Type: schema.TypeString},
				Description: "A list of IP address of DNS server that manage this zone",
			},
			"record": {
				Type:     schema.TypeList,
				Optional: true,
				Computed: true,
				MaxItems: 2000,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"name": schemaResourceName("DNS Record"),
						"type": {
							Type:             schema.TypeString,
							Required:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(types.DNSRecordTypeStrings, false)),
							Description: descf(
								"The type of DNS Record. This must be one of [%s]",
								types.DNSRecordTypeStrings,
							),
						},
						"value": {
							Type:        schema.TypeString,
							Required:    true,
							Description: "The value of the DNS Record",
						},
						"ttl": {
							Type:        schema.TypeInt,
							Optional:    true,
							Default:     defaultTTL,
							Description: "The number of the TTL",
						},
						"priority": {
							Type:             schema.TypeInt,
							Optional:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 65535)),
							Description:      descf("The priority of target DNS Record. %s", descRange(0, 65535)),
						},
						"weight": {
							Type:             schema.TypeInt,
							Optional:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 65535)),
							Description:      descf("The weight of target DNS Record. %s", descRange(0, 65535)),
						},
						"port": {
							Type:             schema.TypeInt,
							Optional:         true,
							ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(1, 65535)),
							Description:      descf("The number of port. %s", descRange(1, 65535)),
						},
					},
				},
			},
			"icon_id":     schemaResourceIconID(resourceName),
			"description": schemaResourceDescription(resourceName),
			"tags":        schemaResourceTags(resourceName),
		},
	}
}

func resourceSakuraCloudDNSCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)

	dnsOp := sacloud.NewDNSOp(client)
	dns, err := dnsOp.Create(ctx, expandDNSCreateRequest(d))
	if err != nil {
		return diag.Errorf("creating SakuraCloud DNS is failed: %s", err)
	}

	d.SetId(dns.ID.String())
	return resourceSakuraCloudDNSRead(ctx, d, meta)
}

func resourceSakuraCloudDNSRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)

	dnsOp := sacloud.NewDNSOp(client)
	dns, err := dnsOp.Read(ctx, sakuraCloudID(d.Id()))
	if err != nil {
		if sacloud.IsNotFoundError(err) {
			d.SetId("")
			return nil
		}
		return diag.Errorf("could not read SakuraCloud DNS[%s]: %s", d.Id(), err)
	}

	return setDNSResourceData(ctx, d, client, dns)
}

func resourceSakuraCloudDNSUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)
	dnsOp := sacloud.NewDNSOp(client)

	sakuraMutexKV.Lock(d.Id())
	defer sakuraMutexKV.Unlock(d.Id())

	dns, err := dnsOp.Read(ctx, sakuraCloudID(d.Id()))
	if err != nil {
		return diag.Errorf("could not read SakuraCloud DNS[%s]: %s", d.Id(), err)
	}

	if _, err := dnsOp.Update(ctx, dns.ID, expandDNSUpdateRequest(d, dns)); err != nil {
		return diag.Errorf("updating SakuraCloud DNS[%s] is failed: %s", d.Id(), err)
	}
	return resourceSakuraCloudDNSRead(ctx, d, meta)
}

func resourceSakuraCloudDNSDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client := meta.(*APIClient)
	dnsOp := sacloud.NewDNSOp(client)

	sakuraMutexKV.Lock(d.Id())
	defer sakuraMutexKV.Unlock(d.Id())

	dns, err := dnsOp.Read(ctx, sakuraCloudID(d.Id()))
	if err != nil {
		if sacloud.IsNotFoundError(err) {
			d.SetId("")
			return nil
		}
		return diag.Errorf("could not read SakuraCloud DNS[%s]: %s", d.Id(), err)
	}

	if err := dnsOp.Delete(ctx, dns.ID); err != nil {
		return diag.Errorf("deleting SakuraCloud DNS[%s] is failed: %s", d.Id(), err)
	}
	return nil
}

func setDNSResourceData(ctx context.Context, d *schema.ResourceData, client *APIClient, data *sacloud.DNS) diag.Diagnostics {
	d.Set("zone", data.DNSZone)            // nolint
	d.Set("icon_id", data.IconID.String()) // nolint
	d.Set("description", data.Description) // nolint
	if err := d.Set("dns_servers", data.DNSNameServers); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("record", flattenDNSRecords(data)); err != nil {
		return diag.FromErr(err)
	}
	return diag.FromErr(d.Set("tags", flattenTags(data.Tags)))
}
<|endoftext|>


<commit_before>package rpc

import (
	"github.com/mitchellh/packer/packer"
	"net/rpc"
)

// An implementation of packer.Provisioner where the provisioner is actually
// executed over an RPC connection.
type provisioner struct {
	client *rpc.Client
}

// ProvisionerServer wraps a packer.Provisioner implementation and makes it
// exportable as part of a Golang RPC server.
type ProvisionerServer struct {
	p packer.Provisioner
}

type ProvisionerPrepareArgs struct {
	Configs []interface{}
}

type ProvisionerProvisionArgs struct {
	RPCAddress string
}

func Provisioner(client *rpc.Client) *provisioner {
	return &provisioner{client}
}
func (p *provisioner) Prepare(configs ...interface{}) (err error) {
	args := &ProvisionerPrepareArgs{configs}
	if cerr := p.client.Call("Provisioner.Prepare", args, &err); cerr != nil {
		err = cerr
	}

	return
}

func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
	// TODO: Error handling
	server := rpc.NewServer()
	RegisterCommunicator(server, comm)
	RegisterUi(server, ui)

	args := &ProvisionerProvisionArgs{serveSingleConn(server)}
	p.client.Call("Provisioner.Provision", args, new(interface{}))
}

func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *error) error {
	*reply = p.p.Prepare(args.Configs...)
	return nil
}

func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *interface{}) error {
	client, err := rpc.Dial("tcp", args.RPCAddress)
	if err != nil {
		return err
	}

	comm := Communicator(client)
	ui := &Ui{client}

	p.p.Provision(ui, comm)
	return nil
}
<commit_msg>packer/rpc: Wrap errors in BasicError for RPC config errors<commit_after>package rpc

import (
	"github.com/mitchellh/packer/packer"
	"net/rpc"
)

// An implementation of packer.Provisioner where the provisioner is actually
// executed over an RPC connection.
type provisioner struct {
	client *rpc.Client
}

// ProvisionerServer wraps a packer.Provisioner implementation and makes it
// exportable as part of a Golang RPC server.
type ProvisionerServer struct {
	p packer.Provisioner
}

type ProvisionerPrepareArgs struct {
	Configs []interface{}
}

type ProvisionerProvisionArgs struct {
	RPCAddress string
}

func Provisioner(client *rpc.Client) *provisioner {
	return &provisioner{client}
}
func (p *provisioner) Prepare(configs ...interface{}) (err error) {
	args := &ProvisionerPrepareArgs{configs}
	if cerr := p.client.Call("Provisioner.Prepare", args, &err); cerr != nil {
		err = cerr
	}

	return
}

func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
	// TODO: Error handling
	server := rpc.NewServer()
	RegisterCommunicator(server, comm)
	RegisterUi(server, ui)

	args := &ProvisionerProvisionArgs{serveSingleConn(server)}
	p.client.Call("Provisioner.Provision", args, new(interface{}))
}

func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *error) error {
	*reply = p.p.Prepare(args.Configs...)
	if *reply != nil {
		*reply = NewBasicError(*reply)
	}

	return nil
}

func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *interface{}) error {
	client, err := rpc.Dial("tcp", args.RPCAddress)
	if err != nil {
		return err
	}

	comm := Communicator(client)
	ui := &Ui{client}

	p.p.Provision(ui, comm)
	return nil
}
<|endoftext|>


<commit_before>package webhook

import (
	"encoding/json"
)

type EventType string

const (
	EventWebhookVerification EventType = "webhook_verification"
	EventAudienceAccepted    EventType = "audience_accepted"
	EventAudienceComplete    EventType = "audience_complete"
)

type AudienceAccepted struct {
	RealmID    string   `json:"realm_id"`
	QueueID    string   `json:"queue_id"`
	AudienceID string   `json:"audience_id"`
	Audience   Audience `json:"audience"`
	DialogueID []string `json:"dialogue_id,omitempty"`
	ChannelID  string   `json:"channel_id,omitempty"`
}

type AudienceComplete struct {
	RealmID    string    `json:"realm_id"`
	QueueID    string    `json:"queue_id"`
	AudienceID string    `json:"audience_id"`
	Audience   Audience  `json:"audience"`
	DialogueID []string  `json:"dialogue_id,omitempty"`
	ChannelID  string    `json:"channel_id,omitempty"`
	Messages   []Message `json:"messages,omitempty"`
}

type Audience struct {
	RequestTime  float64                   `json:"request_time"`
	AcceptTime   float64                   `json:"accept_time,omitempty"`
	FinishTime   float64                   `json:"finish_time,omitempty"`
	CompleteTime float64                   `json:"complete_time,omitempty"`
	Members      map[string]AudienceMember `json:"members"`
	Metadata     Metadata                  `json:"metadata,omitempty"`
}

type AudienceMember struct {
	Agent    bool `json:"agent,omitempty"`
	Customer bool `json:"customer,omitempty"`
}

type Message struct {
	ID       string      `json:"id"`
	Time     float64     `json:"time"`
	Type     MessageType `json:"type"`
	UserID   string      `json:"user_id,omitempty"`
	UserName *string     `json:"user_name,omitempty"`
	Fold     bool        `json:"fold,omitempty"`

	PayloadJSON json.RawMessage `json:"payload"`
}
<commit_msg>webhook: audience_complete: channel metadata<commit_after>package webhook

import (
	"encoding/json"
)

type EventType string

const (
	EventWebhookVerification EventType = "webhook_verification"
	EventAudienceAccepted    EventType = "audience_accepted"
	EventAudienceComplete    EventType = "audience_complete"
)

type AudienceAccepted struct {
	RealmID    string   `json:"realm_id"`
	QueueID    string   `json:"queue_id"`
	AudienceID string   `json:"audience_id"`
	Audience   Audience `json:"audience"`
	DialogueID []string `json:"dialogue_id,omitempty"`
	ChannelID  string   `json:"channel_id,omitempty"`
}

type AudienceComplete struct {
	RealmID    string    `json:"realm_id"`
	QueueID    string    `json:"queue_id"`
	AudienceID string    `json:"audience_id"`
	Audience   Audience  `json:"audience"`
	DialogueID []string  `json:"dialogue_id,omitempty"`
	ChannelID  string    `json:"channel_id,omitempty"`
	Channel    Channel   `json:"channel,omitempty"`
	Messages   []Message `json:"messages,omitempty"`
}

type Audience struct {
	RequestTime  float64                   `json:"request_time"`
	AcceptTime   float64                   `json:"accept_time,omitempty"`
	FinishTime   float64                   `json:"finish_time,omitempty"`
	CompleteTime float64                   `json:"complete_time,omitempty"`
	Members      map[string]AudienceMember `json:"members"`
	Metadata     Metadata                  `json:"metadata,omitempty"`
}

type AudienceMember struct {
	Agent    bool `json:"agent,omitempty"`
	Customer bool `json:"customer,omitempty"`
}

type Channel struct {
	Metadata Metadata `json:"metadata,omitempty"`
}

type Message struct {
	ID       string      `json:"id"`
	Time     float64     `json:"time"`
	Type     MessageType `json:"type"`
	UserID   string      `json:"user_id,omitempty"`
	UserName *string     `json:"user_name,omitempty"`
	Fold     bool        `json:"fold,omitempty"`

	PayloadJSON json.RawMessage `json:"payload"`
}
<|endoftext|>


<commit_before>package models

import (
	// Go Builtin Packages
	"bytes"
	"compress/zlib"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io"
	"log"
	"net/http"
	"regexp"
	"strings"
	"time"

	// Google Appengine Packages
	"appengine"
	"appengine/datastore"
	"appengine/user"
)

type Tags []string
type Paste struct {
	UserID  string    `datastore:"user_id"`
	Title   string    `datastore:"title"`
	Content []byte    `datastore:"content,noindex"`
	Tags    Tags      `datastore:"tags"`
	Format  string    `datastore:"format,noindex"`
	Date    time.Time `datastore:"date_published"`
	// We need the Zlib flag to correctly process old, uncompressed content
	Zlib bool `datastore:"zlib,noindex"`
}

func (p *Paste) Load(ds <-chan datastore.Property) error {
	// TODO: Do something with ErrFieldMismatch here
	if err := datastore.LoadStruct(p, ds); err != nil {
		return nil // Do nothing D:
	}
	return nil
}

func (p *Paste) Save(ds chan<- datastore.Property) error {
	return datastore.SaveStruct(p, ds)
}

const PasteDSKind string = "Paste"

func genpasteKey(c appengine.Context, p *Paste) (*datastore.Key, string) {
	timestamp := p.Date.Format(time.StampNano)

	hasher := sha256.New()
	hasher.Write([]byte(timestamp))
	hasher.Write(p.Content)
	digest := hex.EncodeToString(hasher.Sum(nil))

	paste_id := digest[:8] // This is probably a silly way to go about it xD
	// We're such trolls, we don't even check for collisions ^_^
	return datastore.NewKey(c, PasteDSKind, paste_id, 0, nil), paste_id
}

type ValidationError struct {
	What string // What is invalid
	Why  string // Why is it invalid
}

func (e ValidationError) Error() string {
	return fmt.Sprintf("%s - %s", e.What, e.Why)
}

func (p *Paste) Validate() error {
	// ... this looks more like a cleaner than a validator O_o

	// Title - truncate title to 50
	if len(p.Title) > 50 {
		p.Title = p.Title[:50]
	}

	// Tags - accept a maximum of 15 tags only, each of max length 15
	// Tags - must consist of alphanumeric characters only
	filter_exp := regexp.MustCompile("[^A-Za-z0-9]+")
	for index := 0; index < len(p.Tags); index++ {
		p.Tags[index] = filter_exp.ReplaceAllString(p.Tags[index], "")
		p.Tags[index] = strings.ToLower(strings.Trim(p.Tags[index], "-"))
		if len(p.Tags[index]) > 15 {
			p.Tags[index] = p.Tags[index][:15]
		}
	}
	if len(p.Tags) > 15 {
		p.Tags = p.Tags[:15]
	}

	var u_tags []string
	u_tag_map := make(map[string]string)
	for _, tag := range p.Tags {
		if u_tag_map[tag] == "" {
			u_tags = append(u_tags, tag)
		}
		u_tag_map[tag] = tag
	}

	p.Tags = u_tags // Yay no more duplicates!

	// return &ValidationError{"Huh?", "Why?"}
	return nil
}

func (p *Paste) save(c appengine.Context) (string, error) {
	if err := p.Validate(); err == nil {
		key, paste_id := genpasteKey(c, p)
		log.Printf("Creating new paste with paste_id [%s]", paste_id)
		_, err := datastore.Put(c, key, p)
		if err != nil {
			return "", err
		}
		return paste_id, nil
	} else {
		return "", err
	}
}

type pasteContent interface {
	Write([]byte) (int, error)
}

func (p *Paste) ZContent(pc pasteContent) {
	if p.Zlib { // always the case with new content
		// Decompress content and write out the response
		zbuffer := bytes.NewReader(p.Content)
		ureader, _ := zlib.NewReader(zbuffer)
		io.Copy(pc, ureader)
	} else { // here be old, uncompressed content
		buffer := bytes.NewReader(p.Content)
		io.Copy(pc, buffer)
	}
}

func (p *Paste) Delete(c appengine.Context, paste_id string) {
	key := datastore.NewKey(c, PasteDSKind, paste_id, 0, nil)
	log.Printf("Delete paste with paste_id [%s]", paste_id)
	if err := datastore.Delete(c, key); err != nil {
		log.Panic(err)
	}
}

func NewPaste(c appengine.Context, r *http.Request) (string, error) {
	var paste Paste

	if usr := user.Current(c); usr != nil {
		paste.UserID = usr.ID
	}

	paste.Title = r.PostForm.Get("title")

	var content bytes.Buffer
	w := zlib.NewWriter(&content)
	w.Write([]byte(r.PostForm.Get("content")))
	w.Close()
	paste.Content = content.Bytes()
	paste.Zlib = true

	paste.Tags = strings.Split(r.PostForm.Get("tags"), " ")
	paste.Format = r.PostForm.Get("format")

	paste.Date = time.Now()

	paste_id, err := paste.save(c)
	if err != nil {
		return "", err
	}

	return paste_id, nil
}

func GetPaste(c appengine.Context, paste_id string) (*Paste, error) {
	key := datastore.NewKey(c, PasteDSKind, paste_id, 0, nil)
	paste := new(Paste)
	if err := datastore.Get(c, key, paste); err != nil {
		return paste, err
	}

	return paste, nil
}
<commit_msg>Oops, I really meant a space there!<commit_after>package models

import (
	// Go Builtin Packages
	"bytes"
	"compress/zlib"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io"
	"log"
	"net/http"
	"regexp"
	"strings"
	"time"

	// Google Appengine Packages
	"appengine"
	"appengine/datastore"
	"appengine/user"
)

type Tags []string
type Paste struct {
	UserID  string    `datastore:"user_id"`
	Title   string    `datastore:"title"`
	Content []byte    `datastore:"content,noindex"`
	Tags    Tags      `datastore:"tags"`
	Format  string    `datastore:"format,noindex"`
	Date    time.Time `datastore:"date_published"`
	// We need the Zlib flag to correctly process old, uncompressed content
	Zlib bool `datastore:"zlib,noindex"`
}

func (p *Paste) Load(ds <-chan datastore.Property) error {
	// TODO: Do something with ErrFieldMismatch here
	if err := datastore.LoadStruct(p, ds); err != nil {
		return nil // Do nothing D:
	}
	return nil
}

func (p *Paste) Save(ds chan<- datastore.Property) error {
	return datastore.SaveStruct(p, ds)
}

const PasteDSKind string = "Paste"

func genpasteKey(c appengine.Context, p *Paste) (*datastore.Key, string) {
	timestamp := p.Date.Format(time.StampNano)

	hasher := sha256.New()
	hasher.Write([]byte(timestamp))
	hasher.Write(p.Content)
	digest := hex.EncodeToString(hasher.Sum(nil))

	paste_id := digest[:8] // This is probably a silly way to go about it xD
	// We're such trolls, we don't even check for collisions ^_^
	return datastore.NewKey(c, PasteDSKind, paste_id, 0, nil), paste_id
}

type ValidationError struct {
	What string // What is invalid
	Why  string // Why is it invalid
}

func (e ValidationError) Error() string {
	return fmt.Sprintf("%s - %s", e.What, e.Why)
}

func (p *Paste) Validate() error {
	// ... this looks more like a cleaner than a validator O_o

	// Title - truncate title to 50
	if len(p.Title) > 50 {
		p.Title = p.Title[:50]
	}

	// Tags - accept a maximum of 15 tags only, each of max length 15
	// Tags - must consist of alphanumeric characters only
	filter_exp := regexp.MustCompile("[^A-Za-z0-9]+")
	for index := 0; index < len(p.Tags); index++ {
		p.Tags[index] = filter_exp.ReplaceAllString(p.Tags[index], "")
		p.Tags[index] = strings.ToLower(strings.Trim(p.Tags[index], " "))
		if len(p.Tags[index]) > 15 {
			p.Tags[index] = p.Tags[index][:15]
		}
	}
	if len(p.Tags) > 15 {
		p.Tags = p.Tags[:15]
	}

	var u_tags []string
	u_tag_map := make(map[string]string)
	for _, tag := range p.Tags {
		if u_tag_map[tag] == "" {
			u_tags = append(u_tags, tag)
		}
		u_tag_map[tag] = tag
	}

	p.Tags = u_tags // Yay no more duplicates!

	// return &ValidationError{"Huh?", "Why?"}
	return nil
}

func (p *Paste) save(c appengine.Context) (string, error) {
	if err := p.Validate(); err == nil {
		key, paste_id := genpasteKey(c, p)
		log.Printf("Creating new paste with paste_id [%s]", paste_id)
		_, err := datastore.Put(c, key, p)
		if err != nil {
			return "", err
		}
		return paste_id, nil
	} else {
		return "", err
	}
}

type pasteContent interface {
	Write([]byte) (int, error)
}

func (p *Paste) ZContent(pc pasteContent) {
	if p.Zlib { // always the case with new content
		// Decompress content and write out the response
		zbuffer := bytes.NewReader(p.Content)
		ureader, _ := zlib.NewReader(zbuffer)
		io.Copy(pc, ureader)
	} else { // here be old, uncompressed content
		buffer := bytes.NewReader(p.Content)
		io.Copy(pc, buffer)
	}
}

func (p *Paste) Delete(c appengine.Context, paste_id string) {
	key := datastore.NewKey(c, PasteDSKind, paste_id, 0, nil)
	log.Printf("Delete paste with paste_id [%s]", paste_id)
	if err := datastore.Delete(c, key); err != nil {
		log.Panic(err)
	}
}

func NewPaste(c appengine.Context, r *http.Request) (string, error) {
	var paste Paste

	if usr := user.Current(c); usr != nil {
		paste.UserID = usr.ID
	}

	paste.Title = r.PostForm.Get("title")

	var content bytes.Buffer
	w := zlib.NewWriter(&content)
	w.Write([]byte(r.PostForm.Get("content")))
	w.Close()
	paste.Content = content.Bytes()
	paste.Zlib = true

	paste.Tags = strings.Split(r.PostForm.Get("tags"), " ")
	paste.Format = r.PostForm.Get("format")

	paste.Date = time.Now()

	paste_id, err := paste.save(c)
	if err != nil {
		return "", err
	}

	return paste_id, nil
}

func GetPaste(c appengine.Context, paste_id string) (*Paste, error) {
	key := datastore.NewKey(c, PasteDSKind, paste_id, 0, nil)
	paste := new(Paste)
	if err := datastore.Get(c, key, paste); err != nil {
		return paste, err
	}

	return paste, nil
}
<|endoftext|>