<commit_before>package xorm

import (
	"fmt"

	"github.com/coscms/xorm/core"
)

var DefaultShowLog = map[string]bool{
	"sql":   true,
	"etime": true,
	"cache": true,
	"event": true,
	"base":  true,
	"other": true,
}

var defaultLogProcessor = func(tag string, format string, args []interface{}) (string, []interface{}) {
	if format == "" {
		if len(args) > 0 {
			args[0] = fmt.Sprintf("[%s] %v", tag, args[0])
		}
		return format, args
	}
	format = "[" + tag + "] " + format
	return format, args
}
var LogTagProcessor = map[string]func(tag string, format string, args []interface{}) (string, []interface{}){
	"cache": defaultLogProcessor,
	"event": defaultLogProcessor,
	"sql":   defaultLogProcessor,
	"etime": defaultLogProcessor,
	"base":  defaultLogProcessor,
	"other": defaultLogProcessor,
}

// =====================================
// 增加Engine结构体中的方法
// @author Admpub <swh@admpub.com>
// =====================================

func (engine *Engine) Init() {
	engine.showLog = DefaultShowLog
}

func (engine *Engine) SetTblMapper(mapper core.IMapper) {
	if prefixMapper, ok := mapper.(core.PrefixMapper); ok {
		engine.TablePrefix = prefixMapper.Prefix
	} else if suffixMapper, ok := mapper.(core.SuffixMapper); ok {
		engine.TableSuffix = suffixMapper.Suffix
	}
	engine.TableMapper = mapper
}

func (engine *Engine) OpenLog(types ...string) {
	if len(types) < 1 {
		for typ, _ := range engine.showLog {
			engine.showLog[typ] = true
			if typ == "sql" {
				engine.ShowSQL(engine.showLog[typ])
			}
		}
		return
	}
	for _, typ := range types {
		engine.showLog[typ] = true
		if typ == "sql" {
			engine.ShowSQL(engine.showLog[typ])
		}
	}
}

func (engine *Engine) CloseLog(types ...string) {
	if len(types) < 1 {
		for typ, _ := range engine.showLog {
			engine.showLog[typ] = false
			if typ == "sql" {
				engine.ShowSQL(engine.showLog[typ])
			}
		}
		return
	}
	for _, typ := range types {
		engine.showLog[typ] = false
		if typ == "sql" {
			engine.ShowSQL(engine.showLog[typ])
		}
	}
}

func (engine *Engine) TagLogError(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Error(contents...)
}

func (engine *Engine) TagLogErrorf(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Errorf(format, contents...)
}

// logging info
func (engine *Engine) TagLogInfo(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Info(contents...)
}

func (engine *Engine) TagLogInfof(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Infof(format, contents...)
}

// logging debug
func (engine *Engine) TagLogDebug(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Debug(contents...)
}

func (engine *Engine) TagLogDebugf(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Debugf(format, contents...)
}

// logging warn
func (engine *Engine) TagLogWarn(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Warn(contents...)
}

func (engine *Engine) TagLogWarnf(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Warnf(format, contents...)
}
<commit_msg>improved<commit_after>package xorm

import (
	"fmt"

	"github.com/coscms/xorm/core"
)

var DefaultShowLog = map[string]bool{
	"sql":   true,
	"etime": true,
	"cache": true,
	"event": true,
	"base":  true,
	"other": true,
}

var defaultLogProcessor = func(tag string, format string, args []interface{}) (string, []interface{}) {
	if format == "" {
		if len(args) > 0 {
			args[0] = fmt.Sprintf("[%s] %v", tag, args[0])
		}
		return format, args
	}
	format = "[" + tag + "] " + format
	return format, args
}
var LogTagProcessor = map[string]func(tag string, format string, args []interface{}) (string, []interface{}){
	"cache": defaultLogProcessor,
	"event": defaultLogProcessor,
	"sql":   defaultLogProcessor,
	"etime": defaultLogProcessor,
	"base":  defaultLogProcessor,
	"other": defaultLogProcessor,
}

// =====================================
// 增加Engine结构体中的方法
// @author Admpub <swh@admpub.com>
// =====================================

func (engine *Engine) Init() {
	engine.showLog = DefaultShowLog
}

func (engine *Engine) SetTblMapper(mapper core.IMapper) {
	if prefixMapper, ok := mapper.(core.PrefixMapper); ok {
		engine.TablePrefix = prefixMapper.Prefix
	} else if suffixMapper, ok := mapper.(core.SuffixMapper); ok {
		engine.TableSuffix = suffixMapper.Suffix
	}
	engine.TableMapper = mapper
}

func (engine *Engine) OpenLog(types ...string) {
	if len(types) < 1 {
		for typ, _ := range engine.showLog {
			engine.setLogStatus(typ, true)
		}
		return
	}
	for _, typ := range types {
		engine.setLogStatus(typ, true)
	}
}

func (engine *Engine) setLogStatus(typ string, on bool) {
	engine.showLog[typ] = on
	if typ == "sql" {
		engine.ShowSQL(on)
	} else if typ == "etime" {
		engine.ShowExecTime(on)
	}
}

func (engine *Engine) CloseLog(types ...string) {
	if len(types) < 1 {
		for typ, _ := range engine.showLog {
			engine.setLogStatus(typ, false)
		}
		return
	}
	for _, typ := range types {
		engine.setLogStatus(typ, false)
	}
}

func (engine *Engine) TagLogError(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Error(contents...)
}

func (engine *Engine) TagLogErrorf(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Errorf(format, contents...)
}

// logging info
func (engine *Engine) TagLogInfo(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Info(contents...)
}

func (engine *Engine) TagLogInfof(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Infof(format, contents...)
}

// logging debug
func (engine *Engine) TagLogDebug(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Debug(contents...)
}

func (engine *Engine) TagLogDebugf(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Debugf(format, contents...)
}

// logging warn
func (engine *Engine) TagLogWarn(tag string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		_, contents = fn(tag, "", contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Warn(contents...)
}

func (engine *Engine) TagLogWarnf(tag string, format string, contents ...interface{}) {
	if enable, _ := engine.showLog[tag]; !enable {
		return
	}
	if fn, ok := LogTagProcessor[tag]; ok {
		format, contents = fn(tag, format, contents)
		if contents == nil {
			return
		}
	}
	engine.logger.Warnf(format, contents...)
}
<|endoftext|>


<commit_before>package errs

import "errors"

var (
	// ErrHandlerNameDuplicate occurs when a handler is attempted to
	// be registered but another handler with the same name already
	// exists
	ErrHandlerNameDuplicate = errors.New("Duplicate Handler Name!")
)
<commit_msg>Added new error messages<commit_after>package errs

import "errors"

var (
	// ErrHandlerNameDuplicate occurs when a handler is attempted to
	// be registered but another handler with the same name already
	// exists
	ErrHandlerNameDuplicate = errors.New("Duplicate Handler Name!")
	// ErrHandlerNotSupported indicates that it was attempted to add
	// a handler to a mux that doesn't support the specific handler type.
	ErrHandlerNotSupported = errors.New("Handler not supported")
	// ErrNotImplemnted indicates a particular function has not been implemented
	// yet.
	ErrNotImplemnted = errors.New("Function not implemented yet")
)
<|endoftext|>


<commit_before>package lda

import(
    "bufio"
    "os"
    "strings"
    "fmt"
    "math/rand"
)

type Estimator struct {
    Model *LDAModel
    Param *LDAParams
    Data []*Document
}

func NewEstimator(param *LDAParams) *Estimator {
    e := Estimator{}
    e.Param = param
    e.Model = InitModel(param)
    e.Data = []*Document{}
    e.LoadData(e.Param.Input)
    return &e
}

func (e *Estimator) LoadData(fileName string) error {
    file, err := os.Open(fileName)
    if err != nil {
        return err
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        line := scanner.Text()
        tks := strings.Split(line, "\t");
        if len(tks) < 2  { continue }
        doc := NewDoc(tks[1], e.Model.word_dic)
        doc.Id = tks[0]
        e.Data = append(e.Data, doc)
    }

    return nil
}

func (e *Estimator) Train() {
    // init model
    z := []*IntVector{} // topic assignments for words, size M(number of docs) x doc.size() (word count in a doc)
    nd := []*IntVector{} //nd[i][j]: number of words in document i assigned to topic j, size M x K
    nwsum := NewIntVector() //nwsum[j]: total number of words assigned to topic j, size K
    ndsum := NewIntVector() //ndsum[i]: total number of words in document i, size M

    for m := int64(0); m < int64(len(e.Data)); m++ {
        doc := e.Data[m]
        z = append(z, NewIntVector())
        nd = append(nd, NewIntVector())

        //initilize for z
        for n := int64(0); n < int64(len(doc.Words)); n++ {
            topic := int64(rand.Float64() * float64(e.Param.Ntopics))
            z[m].AddValue(n, topic)

            // number of instances of word assigned to topic j
            e.Model.IncreaseNWZ(doc.Words[n], topic)

            // number of words in document i assigned to topic j
            nd[m].AddValue(topic, 1);
            // total number of words assigned to topic j
            nwsum.AddValue(topic, 1);
        }

        // total number of words in document i
        ndsum.AddValue(m, int64(len(doc.Words)))
    }

    // train lda model
    for iter := int64(0); iter < e.Param.Niters; iter++ {
        fmt.Println("Iteration ", iter, " ...")

        // for all z_i

    }

}

<commit_msg>add sample part.<commit_after>package lda

import(
    "bufio"
    "os"
    "strings"
    "fmt"
    "math/rand"
)

type Estimator struct {
    Model *LDAModel
    Param *LDAParams
    Data []*Document
}

func NewEstimator(param *LDAParams) *Estimator {
    e := Estimator{}
    e.Param = param
    e.Model = InitModel(param)
    e.Data = []*Document{}
    e.LoadData(e.Param.Input)
    return &e
}

func (e *Estimator) LoadData(fileName string) error {
    file, err := os.Open(fileName)
    if err != nil {
        return err
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        line := scanner.Text()
        tks := strings.Split(line, "\t");
        if len(tks) < 2  { continue }
        doc := NewDoc(tks[1], e.Model.word_dic)
        doc.Id = tks[0]
        e.Data = append(e.Data, doc)
    }

    return nil
}

func (e *Estimator) Train() {
    // init model
    z := []*IntVector{} // topic assignments for words, size M(number of docs) x doc.size() (word count in a doc)
    nd := []*IntVector{} //nd[i][j]: number of words in document i assigned to topic j, size M x K
    ndsum := NewIntVector() //ndsum[i]: total number of words in document i, size M

    for m := int64(0); m < int64(len(e.Data)); m++ {
        doc := e.Data[m]
        z = append(z, NewIntVector())
        nd = append(nd, NewIntVector())

        //initilize for z
        for n := int64(0); n < int64(len(doc.Words)); n++ {
            topic := int64(rand.Float64() * float64(e.Param.Ntopics))
            z[m].AddValue(n, topic)

            // number of instances of word assigned to topic j
            e.Model.IncreaseNWZ(doc.Words[n], topic)

            // number of words in document i assigned to topic j
            nd[m].AddValue(topic, 1);
            // total number of words assigned to topic j
            e.Model.IncreaseNZ(topic)
        }

        // total number of words in document i
        ndsum.AddValue(m, int64(len(doc.Words)))
    }

    // train lda model
    for iter := int64(0); iter < e.Param.Niters; iter++ {
        fmt.Println("Iteration ", iter, " ...")

        // for all z_i
        for m := int64(0); m < int64(len(e.Data)); m++ {
            doc := e.Data[m]
            for n := int64(0); n < int64(len(doc.Words)); n++ {
                // z_i = z[m][n]
                topic := z[m].GetValue(n)
                word := doc.Words[n]

                // before sample
                e.Model.DecreaseNWZ(word, topic)
                e.Model.DecreaseNZ(topic)
                nd[m].AddValue(topic, -1)
                ndsum.AddValue(m, -1)

                // sample from p(z_i|z_-i, w)
                pr := e.CalSamplePr(word, nd[m], ndsum.GetValue(m))
                new_topic := pr.Sample()

                // add newly estimated z_i to count variables
                e.Model.IncreaseNWZ(word, new_topic)
                e.Model.IncreaseNZ(topic)
                nd[m].AddValue(topic, 1)
                ndsum.AddValue(m, 1)

                z[m].SetValue(n, topic)
            }// end for each word
        }// end for each document
    }// end iterations

    fmt.Println("Gibbs sampling completed!");
    fmt.Println("Saving the final model!");

    // calcPerplexity

    // save model
}

func (e *Estimator) CalSamplePr(word int64, ndm *IntVector, ndsumm int64) *Prod{
    pr := NewProd()
    Vbeta := e.Model.beta * float64(e.Model.GetWordCount())
    Kalpha := e.Model.alpha * float64(e.Model.ntopics)
    for k := int64(0); k < e.Model.ntopics ; k++ {
        pr_k := (float64(e.Model.GetNWZElement(word, k)) + e.Model.beta) / (float64(e.Model.GetNZElement(k)) + Vbeta) *
                (float64(ndm.GetValue(k)) + e.Model.alpha) / (float64(ndsumm) + Kalpha)
        pr.AddValue(k, pr_k)
    }

    return pr

}<|endoftext|>


<commit_before>// Copyright 2015 The Numgrad Authors. All rights reserved.
// See the LICENSE file for rights to use this source code.

package eval

import (
	"fmt"
	"math/big"

	"numgrad.io/lang/expr"
	"numgrad.io/lang/stmt"
	"numgrad.io/lang/token"
)

type Variable struct {
	// Value has the type:
	//	nil
	//	int64
	//	float32
	//	float64
	//	*big.Int
	//	*big.Float
	//	*Struct
	Value interface{}
}

type Scope struct {
	Parent *Scope
	Var    map[string]*Variable // variable name -> variable
}

type Program struct {
	Pkg       map[string]*Scope // package -> scope
	Cur       *Scope
	Returning bool
}

func (p *Program) Eval(s stmt.Stmt) ([]interface{}, error) {
	if p.Cur == nil {
		p.Cur = p.Pkg["main"]
	}
	return p.evalStmt(s)
}

func (p *Program) pushScope() {
	p.Cur = &Scope{
		Parent: p.Cur,
		Var:    make(map[string]*Variable),
	}
}
func (p *Program) popScope() {
	p.Cur = p.Cur.Parent
}

func (p *Program) evalStmt(s stmt.Stmt) ([]interface{}, error) {
	switch s := s.(type) {
	case *stmt.Assign:
		vars := make([]*Variable, len(s.Left))
		if s.Decl {
			for i, lhs := range s.Left {
				vars[i] = new(Variable)
				p.Cur.Var[lhs.(*expr.Ident).Name] = vars[i]
			}
		} else {
			// TODO: order of evaluation, left-then-right,
			// or right-then-left?
			for i, lhs := range s.Left {
				v, err := p.evalExpr(lhs)
				if err != nil {
					return nil, err
				}
				vars[i] = v[0].(*Variable)
			}
		}
		vals := make([]interface{}, 0, len(s.Left))
		for _, rhs := range s.Right {
			v, err := p.evalExprAndReadVars(rhs)
			if err != nil {
				return nil, err
			}
			vals = append(vals, v...)
		}
		for i := range vars {
			vars[i].Value = vals[i]
		}
		return nil, nil
	case *stmt.Simple:
		return p.evalExpr(s.Expr)
	case *stmt.Block:
		p.pushScope()
		defer p.popScope()
		for _, s := range s.Stmts {
			res, err := p.evalStmt(s)
			if err != nil {
				return nil, err
			}
			if p.Returning {
				return res, nil
			}
		}
		return nil, nil
	case *stmt.If:
		if s.Init != nil {
			if _, err := p.evalStmt(s.Init); err != nil {
				return nil, err
			}
		}
		cond, err := p.evalExprAndReadVar(s.Cond)
		if err != nil {
			return nil, err
		}
		if cond.(bool) {
			return p.evalStmt(s.Body)
		} else if s.Else != nil {
			return p.evalStmt(s.Else)
		}
		return nil, nil
	case *stmt.Return:
		var err error
		var res []interface{}
		if len(s.Exprs) == 1 {
			res, err = p.evalExprAndReadVars(s.Exprs[0])
		} else {
			res = make([]interface{}, len(s.Exprs))
			for i, e := range s.Exprs {
				res[i], err = p.evalExprAndReadVar(e)
				if err != nil {
					break
				}
			}
		}
		p.Returning = true
		if err != nil {
			return nil, err
		}
		return res, nil
	}
	panic(fmt.Sprintf("TODO evalStmt: %T: %s", s, s.Sexp()))
}

func (p *Program) evalExprAndReadVars(e expr.Expr) ([]interface{}, error) {
	res, err := p.evalExpr(e)
	if err != nil {
		return nil, err
	}
	for i, v := range res {
		res[i], err = p.readVar(v)
		if err != nil {
			return nil, err
		}
	}
	return res, nil
}

func (p *Program) evalExprAndReadVar(e expr.Expr) (interface{}, error) {
	res, err := p.evalExpr(e)
	if err != nil {
		return nil, err
	}
	if len(res) != 1 { // TODO these kinds of invariants are the job of the type checker
		return nil, fmt.Errorf("multi-valued (%d) expression in single-value context", len(res))
	}
	return p.readVar(res[0])
}

func (p *Program) readVar(e interface{}) (interface{}, error) {
	switch v := e.(type) {
	case *expr.BasicLiteral:
		return v.Value, nil
	case *Variable:
		return v.Value, nil
	case bool, int64, float32, float64, *big.Int, *big.Float:
		return v, nil
	default:
		return nil, fmt.Errorf("unexpected type %T for value", v)
	}
}

func (p *Program) evalExpr(e expr.Expr) ([]interface{}, error) {
	switch e := e.(type) {
	case *expr.BasicLiteral, *expr.FuncLiteral:
		return []interface{}{e}, nil
	case *expr.Ident:
		for sc := p.Cur; sc != nil; sc = sc.Parent {
			if v, ok := sc.Var[e.Name]; ok {
				return []interface{}{v}, nil
			}
		}
		return nil, fmt.Errorf("eval: undefined identifier: %q", e.Name)
	case *expr.Unary:
		switch e.Op {
		case token.LeftParen:
			return p.evalExpr(e.Expr)
		case token.Not:
			v, err := p.evalExprAndReadVar(e.Expr)
			if err != nil {
				return nil, err
			}
			if v, ok := v.(bool); ok {
				return []interface{}{!v}, nil
			}
			return nil, fmt.Errorf("negation operator expects boolean expression, not %T", v)
		case token.Sub:
			rhs, err := p.evalExprAndReadVar(e.Expr)
			if err != nil {
				return nil, err
			}
			var lhs interface{}
			switch rhs.(type) {
			case int64:
				lhs = int64(0)
			case float32:
				lhs = float32(0)
			case float64:
				lhs = float64(0)
			case *big.Int:
				lhs = big.NewInt(0)
			case *big.Float:
				lhs = big.NewFloat(0)
			}
			v, err := binOp(token.Sub, lhs, rhs)
			if err != nil {
				return nil, err
			}
			return []interface{}{v}, nil
		}
	case *expr.Binary:
		lhs, err := p.evalExprAndReadVar(e.Left)
		if err != nil {
			return nil, err
		}

		switch e.Op {
		case token.LogicalAnd:
			panic("TODO LogicalAnd") // maybe skip RHS
		case token.LogicalOr:
			panic("TODO LogicalOr") // maybe skip RHS
		}

		rhs, err := p.evalExprAndReadVar(e.Right)
		if err != nil {
			return nil, err
		}

		v, err := binOp(e.Op, lhs, rhs)
		if err != nil {
			return nil, err
		}
		return []interface{}{v}, nil
	case *expr.Call:
		res, err := p.evalExpr(e.Func)
		if err != nil {
			return nil, err
		}
		if len(res) != 1 {
			return nil, fmt.Errorf("multi-valued (%d) expression when expecting single-value function", len(res))
		}
		switch fn := res[0].(type) {
		case *expr.FuncLiteral:
			p.pushScope()
			defer p.popScope()
			res, err := p.evalStmt(fn.Body.(*stmt.Block))
			if err != nil {
				return nil, err
			}
			if p.Returning {
				p.Returning = false
			} else if len(fn.Type.Out) > 0 {
				return nil, fmt.Errorf("missing return %v", fn.Type.Out)
			}
			return res, nil
		}
	}
	return nil, fmt.Errorf("TODO evalExpr(%s), %T", e.Sexp(), e)
}
<commit_msg>eval: bug fixes<commit_after>// Copyright 2015 The Numgrad Authors. All rights reserved.
// See the LICENSE file for rights to use this source code.

package eval

import (
	"fmt"
	"math/big"

	"numgrad.io/lang/expr"
	"numgrad.io/lang/stmt"
	"numgrad.io/lang/token"
)

type Variable struct {
	// Value has the type:
	//	nil
	//	int64
	//	float32
	//	float64
	//	*big.Int
	//	*big.Float
	//	*Struct
	Value interface{}
}

type Scope struct {
	Parent *Scope
	Var    map[string]*Variable // variable name -> variable
}

type Program struct {
	Pkg       map[string]*Scope // package -> scope
	Cur       *Scope
	Returning bool
}

func (p *Program) Eval(s stmt.Stmt) ([]interface{}, error) {
	if p.Cur == nil {
		p.Cur = p.Pkg["main"]
	}
	res, err := p.evalStmt(s)
	if err != nil {
		return nil, err
	}
	for i, v := range res {
		res[i], err = p.readVar(v)
		if err != nil {
			return nil, err
		}
	}
	return res, nil
}

func (p *Program) pushScope() {
	p.Cur = &Scope{
		Parent: p.Cur,
		Var:    make(map[string]*Variable),
	}
}
func (p *Program) popScope() {
	p.Cur = p.Cur.Parent
}

func (p *Program) evalStmt(s stmt.Stmt) ([]interface{}, error) {
	switch s := s.(type) {
	case *stmt.Assign:
		vars := make([]*Variable, len(s.Left))
		if s.Decl {
			for i, lhs := range s.Left {
				vars[i] = new(Variable)
				p.Cur.Var[lhs.(*expr.Ident).Name] = vars[i]
			}
		} else {
			// TODO: order of evaluation, left-then-right,
			// or right-then-left?
			for i, lhs := range s.Left {
				v, err := p.evalExpr(lhs)
				if err != nil {
					return nil, err
				}
				vars[i] = v[0].(*Variable)
			}
		}
		vals := make([]interface{}, 0, len(s.Left))
		for _, rhs := range s.Right {
			v, err := p.evalExprAndReadVars(rhs)
			if err != nil {
				return nil, err
			}
			vals = append(vals, v...)
		}
		for i := range vars {
			vars[i].Value = vals[i]
		}
		return nil, nil
	case *stmt.Simple:
		return p.evalExpr(s.Expr)
	case *stmt.Block:
		p.pushScope()
		defer p.popScope()
		for _, s := range s.Stmts {
			res, err := p.evalStmt(s)
			if err != nil {
				return nil, err
			}
			if p.Returning {
				return res, nil
			}
		}
		return nil, nil
	case *stmt.If:
		if s.Init != nil {
			if _, err := p.evalStmt(s.Init); err != nil {
				return nil, err
			}
		}
		cond, err := p.evalExprAndReadVar(s.Cond)
		if err != nil {
			return nil, err
		}
		if cond.(bool) {
			return p.evalStmt(s.Body)
		} else if s.Else != nil {
			return p.evalStmt(s.Else)
		}
		return nil, nil
	case *stmt.Return:
		var err error
		var res []interface{}
		if len(s.Exprs) == 1 {
			res, err = p.evalExprAndReadVars(s.Exprs[0])
		} else {
			res = make([]interface{}, len(s.Exprs))
			for i, e := range s.Exprs {
				res[i], err = p.evalExprAndReadVar(e)
				if err != nil {
					break
				}
			}
		}
		p.Returning = true
		if err != nil {
			return nil, err
		}
		return res, nil
	}
	if s == nil {
		return nil, fmt.Errorf("Parser.evalStmt: statement is nil")
	}
	panic(fmt.Sprintf("TODO evalStmt: %T: %s", s, s.Sexp()))
}

func (p *Program) evalExprAndReadVars(e expr.Expr) ([]interface{}, error) {
	res, err := p.evalExpr(e)
	if err != nil {
		return nil, err
	}
	for i, v := range res {
		res[i], err = p.readVar(v)
		if err != nil {
			return nil, err
		}
	}
	return res, nil
}

func (p *Program) evalExprAndReadVar(e expr.Expr) (interface{}, error) {
	res, err := p.evalExpr(e)
	if err != nil {
		return nil, err
	}
	if len(res) != 1 { // TODO these kinds of invariants are the job of the type checker
		return nil, fmt.Errorf("multi-valued (%d) expression in single-value context", len(res))
	}
	return p.readVar(res[0])
}

func (p *Program) readVar(e interface{}) (interface{}, error) {
	switch v := e.(type) {
	case *expr.FuncLiteral:
		// lack of symmetry with BasicLiteral is unfortunate
		return v, nil
	case *expr.BasicLiteral:
		return v.Value, nil
	case *Variable:
		return v.Value, nil
	case bool, int64, float32, float64, *big.Int, *big.Float:
		return v, nil
	default:
		return nil, fmt.Errorf("unexpected type %T for value", v)
	}
}

func (p *Program) evalExpr(e expr.Expr) ([]interface{}, error) {
	switch e := e.(type) {
	case *expr.BasicLiteral, *expr.FuncLiteral:
		return []interface{}{e}, nil
	case *expr.Ident:
		for sc := p.Cur; sc != nil; sc = sc.Parent {
			if v, ok := sc.Var[e.Name]; ok {
				return []interface{}{v}, nil
			}
		}
		return nil, fmt.Errorf("eval: undefined identifier: %q", e.Name)
	case *expr.Unary:
		switch e.Op {
		case token.LeftParen:
			return p.evalExpr(e.Expr)
		case token.Not:
			v, err := p.evalExprAndReadVar(e.Expr)
			if err != nil {
				return nil, err
			}
			if v, ok := v.(bool); ok {
				return []interface{}{!v}, nil
			}
			return nil, fmt.Errorf("negation operator expects boolean expression, not %T", v)
		case token.Sub:
			rhs, err := p.evalExprAndReadVar(e.Expr)
			if err != nil {
				return nil, err
			}
			var lhs interface{}
			switch rhs.(type) {
			case int64:
				lhs = int64(0)
			case float32:
				lhs = float32(0)
			case float64:
				lhs = float64(0)
			case *big.Int:
				lhs = big.NewInt(0)
			case *big.Float:
				lhs = big.NewFloat(0)
			}
			v, err := binOp(token.Sub, lhs, rhs)
			if err != nil {
				return nil, err
			}
			return []interface{}{v}, nil
		}
	case *expr.Binary:
		lhs, err := p.evalExprAndReadVar(e.Left)
		if err != nil {
			return nil, err
		}

		switch e.Op {
		case token.LogicalAnd:
			panic("TODO LogicalAnd") // maybe skip RHS
		case token.LogicalOr:
			panic("TODO LogicalOr") // maybe skip RHS
		}

		rhs, err := p.evalExprAndReadVar(e.Right)
		if err != nil {
			return nil, err
		}

		v, err := binOp(e.Op, lhs, rhs)
		if err != nil {
			return nil, err
		}
		return []interface{}{v}, nil
	case *expr.Call:
		res, err := p.evalExprAndReadVar(e.Func)
		if err != nil {
			return nil, err
		}
		switch fn := res.(type) {
		case *expr.FuncLiteral:
			p.pushScope()
			defer p.popScope()
			res, err := p.evalStmt(fn.Body.(*stmt.Block))
			if err != nil {
				return nil, err
			}
			if p.Returning {
				p.Returning = false
			} else if len(fn.Type.Out) > 0 {
				return nil, fmt.Errorf("missing return %v", fn.Type.Out)
			}
			return res, nil
		}
	}
	return nil, fmt.Errorf("TODO evalExpr(%s), %T", e.Sexp(), e)
}
<|endoftext|>


<commit_before>package integration

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/qb0C80aE/clay/db"
	"github.com/qb0C80aE/clay/server"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"reflect"
	"testing"
	"time"
)

const version = "v1"
const timeout = 15

// EmptyArrayString represents an empty JSON array in string type
var EmptyArrayString = []byte("[]")

// ErrorResponseText is the struct represents error JSON responses
type ErrorResponseText struct {
	Error string `json:"error"`
}

func error(t *testing.T, message string, args ...interface{}) {
	result := fmt.Sprintf(message, args...)
	t.Fatalf(result)
}

func generateQueryParameter(data map[string]string) string {
	buffer := &bytes.Buffer{}
	for key, value := range data {
		buffer.WriteString(key)
		buffer.WriteByte('=')
		buffer.WriteString(value)
		buffer.WriteByte('&')
	}
	queryParameter := buffer.String()
	return queryParameter[0 : len(queryParameter)-1]
}

// GenerateMultiResourceURL generates a resource url what represents multi resources based on the arguments
func GenerateMultiResourceURL(ts *httptest.Server, resource string, parameters map[string]string) string {
	var url string
	if 0 < len(parameters) {
		url = fmt.Sprintf("%s/%s/%s?%s", ts.URL, version, resource, generateQueryParameter(parameters))
	} else {
		url = fmt.Sprintf("%s/%s/%s", ts.URL, version, resource)
	}
	return url
}

// GenerateSingleResourceURL generates a resource url what represents a single resource based on the arguments
func GenerateSingleResourceURL(ts *httptest.Server, resource string, id string, parameters map[string]string) string {
	var url string
	if 0 < len(parameters) {
		url = fmt.Sprintf("%s/%s/%s/%s?%s", ts.URL, version, resource, id, generateQueryParameter(parameters))
	} else {
		url = fmt.Sprintf("%s/%s/%s/%s", ts.URL, version, resource, id)
	}
	return url
}

// SetupServer setups server for integration tests
func SetupServer() *httptest.Server {
	database := db.Connect()
	s := server.Setup(database)
	return httptest.NewServer(s)
}

// Execute send a HTTP request to the test server and receive a response
func Execute(t *testing.T, method string, resourceURL string, data interface{}) ([]byte, int) {
	byteArray, err := json.Marshal(data)

	if err != nil {
		error(t, "error Occured %v", err)
	}

	request, err := http.NewRequest(
		method,
		resourceURL,
		bytes.NewBuffer(byteArray),
	)
	request.Header.Set("Content-Type", "application/json")

	if err != nil {
		error(t, "error Occured %v", err)
	}

	client := &http.Client{Timeout: time.Duration(timeout * time.Second)}
	response, err := client.Do(request)

	if err != nil {
		error(t, "%s", err)
	}

	defer response.Body.Close()

	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		error(t, "%s", err)
	}
	return contents, response.StatusCode
}

// CheckResponseJSON checks given response JSON text
func CheckResponseJSON(t *testing.T, code int, expectedCode int, responseText []byte, expectedResponseText []byte, model interface{}) {
	if code != expectedCode {
		error(t, "code is expected as %d, but %d\nresponseTest: %s", expectedCode, code, string(responseText))
	}

	vs := reflect.ValueOf(model)
	for vs.Kind() == reflect.Ptr {
		vs = vs.Elem()
	}
	if !vs.IsValid() {
		error(t, "invalid model")

	}
	if !vs.CanInterface() {
		error(t, "model cannot interface()")
	}
	responseContainer := reflect.New(reflect.TypeOf(vs.Interface())).Interface()
	expectationContainer := reflect.New(reflect.TypeOf(vs.Interface())).Interface()

	err := json.Unmarshal(responseText, &responseContainer)
	if err != nil {
		error(t, "couldn't marshal the responseText: %s", string(responseText))
	}
	jsonByteArray, err := json.Marshal(responseContainer)
	if err != nil {
		error(t, "couldn't unmarshal the responseContainer: %v", responseContainer)
	}
	response := string(jsonByteArray)

	err = json.Unmarshal(expectedResponseText, &expectationContainer)
	if err != nil {
		error(t, "couldn't marshal the expectedResponseText: %s", string(expectedResponseText))
	}
	jsonByteArray, err = json.Marshal(expectationContainer)
	if err != nil {
		error(t, "couldn't unmarshal the expectationContainer: %v", expectationContainer)
	}
	expectation := string(jsonByteArray)

	if response != expectation {
		error(t, "response is expected as '%s', but '%s'", expectation, response)
	}
}

// CheckResponseText checks given response text
func CheckResponseText(t *testing.T, code int, expectedCode int, responseText []byte, expectedResponseText []byte) {
	if code != expectedCode {
		error(t, "code is expected as %d, but %dresponseTest: %s", expectedCode, code, string(responseText))
	}

	if string(responseText) != string(expectedResponseText) {
		error(t, "response is expected as '%s', but '%s'", expectedResponseText, responseText)
	}
}

// LoadExpectation loads expectation files to test automatically
func LoadExpectation(t *testing.T, testCaseName string) []byte {
	expectationFile := fmt.Sprintf("expectations/%s", testCaseName)
	data, err := ioutil.ReadFile(expectationFile)
	if err != nil {
		error(t, "couldn't load an expectation file %s", expectationFile)
	}
	return data
}
<commit_msg>Fix format error and type in CheckResponseJSON and CheckResponseText<commit_after>package integration

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/qb0C80aE/clay/db"
	"github.com/qb0C80aE/clay/server"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"reflect"
	"testing"
	"time"
)

const version = "v1"
const timeout = 15

// EmptyArrayString represents an empty JSON array in string type
var EmptyArrayString = []byte("[]")

// ErrorResponseText is the struct represents error JSON responses
type ErrorResponseText struct {
	Error string `json:"error"`
}

func error(t *testing.T, message string, args ...interface{}) {
	result := fmt.Sprintf(message, args...)
	t.Fatalf(result)
}

func generateQueryParameter(data map[string]string) string {
	buffer := &bytes.Buffer{}
	for key, value := range data {
		buffer.WriteString(key)
		buffer.WriteByte('=')
		buffer.WriteString(value)
		buffer.WriteByte('&')
	}
	queryParameter := buffer.String()
	return queryParameter[0 : len(queryParameter)-1]
}

// GenerateMultiResourceURL generates a resource url what represents multi resources based on the arguments
func GenerateMultiResourceURL(ts *httptest.Server, resource string, parameters map[string]string) string {
	var url string
	if 0 < len(parameters) {
		url = fmt.Sprintf("%s/%s/%s?%s", ts.URL, version, resource, generateQueryParameter(parameters))
	} else {
		url = fmt.Sprintf("%s/%s/%s", ts.URL, version, resource)
	}
	return url
}

// GenerateSingleResourceURL generates a resource url what represents a single resource based on the arguments
func GenerateSingleResourceURL(ts *httptest.Server, resource string, id string, parameters map[string]string) string {
	var url string
	if 0 < len(parameters) {
		url = fmt.Sprintf("%s/%s/%s/%s?%s", ts.URL, version, resource, id, generateQueryParameter(parameters))
	} else {
		url = fmt.Sprintf("%s/%s/%s/%s", ts.URL, version, resource, id)
	}
	return url
}

// SetupServer setups server for integration tests
func SetupServer() *httptest.Server {
	database := db.Connect()
	s := server.Setup(database)
	return httptest.NewServer(s)
}

// Execute send a HTTP request to the test server and receive a response
func Execute(t *testing.T, method string, resourceURL string, data interface{}) ([]byte, int) {
	byteArray, err := json.Marshal(data)

	if err != nil {
		error(t, "error Occured %v", err)
	}

	request, err := http.NewRequest(
		method,
		resourceURL,
		bytes.NewBuffer(byteArray),
	)
	request.Header.Set("Content-Type", "application/json")

	if err != nil {
		error(t, "error Occured %v", err)
	}

	client := &http.Client{Timeout: time.Duration(timeout * time.Second)}
	response, err := client.Do(request)

	if err != nil {
		error(t, "%s", err)
	}

	defer response.Body.Close()

	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		error(t, "%s", err)
	}
	return contents, response.StatusCode
}

// CheckResponseJSON checks given response JSON text
func CheckResponseJSON(t *testing.T, code int, expectedCode int, responseText []byte, expectedResponseText []byte, model interface{}) {
	if code != expectedCode {
		error(t, "code is expected as %d, but %d\nresponseText: %s", expectedCode, code, string(responseText))
	}

	vs := reflect.ValueOf(model)
	for vs.Kind() == reflect.Ptr {
		vs = vs.Elem()
	}
	if !vs.IsValid() {
		error(t, "invalid model")

	}
	if !vs.CanInterface() {
		error(t, "model cannot interface()")
	}
	responseContainer := reflect.New(reflect.TypeOf(vs.Interface())).Interface()
	expectationContainer := reflect.New(reflect.TypeOf(vs.Interface())).Interface()

	err := json.Unmarshal(responseText, &responseContainer)
	if err != nil {
		error(t, "couldn't marshal the responseText: %s", string(responseText))
	}
	jsonByteArray, err := json.Marshal(responseContainer)
	if err != nil {
		error(t, "couldn't unmarshal the responseContainer: %v", responseContainer)
	}
	response := string(jsonByteArray)

	err = json.Unmarshal(expectedResponseText, &expectationContainer)
	if err != nil {
		error(t, "couldn't marshal the expectedResponseText: %s", string(expectedResponseText))
	}
	jsonByteArray, err = json.Marshal(expectationContainer)
	if err != nil {
		error(t, "couldn't unmarshal the expectationContainer: %v", expectationContainer)
	}
	expectation := string(jsonByteArray)

	if response != expectation {
		error(t, "response is expected as '%s', but '%s'", expectation, response)
	}
}

// CheckResponseText checks given response text
func CheckResponseText(t *testing.T, code int, expectedCode int, responseText []byte, expectedResponseText []byte) {
	if code != expectedCode {
		error(t, "code is expected as %d, but %d\nresponseText: %s", expectedCode, code, string(responseText))
	}

	if string(responseText) != string(expectedResponseText) {
		error(t, "response is expected as '%s', but '%s'", expectedResponseText, responseText)
	}
}

// LoadExpectation loads expectation files to test automatically
func LoadExpectation(t *testing.T, testCaseName string) []byte {
	expectationFile := fmt.Sprintf("expectations/%s", testCaseName)
	data, err := ioutil.ReadFile(expectationFile)
	if err != nil {
		error(t, "couldn't load an expectation file %s", expectationFile)
	}
	return data
}
<|endoftext|>


<commit_before>// Copyright 2015 Hajime Hoshi
//
// 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.

// +build js

package audio

import (
	"github.com/gopherjs/gopherjs/js"
)

// Keep this so as not to be destroyed by GC.
var (
	nodes   = []*js.Object{}
	dummies = []*js.Object{} // Dummy source nodes for iOS.
	context *js.Object
)

const bufferSize = 1024

type audioProcessor struct {
	channel int
}

func toLR(data []byte) ([]int16, []int16) {
	l := make([]int16, len(data)/4)
	r := make([]int16, len(data)/4)
	for i := 0; i < len(data)/4; i++ {
		l[i] = int16(data[4*i]) | int16(data[4*i+1])<<8
		r[i] = int16(data[4*i+2]) | int16(data[4*i+3])<<8
	}
	return l, r
}

func (a *audioProcessor) Process(e *js.Object) {
	// Can't use goroutines here. Probably it may cause race conditions.
	b := e.Get("outputBuffer")
	l := b.Call("getChannelData", 0)
	r := b.Call("getChannelData", 1)
	inputL, inputR := toLR(loadChannelBuffer(a.channel, bufferSize*4))
	const max = 1 << 15
	for i := 0; i < bufferSize; i++ {
		// TODO: Use copyToChannel?
		if i < len(inputL) {
			l.SetIndex(i, float64(inputL[i])/max)
			r.SetIndex(i, float64(inputR[i])/max)
		} else {
			l.SetIndex(i, 0)
			r.SetIndex(i, 0)
		}
	}
}

func initialize() {
	// Do nothing in node.js.
	if js.Global.Get("require") != js.Undefined {
		return
	}

	class := js.Global.Get("AudioContext")
	if class == js.Undefined {
		class = js.Global.Get("webkitAudioContext")
	}
	if class == js.Undefined {
		return
	}
	context = class.New()
	// TODO: ScriptProcessorNode will be replaced with Audio WebWorker.
	// https://developer.mozilla.org/ja/docs/Web/API/ScriptProcessorNode
	for i := 0; i < MaxChannel; i++ {
		node := context.Call("createScriptProcessor", bufferSize, 0, 2)
		processor := &audioProcessor{i}
		node.Call("addEventListener", "audioprocess", processor.Process)
		nodes = append(nodes, node)

		dummy := context.Call("createBufferSource")
		dummies = append(dummies, dummy)
	}
	audioEnabled = true

	// Do nothing in node.js.
	if js.Global.Get("require") != js.Undefined {
		return
	}

	destination := context.Get("destination")
	for i, node := range nodes {
		dummy := dummies[i]
		dummy.Call("connect", node)
		node.Call("connect", destination)
	}
}
<commit_msg>audio: Refactoring<commit_after>// Copyright 2015 Hajime Hoshi
//
// 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.

// +build js

package audio

import (
	"github.com/gopherjs/gopherjs/js"
)

// Keep this so as not to be destroyed by GC.
var (
	nodes   = []*js.Object{}
	dummies = []*js.Object{} // Dummy source nodes for iOS.
	context *js.Object
)

const bufferSize = 1024

type audioProcessor struct {
	channel int
}

func toLR(data []byte) ([]int16, []int16) {
	l := make([]int16, len(data)/4)
	r := make([]int16, len(data)/4)
	for i := 0; i < len(data)/4; i++ {
		l[i] = int16(data[4*i]) | int16(data[4*i+1])<<8
		r[i] = int16(data[4*i+2]) | int16(data[4*i+3])<<8
	}
	return l, r
}

func (a *audioProcessor) Process(e *js.Object) {
	// Can't use goroutines here. Probably it may cause race conditions.
	b := e.Get("outputBuffer")
	l := b.Call("getChannelData", 0)
	r := b.Call("getChannelData", 1)
	inputL, inputR := toLR(loadChannelBuffer(a.channel, bufferSize*4))
	const max = 1 << 15
	for i := 0; i < bufferSize; i++ {
		// TODO: Use copyToChannel?
		if i < len(inputL) {
			l.SetIndex(i, float64(inputL[i])/max)
			r.SetIndex(i, float64(inputR[i])/max)
		} else {
			l.SetIndex(i, 0)
			r.SetIndex(i, 0)
		}
	}
}

func initialize() {
	// Do nothing in node.js.
	if js.Global.Get("require") != js.Undefined {
		return
	}

	class := js.Global.Get("AudioContext")
	if class == js.Undefined {
		class = js.Global.Get("webkitAudioContext")
	}
	if class == js.Undefined {
		return
	}
	context = class.New()
	// TODO: ScriptProcessorNode will be replaced with Audio WebWorker.
	// https://developer.mozilla.org/ja/docs/Web/API/ScriptProcessorNode
	for i := 0; i < MaxChannel; i++ {
		node := context.Call("createScriptProcessor", bufferSize, 0, 2)
		processor := &audioProcessor{i}
		node.Call("addEventListener", "audioprocess", processor.Process)
		nodes = append(nodes, node)

		dummy := context.Call("createBufferSource")
		dummies = append(dummies, dummy)
	}
	audioEnabled = true

	destination := context.Get("destination")
	for i, node := range nodes {
		dummy := dummies[i]
		dummy.Call("connect", node)
		node.Call("connect", destination)
	}
}
<|endoftext|>


<commit_before>package cmd

/* todo
- pagination
- style?
- api endpoints for:
  - t 8 --json

*/

const eventsUI = `
<!DOCTYPE html>

  <form autocomplete="off">
    <input type="text" name="after" placeholder="after" />
    <input type="text" name="before" placeholder="before" />
    <input type="text" name="event-user" placeholder="user" />
    <input type="text" name="action" placeholder="action" />
    <input type="text" name="object-type" placeholder="obj. type" />
    <input type="text" name="object-name" placeholder="obj. name" />
    <input type="text" name="task" placeholder="task" />
    <input type="text" name="deployment" placeholder="deployment" />
    <input type="text" name="instance" placeholder="instance" />
    <button type="submit">Go</button>
  </form>

    {id}

      {time}

      {user}

      {action}

      {object_type}

      {object_name}

      {task_id}

      {deployment}

      {instance}

    {context}
    {error}

No matching events

Error fetching events

`
<commit_msg>blur should not block other ui interaction<commit_after>package cmd

/* todo
- pagination
- style?
- api endpoints for:
  - t 8 --json

*/

const eventsUI = `
<!DOCTYPE html>

  <form autocomplete="off">
    <input type="text" name="after" placeholder="after" />
    <input type="text" name="before" placeholder="before" />
    <input type="text" name="event-user" placeholder="user" />
    <input type="text" name="action" placeholder="action" />
    <input type="text" name="object-type" placeholder="obj. type" />
    <input type="text" name="object-name" placeholder="obj. name" />
    <input type="text" name="task" placeholder="task" />
    <input type="text" name="deployment" placeholder="deployment" />
    <input type="text" name="instance" placeholder="instance" />
    <button type="submit">Go</button>
  </form>

    {id}

      {time}

      {user}

      {action}

      {object_type}

      {object_name}

      {task_id}

      {deployment}

      {instance}

    {context}
    {error}

No matching events

Error fetching events

`
<|endoftext|>


<commit_before>package store

import (
	"errors"
	"github.com/cznic/mathutil"
)

type ItemIterator interface {
	HasMore() bool
	Next() int32
	Peek() int32
	MaxSize() int
}

type FastItemIterator interface {
	ItemIterator
	SkipUntil(val int32)
}

type ToSliceIterator interface {
	ToSlice() []int32
}

func IteratorSkipUntil(iter ItemIterator, val int32) {
	if fast, ok := iter.(FastItemIterator); ok {
		fast.SkipUntil(val)
	} else {
		for iter.HasMore() && iter.Peek() < val {
			iter.Next()
		}
	}
}

func IteratorToList(result []int32, iter ItemIterator) []int32 {
	if it, ok := iter.(ToSliceIterator); ok {
		return it.ToSlice()
	}

	if result == nil {
		if maxSize := iter.MaxSize(); maxSize > 0 {
			result = make([]int32, 0, maxSize)
		}
	}

	for iter.HasMore() {
		result = append(result, iter.Next())
	}

	return result
}

type emptyIterator struct{}

var emptyIteratorInstance emptyIterator

func NewEmptyIterator() ItemIterator {
	return &emptyIteratorInstance
}

func (it *emptyIterator) HasMore() bool {
	return false
}

func (it *emptyIterator) Peek() int32 {
	panic(errors.New("Peek() called on empty iterator."))
}

func (it *emptyIterator) Next() int32 {
	panic(errors.New("Next() called on empty iterator."))
}

func (it *emptyIterator) MaxSize() int {
	return 0
}

type decompressingIterator struct {
	pos            int
	next, previous int32
	bytes          []byte
	more           bool
}

func NewDecompressingIterator(bytes []byte) ItemIterator {
	it := &decompressingIterator{bytes: bytes, more: true}
	it.advance()
	return it
}

func (it *decompressingIterator) HasMore() bool {
	return it.more
}

func (it *decompressingIterator) Peek() int32 {
	return it.next
}

func (it *decompressingIterator) Next() int32 {
	current := it.next

	if it.more {
		it.advance()
	}

	return current
}

func (it *decompressingIterator) MaxSize() int {
	return len(it.bytes)
}

func (it *decompressingIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.advance()
	}
}

func (it *decompressingIterator) advance() {
	if it.pos < len(it.bytes) {
		next, n := fastVarint32(it.bytes[it.pos:])
		if n <= 0 {
			panic(errors.New("Could not decode varint."))
		}

		it.pos += n
		it.next = next + it.previous
		it.previous = it.next
	} else {
		it.more = false
	}
}

// Varint decodes an int32 from buf and returns that value and the
// number of bytes read (> 0). If an error occurred, the value is 0
// and the number of bytes n is <= 0 with the following meaning:
//
//	n == 0: buf too small
//	n  < 0: value larger than 32 bits (overflow)
//              and -n is the number of bytes read
//
func fastVarint32(buf []byte) (int32, int) {
	var ux uint32
	var s uint
	for i, b := range buf {
		if b < 0x80 {
			if i > 4 || i == 4 && b > 1 {
				return 0, -(i + 1) // overflow
			}

			ux = ux | uint32(b) << s
			x := int32(ux >> 1)
			if ux & 1 != 0 {
				x = ^x
			}

			return x, i + 1
		}

		ux |= uint32(b & 0x7f) << s
		s += 7
	}

	return 0, 0
}

type negateIterator struct {
	ItemIterator
}

func NewNegateIterator(iter ItemIterator) ItemIterator {
	return &negateIterator{iter}
}

func (it *negateIterator) HasMore() bool {
	return it.ItemIterator.HasMore()
}

func (it *negateIterator) Peek() int32 {
	return -1 * it.ItemIterator.Peek()
}

func (it *negateIterator) Next() int32 {
	return -1 * it.ItemIterator.Next()
}

func (it *negateIterator) SkipUntil(val int32) {
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}

type limitIterator struct {
	totalCount int
	remaining  int
	iter       ItemIterator
}

func NewLimitIterator(limit int, iter ItemIterator) ItemIterator {
	return &limitIterator{totalCount: limit, remaining: limit, iter: iter}
}

func (it *limitIterator) HasMore() bool {
	return it.remaining > 0 && it.iter.HasMore()
}

func (it *limitIterator) Peek() int32 {
	return it.iter.Peek()
}

func (it *limitIterator) Next() int32 {
	it.remaining -= 1
	return it.iter.Next()
}

func (it *limitIterator) MaxSize() int {
	return it.totalCount
}

type andIterator struct {
	first, second ItemIterator
}

func NewAndIterator(iterators... ItemIterator) ItemIterator {
	switch {
	case len(iterators) == 0:
		return NewEmptyIterator()

	case len(iterators) == 1:
		return iterators[0]

	case len(iterators) == 2:
		return &andIterator{iterators[0], iterators[1]}

	default:
		return NewAndIterator(append([]ItemIterator{NewAndIterator(iterators[0], iterators[1])}, iterators[2:]...)...)
	}
}

func (it *andIterator) HasMore() bool {
	for it.first.HasMore() && it.second.HasMore() {
		a := it.first.Peek()
		b := it.second.Peek()

		if a == b {
			return true
		}

		if a < b {
			IteratorSkipUntil(it.first, b)
		} else {
			IteratorSkipUntil(it.second, a)
		}
	}

	return false
}

func (it *andIterator) Peek() int32 {
	return it.first.Peek()
}

func (it *andIterator) Next() int32 {
	return it.first.Next()
}

func (it *andIterator) MaxSize() int {
	return mathutil.Min(it.first.MaxSize(), it.second.MaxSize())
}

func (it *andIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}

type orIterator struct {
	first, second ItemIterator
}

func NewOrIterator(iterators... ItemIterator) ItemIterator {
	switch {
	case len(iterators) == 0:
		return NewEmptyIterator()

	case len(iterators) == 1:
		return iterators[0]

	case len(iterators) == 2:
		return &orIterator{iterators[0], iterators[1]}

	default:
		return NewOrIterator(append([]ItemIterator{NewOrIterator(iterators[0], iterators[1])}, iterators[2:]...)...)
	}
}

func (it *orIterator) HasMore() bool {
	return it.first.HasMore() || it.second.HasMore()
}

func (it *orIterator) Peek() int32 {
	if it.first.HasMore() && it.second.HasMore() {
		a := it.first.Peek()
		b := it.second.Peek()

		if a < b {
			return a
		} else {
			return b
		}
	}

	if it.first.HasMore() {
		return it.first.Peek()
	}

	if it.second.HasMore() {
		return it.second.Peek()
	}

	panic(errors.New("end of iterator"))
}

func (it *orIterator) Next() int32 {
	if it.first.HasMore() && it.second.HasMore() {
		a := it.first.Peek()
		b := it.second.Peek()

		if a == b {
			it.second.Next()
			return it.first.Next()
		}

		if a < b {
			return it.first.Next()
		} else {
			return it.second.Next()
		}
	}

	if it.first.HasMore() {
		return it.first.Next()
	}

	if it.second.HasMore() {
		return it.second.Next()
	}

	panic(errors.New("end of iterator"))
}

func (it *orIterator) MaxSize() int {
	return it.first.MaxSize() + it.second.MaxSize()
}

func (it *orIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}

type diffIterator struct {
	first, second ItemIterator
}

func NewDiffIterator(iterators... ItemIterator) ItemIterator {
	switch {
	case len(iterators) == 0:
		return NewEmptyIterator()

	case len(iterators) == 1:
		return iterators[0]

	case len(iterators) == 2:
		return &diffIterator{iterators[0], iterators[1]}

	default:
		return NewDiffIterator(iterators[0], NewAndIterator(iterators[1:]...))
	}
}

func (it *diffIterator) HasMore() bool {
	for it.first.HasMore() {
		if !it.second.HasMore() {
			return it.first.HasMore()
		}

		a := it.first.Peek()
		b := it.second.Peek()

		if a < b {
			return true
		} else if a == b {
			a = it.first.Next()
			IteratorSkipUntil(it.second, a)
		} else {
			IteratorSkipUntil(it.second, a)
		}
	}

	return false
}

func (it *diffIterator) Peek() int32 {
	return it.first.Peek()
}

func (it *diffIterator) Next() int32 {
	return it.first.Next()
}

func (it *diffIterator) MaxSize() int {
	return mathutil.Max(it.first.MaxSize(), it.second.MaxSize())
}

func (it *diffIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}
<commit_msg>Fix combining iterators<commit_after>package store

import (
	"errors"
	"github.com/cznic/mathutil"
)

type ItemIterator interface {
	HasMore() bool
	Next() int32
	Peek() int32
	MaxSize() int
}

type FastItemIterator interface {
	ItemIterator
	SkipUntil(val int32)
}

type ToSliceIterator interface {
	ToSlice() []int32
}

func IteratorSkipUntil(iter ItemIterator, val int32) {
	if fast, ok := iter.(FastItemIterator); ok {
		fast.SkipUntil(val)
	} else {
		for iter.HasMore() && iter.Peek() < val {
			iter.Next()
		}
	}
}

func IteratorToList(result []int32, iter ItemIterator) []int32 {
	if it, ok := iter.(ToSliceIterator); ok {
		return it.ToSlice()
	}

	if result == nil {
		if maxSize := iter.MaxSize(); maxSize > 0 {
			result = make([]int32, 0, maxSize)
		}
	}

	for iter.HasMore() {
		result = append(result, iter.Next())
	}

	return result
}

type emptyIterator struct{}

var emptyIteratorInstance emptyIterator

func NewEmptyIterator() ItemIterator {
	return &emptyIteratorInstance
}

func (it *emptyIterator) HasMore() bool {
	return false
}

func (it *emptyIterator) Peek() int32 {
	panic(errors.New("Peek() called on empty iterator."))
}

func (it *emptyIterator) Next() int32 {
	panic(errors.New("Next() called on empty iterator."))
}

func (it *emptyIterator) MaxSize() int {
	return 0
}

type decompressingIterator struct {
	pos            int
	next, previous int32
	bytes          []byte
	more           bool
}

func NewDecompressingIterator(bytes []byte) ItemIterator {
	it := &decompressingIterator{bytes: bytes, more: true}
	it.advance()
	return it
}

func (it *decompressingIterator) HasMore() bool {
	return it.more
}

func (it *decompressingIterator) Peek() int32 {
	return it.next
}

func (it *decompressingIterator) Next() int32 {
	current := it.next

	if it.more {
		it.advance()
	}

	return current
}

func (it *decompressingIterator) MaxSize() int {
	return len(it.bytes)
}

func (it *decompressingIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.advance()
	}
}

func (it *decompressingIterator) advance() {
	if it.pos < len(it.bytes) {
		next, n := fastVarint32(it.bytes[it.pos:])
		if n <= 0 {
			panic(errors.New("Could not decode varint."))
		}

		it.pos += n
		it.next = next + it.previous
		it.previous = it.next
	} else {
		it.more = false
	}
}

// Varint decodes an int32 from buf and returns that value and the
// number of bytes read (> 0). If an error occurred, the value is 0
// and the number of bytes n is <= 0 with the following meaning:
//
//	n == 0: buf too small
//	n  < 0: value larger than 32 bits (overflow)
//              and -n is the number of bytes read
//
func fastVarint32(buf []byte) (int32, int) {
	var ux uint32
	var s uint
	for i, b := range buf {
		if b < 0x80 {
			if i > 4 || i == 4 && b > 1 {
				return 0, -(i + 1) // overflow
			}

			ux = ux | uint32(b) << s
			x := int32(ux >> 1)
			if ux & 1 != 0 {
				x = ^x
			}

			return x, i + 1
		}

		ux |= uint32(b & 0x7f) << s
		s += 7
	}

	return 0, 0
}

type negateIterator struct {
	ItemIterator
}

func NewNegateIterator(iter ItemIterator) ItemIterator {
	return &negateIterator{iter}
}

func (it *negateIterator) HasMore() bool {
	return it.ItemIterator.HasMore()
}

func (it *negateIterator) Peek() int32 {
	return -1 * it.ItemIterator.Peek()
}

func (it *negateIterator) Next() int32 {
	return -1 * it.ItemIterator.Next()
}

func (it *negateIterator) SkipUntil(val int32) {
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}

type limitIterator struct {
	totalCount int
	remaining  int
	iter       ItemIterator
}

func NewLimitIterator(limit int, iter ItemIterator) ItemIterator {
	return &limitIterator{totalCount: limit, remaining: limit, iter: iter}
}

func (it *limitIterator) HasMore() bool {
	return it.remaining > 0 && it.iter.HasMore()
}

func (it *limitIterator) Peek() int32 {
	return it.iter.Peek()
}

func (it *limitIterator) Next() int32 {
	it.remaining -= 1
	return it.iter.Next()
}

func (it *limitIterator) MaxSize() int {
	return it.totalCount
}

type andIterator struct {
	first, second ItemIterator
}

func NewAndIterator(iterators... ItemIterator) ItemIterator {
	switch {
	case len(iterators) == 0:
		return NewEmptyIterator()

	case len(iterators) == 1:
		return iterators[0]

	case len(iterators) == 2:
		return &andIterator{iterators[0], iterators[1]}

	default:
		return NewAndIterator(append([]ItemIterator{NewAndIterator(iterators[0], iterators[1])}, iterators[2:]...)...)
	}
}

func (it *andIterator) HasMore() bool {
	for it.first.HasMore() && it.second.HasMore() {
		a := it.first.Peek()
		b := it.second.Peek()

		if a == b {
			return true
		}

		if a < b {
			IteratorSkipUntil(it.first, b)
		} else {
			IteratorSkipUntil(it.second, a)
		}
	}

	return false
}

func (it *andIterator) Peek() int32 {
	return it.first.Peek()
}

func (it *andIterator) Next() int32 {
	return it.first.Next()
}

func (it *andIterator) MaxSize() int {
	return mathutil.Min(it.first.MaxSize(), it.second.MaxSize())
}

func (it *andIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}

type orIterator struct {
	first, second ItemIterator
}

func NewOrIterator(iterators... ItemIterator) ItemIterator {
	switch {
	case len(iterators) == 0:
		return NewEmptyIterator()

	case len(iterators) == 1:
		return iterators[0]

	case len(iterators) == 2:
		return &orIterator{iterators[0], iterators[1]}

	default:
		return NewOrIterator(append([]ItemIterator{NewOrIterator(iterators[0], iterators[1])}, iterators[2:]...)...)
	}
}

func (it *orIterator) HasMore() bool {
	return it.first.HasMore() || it.second.HasMore()
}

func (it *orIterator) Peek() int32 {
	if it.first.HasMore() && it.second.HasMore() {
		a := it.first.Peek()
		b := it.second.Peek()

		if a < b {
			return a
		} else {
			return b
		}
	}

	if it.first.HasMore() {
		return it.first.Peek()
	}

	if it.second.HasMore() {
		return it.second.Peek()
	}

	panic(errors.New("end of iterator"))
}

func (it *orIterator) Next() int32 {
	if it.first.HasMore() && it.second.HasMore() {
		a := it.first.Peek()
		b := it.second.Peek()

		if a == b {
			it.second.Next()
			return it.first.Next()
		}

		if a < b {
			return it.first.Next()
		} else {
			return it.second.Next()
		}
	}

	if it.first.HasMore() {
		return it.first.Next()
	}

	if it.second.HasMore() {
		return it.second.Next()
	}

	panic(errors.New("end of iterator"))
}

func (it *orIterator) MaxSize() int {
	return it.first.MaxSize() + it.second.MaxSize()
}

func (it *orIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}

type diffIterator struct {
	first, second ItemIterator
}

func NewDiffIterator(iterators... ItemIterator) ItemIterator {
	switch {
	case len(iterators) == 0:
		return NewEmptyIterator()

	case len(iterators) == 1:
		return iterators[0]

	case len(iterators) == 2:
		return &diffIterator{iterators[0], iterators[1]}

	default:
		return NewDiffIterator(iterators[0], NewOrIterator(iterators[1:]...))
	}
}

func (it *diffIterator) HasMore() bool {
	for it.first.HasMore() {
		if !it.second.HasMore() {
			return it.first.HasMore()
		}

		a := it.first.Peek()
		b := it.second.Peek()

		if a < b {
			return true
		} else if a == b {
			a = it.first.Next()
			IteratorSkipUntil(it.second, a)
		} else {
			IteratorSkipUntil(it.second, a)
		}
	}

	return false
}

func (it *diffIterator) Peek() int32 {
	return it.first.Peek()
}

func (it *diffIterator) Next() int32 {
	return it.first.Next()
}

func (it *diffIterator) MaxSize() int {
	return mathutil.Max(it.first.MaxSize(), it.second.MaxSize())
}

func (it *diffIterator) SkipUntil(val int32) {
	// allow inlining of the methods.
	for it.HasMore() && it.Peek() < val {
		it.Next()
	}
}
<|endoftext|>


<commit_before>package cluster

import (
	"context"
	"fmt"
	"reflect"

	"github.com/rancher/kontainer-engine/service"
	"github.com/rancher/kontainer-engine/types"
	"github.com/rancher/rancher/pkg/controllers/management/clusterprovisioner"
	"github.com/rancher/rke/cloudprovider/aws"
	"github.com/rancher/rke/cloudprovider/azure"
	"github.com/rancher/types/apis/core/v1"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/rancher/types/config"
	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
)

const (
	GoogleCloudLoadBalancer = "GCLB"
	ElasticLoadBalancer     = "ELB"
	AzureL4LB               = "Azure L4 LB"
	NginxIngressProvider    = "Nginx"
	DefaultNodePortRange    = "30000-32767"
)

type controller struct {
	clusterClient         v3.ClusterInterface
	clusterLister         v3.ClusterLister
	nodeLister            v3.NodeLister
	kontainerDriverLister v3.KontainerDriverLister
	namespaces            v1.NamespaceInterface
	coreV1                v1.Interface
}

func Register(ctx context.Context, management *config.ManagementContext) {
	c := controller{
		clusterClient:         management.Management.Clusters(""),
		clusterLister:         management.Management.Clusters("").Controller().Lister(),
		nodeLister:            management.Management.Nodes("").Controller().Lister(),
		kontainerDriverLister: management.Management.KontainerDrivers("").Controller().Lister(),
		namespaces:            management.Core.Namespaces(""),
		coreV1:                management.Core,
	}

	c.clusterClient.AddHandler(ctx, "clusterCreateUpdate", c.capsSync)
}

func (c *controller) capsSync(key string, cluster *v3.Cluster) (runtime.Object, error) {
	var err error
	if cluster == nil || cluster.DeletionTimestamp != nil {
		return nil, nil
	}

	if cluster.Spec.ImportedConfig != nil {
		return nil, nil
	}
	capabilities := v3.Capabilities{}
	capabilities.NodePortRange = DefaultNodePortRange

	if cluster.Spec.RancherKubernetesEngineConfig != nil {
		if capabilities, err = c.RKECapabilities(capabilities, *cluster.Spec.RancherKubernetesEngineConfig, cluster.Name); err != nil {
			return nil, err
		}
	} else if cluster.Spec.GenericEngineConfig != nil {
		driverName, ok := (*cluster.Spec.GenericEngineConfig)["driverName"].(string)
		if !ok {
			logrus.Warnf("cluster %v had generic engine config but no driver name, k8s capabilities will "+
				"not be populated correctly", key)
			return nil, nil
		}

		kontainerDriver, err := c.kontainerDriverLister.Get("", driverName)
		if err != nil {
			return nil, fmt.Errorf("error getting kontainer driver: %v", err)
		}

		driver := service.NewEngineService(
			clusterprovisioner.NewPersistentStore(c.namespaces, c.coreV1),
		)
		k8sCapabilities, err := driver.GetK8sCapabilities(context.Background(), kontainerDriver.Name, kontainerDriver,
			cluster.Spec)
		if err != nil {
			return nil, fmt.Errorf("error getting k8s capabilities: %v", err)
		}

		capabilities = toCapabilities(k8sCapabilities)
	} else {
		return nil, nil
	}

	if !reflect.DeepEqual(capabilities, cluster.Status.Capabilities) {
		toUpdateCluster := cluster.DeepCopy()
		toUpdateCluster.Status.Capabilities = capabilities
		if _, err := c.clusterClient.Update(toUpdateCluster); err != nil {
			return nil, err
		}
	}

	return nil, nil
}

func (c *controller) RKECapabilities(capabilities v3.Capabilities, rkeConfig v3.RancherKubernetesEngineConfig, clusterName string) (v3.Capabilities, error) {
	switch rkeConfig.CloudProvider.Name {
	case aws.AWSCloudProviderName:
		capabilities.LoadBalancerCapabilities = c.L4Capability(true, ElasticLoadBalancer, []string{"TCP"}, true)
	case azure.AzureCloudProviderName:
		capabilities.LoadBalancerCapabilities = c.L4Capability(true, AzureL4LB, []string{"TCP", "UDP"}, true)
	}
	// only if not custom, non custom clusters have nodepools set
	nodes, err := c.nodeLister.List(clusterName, labels.Everything())
	if err != nil {
		return capabilities, err
	}

	if len(nodes) > 0 {
		if nodes[0].Spec.NodePoolName != "" {
			capabilities.NodePoolScalingSupported = true
		}
	}

	ingressController := c.IngressCapability(true, NginxIngressProvider, false)
	capabilities.IngressCapabilities = []v3.IngressCapabilities{ingressController}
	if rkeConfig.Services.KubeAPI.ExtraArgs["service-node-port-range"] != "" {
		capabilities.NodePortRange = rkeConfig.Services.KubeAPI.ExtraArgs["service-node-port-range"]
	}

	return capabilities, nil
}

func (c *controller) L4Capability(enabled bool, providerName string, protocols []string, healthCheck bool) v3.LoadBalancerCapabilities {
	l4lb := v3.LoadBalancerCapabilities{
		Enabled:              enabled,
		Provider:             providerName,
		ProtocolsSupported:   protocols,
		HealthCheckSupported: healthCheck,
	}
	return l4lb
}

func (c *controller) IngressCapability(httpLBEnabled bool, providerName string, customDefaultBackend bool) v3.IngressCapabilities {
	ing := v3.IngressCapabilities{
		IngressProvider:      providerName,
		CustomDefaultBackend: customDefaultBackend,
	}
	return ing
}

func toCapabilities(k8sCapabilities *types.K8SCapabilities) v3.Capabilities {
	var controllers []v3.IngressCapabilities

	for _, controller := range k8sCapabilities.IngressControllers {
		controllers = append(controllers, v3.IngressCapabilities{
			CustomDefaultBackend: controller.CustomDefaultBackend,
			IngressProvider:      controller.IngressProvider,
		})
	}

	return v3.Capabilities{
		IngressCapabilities: controllers,
		LoadBalancerCapabilities: v3.LoadBalancerCapabilities{
			Enabled:              k8sCapabilities.L4LoadBalancer.Enabled,
			HealthCheckSupported: k8sCapabilities.L4LoadBalancer.HealthCheckSupported,
			ProtocolsSupported:   k8sCapabilities.L4LoadBalancer.ProtocolsSupported,
			Provider:             k8sCapabilities.L4LoadBalancer.Provider,
		},
		NodePoolScalingSupported: k8sCapabilities.NodePoolScalingSupported,
		NodePortRange:            k8sCapabilities.NodePortRange,
	}
}
<commit_msg>Update load balancer capabilities<commit_after>package cluster

import (
	"context"
	"fmt"
	"reflect"

	"github.com/rancher/kontainer-engine/service"
	"github.com/rancher/kontainer-engine/types"
	"github.com/rancher/rancher/pkg/controllers/management/clusterprovisioner"
	"github.com/rancher/rke/cloudprovider/aws"
	"github.com/rancher/rke/cloudprovider/azure"
	"github.com/rancher/types/apis/core/v1"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/rancher/types/config"
	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
)

const (
	GoogleCloudLoadBalancer = "GCLB"
	ElasticLoadBalancer     = "ELB"
	AzureL4LB               = "Azure L4 LB"
	NginxIngressProvider    = "Nginx"
	DefaultNodePortRange    = "30000-32767"
)

type controller struct {
	clusterClient         v3.ClusterInterface
	clusterLister         v3.ClusterLister
	nodeLister            v3.NodeLister
	kontainerDriverLister v3.KontainerDriverLister
	namespaces            v1.NamespaceInterface
	coreV1                v1.Interface
}

func Register(ctx context.Context, management *config.ManagementContext) {
	c := controller{
		clusterClient:         management.Management.Clusters(""),
		clusterLister:         management.Management.Clusters("").Controller().Lister(),
		nodeLister:            management.Management.Nodes("").Controller().Lister(),
		kontainerDriverLister: management.Management.KontainerDrivers("").Controller().Lister(),
		namespaces:            management.Core.Namespaces(""),
		coreV1:                management.Core,
	}

	c.clusterClient.AddHandler(ctx, "clusterCreateUpdate", c.capsSync)
}

func (c *controller) capsSync(key string, cluster *v3.Cluster) (runtime.Object, error) {
	var err error
	if cluster == nil || cluster.DeletionTimestamp != nil {
		return nil, nil
	}

	if cluster.Spec.ImportedConfig != nil {
		return nil, nil
	}
	capabilities := v3.Capabilities{}
	capabilities.NodePortRange = DefaultNodePortRange

	if cluster.Spec.RancherKubernetesEngineConfig != nil {
		if capabilities, err = c.RKECapabilities(capabilities, *cluster.Spec.RancherKubernetesEngineConfig, cluster.Name); err != nil {
			return nil, err
		}
	} else if cluster.Spec.GenericEngineConfig != nil {
		driverName, ok := (*cluster.Spec.GenericEngineConfig)["driverName"].(string)
		if !ok {
			logrus.Warnf("cluster %v had generic engine config but no driver name, k8s capabilities will "+
				"not be populated correctly", key)
			return nil, nil
		}

		kontainerDriver, err := c.kontainerDriverLister.Get("", driverName)
		if err != nil {
			return nil, fmt.Errorf("error getting kontainer driver: %v", err)
		}

		driver := service.NewEngineService(
			clusterprovisioner.NewPersistentStore(c.namespaces, c.coreV1),
		)
		k8sCapabilities, err := driver.GetK8sCapabilities(context.Background(), kontainerDriver.Name, kontainerDriver,
			cluster.Spec)
		if err != nil {
			return nil, fmt.Errorf("error getting k8s capabilities: %v", err)
		}

		capabilities = toCapabilities(k8sCapabilities)
	} else {
		return nil, nil
	}

	if !reflect.DeepEqual(capabilities, cluster.Status.Capabilities) {
		toUpdateCluster := cluster.DeepCopy()
		toUpdateCluster.Status.Capabilities = capabilities
		if _, err := c.clusterClient.Update(toUpdateCluster); err != nil {
			return nil, err
		}
	}

	return nil, nil
}

func (c *controller) RKECapabilities(capabilities v3.Capabilities, rkeConfig v3.RancherKubernetesEngineConfig, clusterName string) (v3.Capabilities, error) {
	switch rkeConfig.CloudProvider.Name {
	case aws.AWSCloudProviderName:
		capabilities.LoadBalancerCapabilities = c.L4Capability(true, ElasticLoadBalancer, []string{"TCP"}, true)
	case azure.AzureCloudProviderName:
		capabilities.LoadBalancerCapabilities = c.L4Capability(true, AzureL4LB, []string{"TCP", "UDP"}, true)
	default:
		capabilities.LoadBalancerCapabilities = c.L4Capability(false, "", []string{}, false)
	}
	// only if not custom, non custom clusters have nodepools set
	nodes, err := c.nodeLister.List(clusterName, labels.Everything())
	if err != nil {
		return capabilities, err
	}

	if len(nodes) > 0 {
		if nodes[0].Spec.NodePoolName != "" {
			capabilities.NodePoolScalingSupported = true
		}
	}

	ingressController := c.IngressCapability(true, NginxIngressProvider, false)
	capabilities.IngressCapabilities = []v3.IngressCapabilities{ingressController}
	if rkeConfig.Services.KubeAPI.ExtraArgs["service-node-port-range"] != "" {
		capabilities.NodePortRange = rkeConfig.Services.KubeAPI.ExtraArgs["service-node-port-range"]
	}

	return capabilities, nil
}

func (c *controller) L4Capability(enabled bool, providerName string, protocols []string, healthCheck bool) v3.LoadBalancerCapabilities {
	l4lb := v3.LoadBalancerCapabilities{
		Enabled:              &enabled,
		Provider:             providerName,
		ProtocolsSupported:   protocols,
		HealthCheckSupported: healthCheck,
	}
	return l4lb
}

func (c *controller) IngressCapability(httpLBEnabled bool, providerName string, customDefaultBackend bool) v3.IngressCapabilities {
	ing := v3.IngressCapabilities{
		IngressProvider:      providerName,
		CustomDefaultBackend: customDefaultBackend,
	}
	return ing
}

func toCapabilities(k8sCapabilities *types.K8SCapabilities) v3.Capabilities {
	var controllers []v3.IngressCapabilities

	for _, controller := range k8sCapabilities.IngressControllers {
		controllers = append(controllers, v3.IngressCapabilities{
			CustomDefaultBackend: controller.CustomDefaultBackend,
			IngressProvider:      controller.IngressProvider,
		})
	}

	return v3.Capabilities{
		IngressCapabilities: controllers,
		LoadBalancerCapabilities: v3.LoadBalancerCapabilities{
			Enabled:              &k8sCapabilities.L4LoadBalancer.Enabled,
			HealthCheckSupported: k8sCapabilities.L4LoadBalancer.HealthCheckSupported,
			ProtocolsSupported:   k8sCapabilities.L4LoadBalancer.ProtocolsSupported,
			Provider:             k8sCapabilities.L4LoadBalancer.Provider,
		},
		NodePoolScalingSupported: k8sCapabilities.NodePoolScalingSupported,
		NodePortRange:            k8sCapabilities.NodePortRange,
	}
}
<|endoftext|>


<commit_before>// Copyright 2014 The oauth2 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package clientcredentials implements the OAuth2.0 "client credentials" token flow,
// also known as the "two-legged OAuth 2.0".
//
// This should be used when the client is acting on its own behalf or when the client
// is the resource owner. It may also be used when requesting access to protected
// resources based on an authorization previously arranged with the authorization
// server.
//
// See http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.4
package clientcredentials // import "golang.org/x/oauth2/clientcredentials"

import (
	"net/http"
	"net/url"
	"strings"

	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/internal"
)

// tokenFromInternal maps an *internal.Token struct into
// an *oauth2.Token struct.
func tokenFromInternal(t *internal.Token) *oauth2.Token {
	if t == nil {
		return nil
	}
	tk := &oauth2.Token{
		AccessToken:  t.AccessToken,
		TokenType:    t.TokenType,
		RefreshToken: t.RefreshToken,
		Expiry:       t.Expiry,
	}
	return tk.WithExtra(t.Raw)
}

// retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
// This token is then mapped from *internal.Token into an *oauth2.Token which is
// returned along with an error.
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*oauth2.Token, error) {
	tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.TokenURL, v)
	if err != nil {
		return nil, err
	}
	return tokenFromInternal(tk), nil
}

// Client Credentials Config describes a 2-legged OAuth2 flow, with both the
// client application information and the server's endpoint URLs.
type Config struct {
	// ClientID is the application's ID.
	ClientID string

	// ClientSecret is the application's secret.
	ClientSecret string

	// TokenURL is the resource server's token endpoint
	// URL. This is a constant specific to each server.
	TokenURL string

	// Scope specifies optional requested permissions.
	Scopes []string
}

// Token uses client credentials to retreive a token.
// The HTTP client to use is derived from the context.
// If nil, http.DefaultClient is used.
func (c *Config) Token(ctx context.Context) (*oauth2.Token, error) {
	return retrieveToken(ctx, c, url.Values{
		"grant_type": {"client_credentials"},
		"scope":      internal.CondVal(strings.Join(c.Scopes, " ")),
	})
}

// Client returns an HTTP client using the provided token.
// The token will auto-refresh as necessary. The underlying
// HTTP transport will be obtained using the provided context.
// The returned client and its Transport should not be modified.
func (c *Config) Client(ctx context.Context) *http.Client {
	return oauth2.NewClient(ctx, c.TokenSource(ctx))
}

// TokenSource returns a TokenSource that returns t until t expires,
// automatically refreshing it as necessary using the provided context and the
// client ID and client secret.
//
// Most users will use Config.Client instead.
func (c *Config) TokenSource(ctx context.Context) oauth2.TokenSource {
	source := &tokenSource{
		ctx:  ctx,
		conf: c,
	}
	return oauth2.ReuseTokenSource(nil, source)
}

type tokenSource struct {
	ctx  context.Context
	conf *Config
}

// Token refreshes the token by using a new client credentials request.
// tokens received this way do not include a refresh token
func (c *tokenSource) Token() (*oauth2.Token, error) {
	return retrieveToken(c.ctx, c.conf, url.Values{
		"grant_type": {"client_credentials"},
		"scope":      internal.CondVal(strings.Join(c.conf.Scopes, " ")),
	})
}
<commit_msg>small typo fix in clientcredentials.go<commit_after>// Copyright 2014 The oauth2 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package clientcredentials implements the OAuth2.0 "client credentials" token flow,
// also known as the "two-legged OAuth 2.0".
//
// This should be used when the client is acting on its own behalf or when the client
// is the resource owner. It may also be used when requesting access to protected
// resources based on an authorization previously arranged with the authorization
// server.
//
// See http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.4
package clientcredentials // import "golang.org/x/oauth2/clientcredentials"

import (
	"net/http"
	"net/url"
	"strings"

	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/internal"
)

// tokenFromInternal maps an *internal.Token struct into
// an *oauth2.Token struct.
func tokenFromInternal(t *internal.Token) *oauth2.Token {
	if t == nil {
		return nil
	}
	tk := &oauth2.Token{
		AccessToken:  t.AccessToken,
		TokenType:    t.TokenType,
		RefreshToken: t.RefreshToken,
		Expiry:       t.Expiry,
	}
	return tk.WithExtra(t.Raw)
}

// retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
// This token is then mapped from *internal.Token into an *oauth2.Token which is
// returned along with an error.
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*oauth2.Token, error) {
	tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.TokenURL, v)
	if err != nil {
		return nil, err
	}
	return tokenFromInternal(tk), nil
}

// Client Credentials Config describes a 2-legged OAuth2 flow, with both the
// client application information and the server's endpoint URLs.
type Config struct {
	// ClientID is the application's ID.
	ClientID string

	// ClientSecret is the application's secret.
	ClientSecret string

	// TokenURL is the resource server's token endpoint
	// URL. This is a constant specific to each server.
	TokenURL string

	// Scope specifies optional requested permissions.
	Scopes []string
}

// Token uses client credentials to retrieve a token.
// The HTTP client to use is derived from the context.
// If nil, http.DefaultClient is used.
func (c *Config) Token(ctx context.Context) (*oauth2.Token, error) {
	return retrieveToken(ctx, c, url.Values{
		"grant_type": {"client_credentials"},
		"scope":      internal.CondVal(strings.Join(c.Scopes, " ")),
	})
}

// Client returns an HTTP client using the provided token.
// The token will auto-refresh as necessary. The underlying
// HTTP transport will be obtained using the provided context.
// The returned client and its Transport should not be modified.
func (c *Config) Client(ctx context.Context) *http.Client {
	return oauth2.NewClient(ctx, c.TokenSource(ctx))
}

// TokenSource returns a TokenSource that returns t until t expires,
// automatically refreshing it as necessary using the provided context and the
// client ID and client secret.
//
// Most users will use Config.Client instead.
func (c *Config) TokenSource(ctx context.Context) oauth2.TokenSource {
	source := &tokenSource{
		ctx:  ctx,
		conf: c,
	}
	return oauth2.ReuseTokenSource(nil, source)
}

type tokenSource struct {
	ctx  context.Context
	conf *Config
}

// Token refreshes the token by using a new client credentials request.
// tokens received this way do not include a refresh token
func (c *tokenSource) Token() (*oauth2.Token, error) {
	return retrieveToken(c.ctx, c.conf, url.Values{
		"grant_type": {"client_credentials"},
		"scope":      internal.CondVal(strings.Join(c.conf.Scopes, " ")),
	})
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/zarevucky/spl"
	"os"
	"os/exec"
	pathpkg "path"
	"strings"
	"log"
)

type Application struct {
	Packages map[string]*Package
}

func NewApplication() *Application {
	return &Application{make(map[string]*Package)}
}

// This method loads the builtin "environment" package into the program context
// under a given name. The package contains very basic methods for interfacing
// with operating system (files, output, program args, etc...).
// It only provides the bare minimum of methods necessary for implementing
// a full-featured self-hosting compiler.
func (app *Application) LoadBuiltinEnv(name string) {
	envpkg := NewPackage("<builtin_env>")
	app.Packages[name] = envpkg

	// Takes arbitrary number of arguments, and prints each of them to
	// the standard output, with nothing in between or at the end.
	// It should only be used to print strings and integers if
	// well-defined result is required, but other values may be printed
	// for human debugging.
	envpkg.Methods["print"] = NewBuiltinMethod("env.print", nil)

	// Accepts a filename as the only argument, and returns an
	// array of bytes with the contents of the file. Can fail.
	// FIXME: Document failure modes.
	envpkg.Methods["read_file"] = NewBuiltinMethod("env.read_file", []Type{NewPointerType(NewArrayType(INT_TYPE, -1))})

	// This method just returns the array of command-line arguments
	// for the running program.
	envpkg.Methods["args"] = NewBuiltinMethod("env.args", []Type{NewPointerType(NewArrayType(STRING_TYPE, -1))})

	/*iopkg.Globals["File"] = NewBuiltinType("io.File")
	iopkg.Globals["Open"] = NewBuiltinMethod("io.Open")
	iopkg.Globals["*File.Read"] = NewBuiltinMethod("io.*File.Read")
	iopkg.Globals["*File.Write"] = NewBuiltinMethod("io.*File.Write")
	iopkg.Globals["Dir"] = NewBuiltinType("io.Dir")
	iopkg.Glabals["OpenDir"] = NewBuiltinMethod("io.OpenDir")
	iopkg.Globals["*Dir.Read"] = NewBuiltinMethod("io.*Dir.Read")
	*/
}

func (app *Application) LoadPackage(name string, path string) {
	pkg := NewPackage(path)

	if path[0] == '/' {
		// TODO: PkgDir
		//	path = pathpkg.Join(app.PkgDir, path)
	}

	dir, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	fi, err := dir.Readdir(0)
	if err != nil {
		panic(err)
	}

	for i := range fi {
		if fi[i].IsDir() {
			continue
		}

		if !strings.HasSuffix(fi[i].Name(), ".zs") {
			continue
		}

		filename := pathpkg.Join(path, fi[i].Name())

		var err error

		parser := os.Getenv("ZSPARSER")
		if parser == "" {
			parser = "zsparser"
		}

		cmd := exec.Command(parser)
		cmd.Stderr = os.Stderr
		cmd.Stdin, err = os.Open(filename)
		if err != nil {
			log.Fatalf("Cannot open file %s: %s", filename, err)
		}

		// TODO: Get rid of panics.

		stdout, err := cmd.StdoutPipe()
		if err != nil {
			panic(err)
		}

		err = cmd.Start()
		if err != nil {
			panic(err)
		}

		p := spl.NewSeqParser(stdout)

		file, err1 := ParseASTFile(p)

		err = cmd.Wait()
		if err != nil {
			log.Fatalf("Parser failed on file %s: %s", filename, err)
		}

		if err1 != nil {
			log.Fatalf("Unmarshaller failed on file %s: %s", filename, err)
		}

		pkg.LoadFile(app, file._globals)
	}

	for _, t := range pkg.Types {
		t.Resolve()
	}

	for _, m := range pkg.MethodList {
		m.Resolve()
	}

	for _, m := range pkg.MethodList {
		NewTranscribeContext(m).Transcribe()
	}

	app.Packages[name] = pkg
}
<commit_msg>go fmt<commit_after>package main

import (
	"github.com/zarevucky/spl"
	"log"
	"os"
	"os/exec"
	pathpkg "path"
	"strings"
)

type Application struct {
	Packages map[string]*Package
}

func NewApplication() *Application {
	return &Application{make(map[string]*Package)}
}

// This method loads the builtin "environment" package into the program context
// under a given name. The package contains very basic methods for interfacing
// with operating system (files, output, program args, etc...).
// It only provides the bare minimum of methods necessary for implementing
// a full-featured self-hosting compiler.
func (app *Application) LoadBuiltinEnv(name string) {
	envpkg := NewPackage("<builtin_env>")
	app.Packages[name] = envpkg

	// Takes arbitrary number of arguments, and prints each of them to
	// the standard output, with nothing in between or at the end.
	// It should only be used to print strings and integers if
	// well-defined result is required, but other values may be printed
	// for human debugging.
	envpkg.Methods["print"] = NewBuiltinMethod("env.print", nil)

	// Accepts a filename as the only argument, and returns an
	// array of bytes with the contents of the file. Can fail.
	// FIXME: Document failure modes.
	envpkg.Methods["read_file"] = NewBuiltinMethod("env.read_file", []Type{NewPointerType(NewArrayType(INT_TYPE, -1))})

	// This method just returns the array of command-line arguments
	// for the running program.
	envpkg.Methods["args"] = NewBuiltinMethod("env.args", []Type{NewPointerType(NewArrayType(STRING_TYPE, -1))})

	/*iopkg.Globals["File"] = NewBuiltinType("io.File")
	iopkg.Globals["Open"] = NewBuiltinMethod("io.Open")
	iopkg.Globals["*File.Read"] = NewBuiltinMethod("io.*File.Read")
	iopkg.Globals["*File.Write"] = NewBuiltinMethod("io.*File.Write")
	iopkg.Globals["Dir"] = NewBuiltinType("io.Dir")
	iopkg.Glabals["OpenDir"] = NewBuiltinMethod("io.OpenDir")
	iopkg.Globals["*Dir.Read"] = NewBuiltinMethod("io.*Dir.Read")
	*/
}

func (app *Application) LoadPackage(name string, path string) {
	pkg := NewPackage(path)

	if path[0] == '/' {
		// TODO: PkgDir
		//	path = pathpkg.Join(app.PkgDir, path)
	}

	dir, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	fi, err := dir.Readdir(0)
	if err != nil {
		panic(err)
	}

	for i := range fi {
		if fi[i].IsDir() {
			continue
		}

		if !strings.HasSuffix(fi[i].Name(), ".zs") {
			continue
		}

		filename := pathpkg.Join(path, fi[i].Name())

		var err error

		parser := os.Getenv("ZSPARSER")
		if parser == "" {
			parser = "zsparser"
		}

		cmd := exec.Command(parser)
		cmd.Stderr = os.Stderr
		cmd.Stdin, err = os.Open(filename)
		if err != nil {
			log.Fatalf("Cannot open file %s: %s", filename, err)
		}

		// TODO: Get rid of panics.

		stdout, err := cmd.StdoutPipe()
		if err != nil {
			panic(err)
		}

		err = cmd.Start()
		if err != nil {
			panic(err)
		}

		p := spl.NewSeqParser(stdout)

		file, err1 := ParseASTFile(p)

		err = cmd.Wait()
		if err != nil {
			log.Fatalf("Parser failed on file %s: %s", filename, err)
		}

		if err1 != nil {
			log.Fatalf("Unmarshaller failed on file %s: %s", filename, err)
		}

		pkg.LoadFile(app, file._globals)
	}

	for _, t := range pkg.Types {
		t.Resolve()
	}

	for _, m := range pkg.MethodList {
		m.Resolve()
	}

	for _, m := range pkg.MethodList {
		NewTranscribeContext(m).Transcribe()
	}

	app.Packages[name] = pkg
}
<|endoftext|>