<commit_before>package expreduce

func GetBooleanDefinitions() (defs []Definition) {
	defs = append(defs, Definition{
		Name:       "And",
		Usage:      "`e1 && e2 && ...` returns `True` if all expressions evaluate to `True`.",
		Attributes: []string{"Flat", "HoldAll", "OneIdentity"},
		toString: func(this *Expression, form string) (bool, string) {
			return ToStringInfix(this.Parts[1:], " && ", form)
		},
		legacyEvalFn: func(this *Expression, es *EvalState) Ex {
			res := &Expression{[]Ex{&Symbol{"And"}}}
			for i := 1; i < len(this.Parts); i++ {
				this.Parts[i] = this.Parts[i].Eval(es)
				if booleanQ(this.Parts[i], &es.CASLogger) {
				    if falseQ(this.Parts[i], &es.CASLogger) {
						return &Symbol{"False"}
					}
				} else {
					res.appendEx(this.Parts[i])
				}
			}
			if len(res.Parts) == 1 {
				return &Symbol{"True"}
			}
			if len(res.Parts) == 2 {
				return res.Parts[1]
			}
			return res
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"False", "True && False"},
			&SameTest{"True", "True && True && True"},
		},
		Tests: []TestInstruction{
			&SameTest{"True", "And[]"},
			&SameTest{"1", "1 && True && True"},
			&SameTest{"False", "True && False"},
			&SameTest{"False", "False && True"},
			&SameTest{"True", "True && True"},
			&SameTest{"False", "False && 1"},
			&SameTest{"False", "1 && False"},
			&SameTest{"1 && 1", "1 && 1"},
			&SameTest{"1 && 1 && kfdkkfd", "1 && 1 && kfdkkfd"},
			&SameTest{"1 && 1 && kfdkkfd", "1 && 1 && True && kfdkkfd"},
			&SameTest{"False", "1 && 1 && True && False && kfdkkfd"},
		},
	})
	defs = append(defs, Definition{
		Name:       "Or",
		Usage:      "`e1 || e2 || ...` returns `True` if any expressions evaluate to `True`.",
		Attributes: []string{"Flat", "HoldAll", "OneIdentity"},
		toString: func(this *Expression, form string) (bool, string) {
			return ToStringInfix(this.Parts[1:], " || ", form)
		},
		legacyEvalFn: func(this *Expression, es *EvalState) Ex {
			res := &Expression{[]Ex{&Symbol{"Or"}}}
			for i := 1; i < len(this.Parts); i++ {
				this.Parts[i] = this.Parts[i].Eval(es)
				if booleanQ(this.Parts[i], &es.CASLogger) {
				    if trueQ(this.Parts[i], &es.CASLogger) {
						return &Symbol{"True"}
					}
				} else {
					res.appendEx(this.Parts[i])
				}
			}
			if len(res.Parts) == 1 {
				return &Symbol{"False"}
			}
			if len(res.Parts) == 2 {
				return res.Parts[1]
			}
			return res
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"True", "True || False"},
			&SameTest{"False", "False || False || False"},
		},
		Tests: []TestInstruction{
			&SameTest{"a || b", "a || b"},
			&SameTest{"True", "a || True || b"},
			&SameTest{"True", "a || True || False"},
			&SameTest{"a || b", "a || b || False"},
			&SameTest{"a || b", "a || b || False || False"},
			&SameTest{"a || b", "a || False || b || False || False"},
			&SameTest{"True", "True || False"},
			&SameTest{"False", "False || False"},
			&SameTest{"False", "Or[False]"},
			&SameTest{"False", "Or[]"},
		},
	})
	defs = append(defs, Definition{
		Name:       "Not",
		Usage:      "`!e` returns `True` if `e` is `False` and `False` if `e` is `True`.",
		Attributes: []string{},
		legacyEvalFn: func(this *Expression, es *EvalState) Ex {
			if len(this.Parts) != 2 {
				return this
			}
			if trueQ(this.Parts[1], &es.CASLogger) {
				return &Symbol{"False"}
			}
			if falseQ(this.Parts[1], &es.CASLogger) {
				return &Symbol{"True"}
			}
			return this
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"False", "!True"},
			&SameTest{"True", "!False"},
			&SameTest{"!a", "!a"},
			&SameTest{"a", "!!a"},
		},
		Rules: []Rule{
			{"!!e_", "e"},
		},
	})
	defs = append(defs, Definition{
		Name:         "TrueQ",
		Usage:        "`TrueQ[expr]` returns True if `expr` is True, False otherwise.",
		legacyEvalFn: singleParamQLogEval(trueQ),
		SimpleExamples: []TestInstruction{
			&SameTest{"True", "TrueQ[True]"},
			&SameTest{"False", "TrueQ[False]"},
			&SameTest{"False", "TrueQ[1]"},
		},
	})
	defs = append(defs, Definition{
		Name:         "BooleanQ",
		Usage:        "`BooleanQ[expr]` returns True if `expr` is True or False, False otherwise.",
		legacyEvalFn: singleParamQLogEval(booleanQ),
		SimpleExamples: []TestInstruction{
			&SameTest{"True", "BooleanQ[True]"},
			&SameTest{"True", "BooleanQ[False]"},
			&SameTest{"False", "BooleanQ[1]"},
		},
	})
	defs = append(defs, Definition{
		Name:         "AllTrue",
		Usage:        "`AllTrue[list, condition]` returns True if all parts of `list` satisfy `condition`.",
		SimpleExamples: []TestInstruction{
			&SameTest{"False", "AllTrue[{1, a}, NumberQ]"},
			&SameTest{"True", "AllTrue[{1, 2}, NumberQ]"},
		},
		Rules: []Rule{
			{"AllTrue[_[elems___], cond_]", "And @@ (cond /@ {elems})"},
		},
	})
	/*
	defs = append(defs, Definition{
		Name: "LogicalExpand",
		SimpleExamples: []TestInstruction{
			&TestComment{"`LogicalExpand` can expand logic expressions."},
		},
		Rules: []Rule{
			{"LogicalExpand[exp_]", "exp //. {And[begin___,e_,!e_,end___]:>And[begin, end]}"},
		},
	})*/
	return
}
<commit_msg>Boole function.<commit_after>package expreduce

func GetBooleanDefinitions() (defs []Definition) {
	defs = append(defs, Definition{
		Name:       "And",
		Usage:      "`e1 && e2 && ...` returns `True` if all expressions evaluate to `True`.",
		Attributes: []string{"Flat", "HoldAll", "OneIdentity"},
		toString: func(this *Expression, form string) (bool, string) {
			return ToStringInfix(this.Parts[1:], " && ", form)
		},
		legacyEvalFn: func(this *Expression, es *EvalState) Ex {
			res := &Expression{[]Ex{&Symbol{"And"}}}
			for i := 1; i < len(this.Parts); i++ {
				this.Parts[i] = this.Parts[i].Eval(es)
				if booleanQ(this.Parts[i], &es.CASLogger) {
				    if falseQ(this.Parts[i], &es.CASLogger) {
						return &Symbol{"False"}
					}
				} else {
					res.appendEx(this.Parts[i])
				}
			}
			if len(res.Parts) == 1 {
				return &Symbol{"True"}
			}
			if len(res.Parts) == 2 {
				return res.Parts[1]
			}
			return res
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"False", "True && False"},
			&SameTest{"True", "True && True && True"},
		},
		Tests: []TestInstruction{
			&SameTest{"True", "And[]"},
			&SameTest{"1", "1 && True && True"},
			&SameTest{"False", "True && False"},
			&SameTest{"False", "False && True"},
			&SameTest{"True", "True && True"},
			&SameTest{"False", "False && 1"},
			&SameTest{"False", "1 && False"},
			&SameTest{"1 && 1", "1 && 1"},
			&SameTest{"1 && 1 && kfdkkfd", "1 && 1 && kfdkkfd"},
			&SameTest{"1 && 1 && kfdkkfd", "1 && 1 && True && kfdkkfd"},
			&SameTest{"False", "1 && 1 && True && False && kfdkkfd"},
		},
	})
	defs = append(defs, Definition{
		Name:       "Or",
		Usage:      "`e1 || e2 || ...` returns `True` if any expressions evaluate to `True`.",
		Attributes: []string{"Flat", "HoldAll", "OneIdentity"},
		toString: func(this *Expression, form string) (bool, string) {
			return ToStringInfix(this.Parts[1:], " || ", form)
		},
		legacyEvalFn: func(this *Expression, es *EvalState) Ex {
			res := &Expression{[]Ex{&Symbol{"Or"}}}
			for i := 1; i < len(this.Parts); i++ {
				this.Parts[i] = this.Parts[i].Eval(es)
				if booleanQ(this.Parts[i], &es.CASLogger) {
				    if trueQ(this.Parts[i], &es.CASLogger) {
						return &Symbol{"True"}
					}
				} else {
					res.appendEx(this.Parts[i])
				}
			}
			if len(res.Parts) == 1 {
				return &Symbol{"False"}
			}
			if len(res.Parts) == 2 {
				return res.Parts[1]
			}
			return res
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"True", "True || False"},
			&SameTest{"False", "False || False || False"},
		},
		Tests: []TestInstruction{
			&SameTest{"a || b", "a || b"},
			&SameTest{"True", "a || True || b"},
			&SameTest{"True", "a || True || False"},
			&SameTest{"a || b", "a || b || False"},
			&SameTest{"a || b", "a || b || False || False"},
			&SameTest{"a || b", "a || False || b || False || False"},
			&SameTest{"True", "True || False"},
			&SameTest{"False", "False || False"},
			&SameTest{"False", "Or[False]"},
			&SameTest{"False", "Or[]"},
		},
	})
	defs = append(defs, Definition{
		Name:       "Not",
		Usage:      "`!e` returns `True` if `e` is `False` and `False` if `e` is `True`.",
		Attributes: []string{},
		legacyEvalFn: func(this *Expression, es *EvalState) Ex {
			if len(this.Parts) != 2 {
				return this
			}
			if trueQ(this.Parts[1], &es.CASLogger) {
				return &Symbol{"False"}
			}
			if falseQ(this.Parts[1], &es.CASLogger) {
				return &Symbol{"True"}
			}
			return this
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"False", "!True"},
			&SameTest{"True", "!False"},
			&SameTest{"!a", "!a"},
			&SameTest{"a", "!!a"},
		},
		Rules: []Rule{
			{"!!e_", "e"},
		},
	})
	defs = append(defs, Definition{
		Name:         "TrueQ",
		Usage:        "`TrueQ[expr]` returns True if `expr` is True, False otherwise.",
		legacyEvalFn: singleParamQLogEval(trueQ),
		SimpleExamples: []TestInstruction{
			&SameTest{"True", "TrueQ[True]"},
			&SameTest{"False", "TrueQ[False]"},
			&SameTest{"False", "TrueQ[1]"},
		},
	})
	defs = append(defs, Definition{
		Name:         "BooleanQ",
		Usage:        "`BooleanQ[expr]` returns True if `expr` is True or False, False otherwise.",
		legacyEvalFn: singleParamQLogEval(booleanQ),
		SimpleExamples: []TestInstruction{
			&SameTest{"True", "BooleanQ[True]"},
			&SameTest{"True", "BooleanQ[False]"},
			&SameTest{"False", "BooleanQ[1]"},
		},
	})
	defs = append(defs, Definition{
		Name:         "AllTrue",
		Usage:        "`AllTrue[list, condition]` returns True if all parts of `list` satisfy `condition`.",
		SimpleExamples: []TestInstruction{
			&SameTest{"False", "AllTrue[{1, a}, NumberQ]"},
			&SameTest{"True", "AllTrue[{1, 2}, NumberQ]"},
		},
		Rules: []Rule{
			{"AllTrue[_[elems___], cond_]", "And @@ (cond /@ {elems})"},
		},
	})
	/*
	defs = append(defs, Definition{
		Name: "LogicalExpand",
		SimpleExamples: []TestInstruction{
			&TestComment{"`LogicalExpand` can expand logic expressions."},
		},
		Rules: []Rule{
			{"LogicalExpand[exp_]", "exp //. {And[begin___,e_,!e_,end___]:>And[begin, end]}"},
		},
	})*/
	defs = append(defs, Definition{
		Name:  "Boole",
		Usage:  "`Boole[e]` returns 0 if `e` is False and 1 if `e` is True.",
		Attributes: []string{"Listable"},
		Rules: []Rule{
			{"Boole[True]", "1"},
			{"Boole[False]", "0"},
		},
		SimpleExamples: []TestInstruction{
			&SameTest{"1", "Boole[True]"},
			&SameTest{"0", "Boole[False]"},
		},
		Tests: []TestInstruction{
			&SameTest{"Boole[1]", "Boole[1]"},
			&SameTest{"Boole[a]", "Boole[a]"},
			&SameTest{"Boole[False,False]", "Boole[False, False]"},
		},
	})
	return
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
	var mf cloudwatchlogs.MetricFilter
	rInt := acctest.RandInt()

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSCloudWatchLogMetricFilterDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccAWSCloudWatchLogMetricFilterConfig(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", ""),
					testAccCheckCloudWatchLogMetricFilterPattern(&mf, ""),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "EventCount"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "YourNamespace"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "1"),
					testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
						MetricName:      aws.String("EventCount"),
						MetricNamespace: aws.String("YourNamespace"),
						MetricValue:     aws.String("1"),
					}),
				),
			},
			{
				Config: testAccAWSCloudWatchLogMetricFilterConfigModified(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", "{ $.errorCode = \"AccessDenied\" }"),
					testAccCheckCloudWatchLogMetricFilterPattern(&mf, "{ $.errorCode = \"AccessDenied\" }"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "AccessDeniedCount"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "MyNamespace"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "2"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.default_value", "1"),
					testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
						MetricName:      aws.String("AccessDeniedCount"),
						MetricNamespace: aws.String("MyNamespace"),
						MetricValue:     aws.String("2"),
						DefaultValue:    aws.Float64(1),
					}),
				),
			},
		},
	})
}

func testAccCheckCloudWatchLogMetricFilterName(mf *cloudwatchlogs.MetricFilter, name string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		if name != *mf.FilterName {
			return fmt.Errorf("Expected filter name: %q, given: %q", name, *mf.FilterName)
		}
		return nil
	}
}

func testAccCheckCloudWatchLogMetricFilterPattern(mf *cloudwatchlogs.MetricFilter, pattern string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		if mf.FilterPattern == nil {
			if pattern != "" {
				return fmt.Errorf("Received empty filter pattern, expected: %q", pattern)
			}
			return nil
		}

		if pattern != *mf.FilterPattern {
			return fmt.Errorf("Expected filter pattern: %q, given: %q", pattern, *mf.FilterPattern)
		}
		return nil
	}
}

func testAccCheckCloudWatchLogMetricFilterTransformation(mf *cloudwatchlogs.MetricFilter,
	t *cloudwatchlogs.MetricTransformation) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		given := mf.MetricTransformations[0]
		expected := t

		if *given.MetricName != *expected.MetricName {
			return fmt.Errorf("Expected metric name: %q, received: %q",
				*expected.MetricName, *given.MetricName)
		}

		if *given.MetricNamespace != *expected.MetricNamespace {
			return fmt.Errorf("Expected metric namespace: %q, received: %q",
				*expected.MetricNamespace, *given.MetricNamespace)
		}

		if *given.MetricValue != *expected.MetricValue {
			return fmt.Errorf("Expected metric value: %q, received: %q",
				*expected.MetricValue, *given.MetricValue)
		}

		if (given.DefaultValue != nil) != (expected.DefaultValue != nil) {
			return fmt.Errorf("Expected default value to be present: %t, received: %t",
				expected.DefaultValue != nil, given.DefaultValue != nil)
		} else if (given.DefaultValue != nil) && *given.DefaultValue != *expected.DefaultValue {
			return fmt.Errorf("Expected metric value: %g, received: %g",
				*expected.DefaultValue, *given.DefaultValue)
		}

		return nil
	}
}

func testAccCheckCloudWatchLogMetricFilterExists(n string, mf *cloudwatchlogs.MetricFilter) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
		metricFilter, err := lookupCloudWatchLogMetricFilter(conn, rs.Primary.ID, rs.Primary.Attributes["log_group_name"], nil)
		if err != nil {
			return err
		}

		*mf = *metricFilter

		return nil
	}
}

func testAccCheckAWSCloudWatchLogMetricFilterDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_cloudwatch_log_metric_filter" {
			continue
		}

		_, err := lookupCloudWatchLogMetricFilter(conn, rs.Primary.ID, rs.Primary.Attributes["log_group_name"], nil)
		if err == nil {
			return fmt.Errorf("MetricFilter Still Exists: %s", rs.Primary.ID)
		}
	}

	return nil
}

func testAccAWSCloudWatchLogMetricFilterConfig(rInt int) string {
	return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" {
  name = "MyAppAccessCount-%d"
  pattern = ""
  log_group_name = "${aws_cloudwatch_log_group.dada.name}"

  metric_transformation {
  	name = "EventCount"
  	namespace = "YourNamespace"
  	value = "1"
  }
}

resource "aws_cloudwatch_log_group" "dada" {
	name = "MyApp/access-%d.log"
}
`, rInt, rInt)
}

func testAccAWSCloudWatchLogMetricFilterConfigModified(rInt int) string {
	return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" {
  name = "MyAppAccessCount-%d"
  pattern = <<PATTERN
{ $.errorCode = "AccessDenied" }
PATTERN
  log_group_name = "${aws_cloudwatch_log_group.dada.name}"

  metric_transformation {
  	name = "AccessDeniedCount"
  	namespace = "MyNamespace"
  	value = "2"
  	default_value = "1"
  }
}

resource "aws_cloudwatch_log_group" "dada" {
	name = "MyApp/access-%d.log"
}
`, rInt, rInt)
}
<commit_msg>Add test to create multiple filters. #7605<commit_after>package aws

import (
	"fmt"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
	var mf cloudwatchlogs.MetricFilter
	rInt := acctest.RandInt()

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSCloudWatchLogMetricFilterDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccAWSCloudWatchLogMetricFilterConfig(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", ""),
					testAccCheckCloudWatchLogMetricFilterPattern(&mf, ""),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "EventCount"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "YourNamespace"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "1"),
					testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
						MetricName:      aws.String("EventCount"),
						MetricNamespace: aws.String("YourNamespace"),
						MetricValue:     aws.String("1"),
					}),
				),
			},
			{
				Config: testAccAWSCloudWatchLogMetricFilterConfigModified(rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", "{ $.errorCode = \"AccessDenied\" }"),
					testAccCheckCloudWatchLogMetricFilterPattern(&mf, "{ $.errorCode = \"AccessDenied\" }"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "AccessDeniedCount"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "MyNamespace"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "2"),
					resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.default_value", "1"),
					testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
						MetricName:      aws.String("AccessDeniedCount"),
						MetricNamespace: aws.String("MyNamespace"),
						MetricValue:     aws.String("2"),
						DefaultValue:    aws.Float64(1),
					}),
				),
			},
			{
				Config: testAccAWSCloudwatchLogMetricFilterConfigMany(rInt),
				Check:  testAccCheckCloudwatchLogMetricFilterManyExist("aws_cloudwatch_log_metric_filter.count_dracula", &mf),
			},
		},
	})
}

func testAccCheckCloudWatchLogMetricFilterName(mf *cloudwatchlogs.MetricFilter, name string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		if name != *mf.FilterName {
			return fmt.Errorf("Expected filter name: %q, given: %q", name, *mf.FilterName)
		}
		return nil
	}
}

func testAccCheckCloudWatchLogMetricFilterPattern(mf *cloudwatchlogs.MetricFilter, pattern string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		if mf.FilterPattern == nil {
			if pattern != "" {
				return fmt.Errorf("Received empty filter pattern, expected: %q", pattern)
			}
			return nil
		}

		if pattern != *mf.FilterPattern {
			return fmt.Errorf("Expected filter pattern: %q, given: %q", pattern, *mf.FilterPattern)
		}
		return nil
	}
}

func testAccCheckCloudWatchLogMetricFilterTransformation(mf *cloudwatchlogs.MetricFilter,
	t *cloudwatchlogs.MetricTransformation) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		given := mf.MetricTransformations[0]
		expected := t

		if *given.MetricName != *expected.MetricName {
			return fmt.Errorf("Expected metric name: %q, received: %q",
				*expected.MetricName, *given.MetricName)
		}

		if *given.MetricNamespace != *expected.MetricNamespace {
			return fmt.Errorf("Expected metric namespace: %q, received: %q",
				*expected.MetricNamespace, *given.MetricNamespace)
		}

		if *given.MetricValue != *expected.MetricValue {
			return fmt.Errorf("Expected metric value: %q, received: %q",
				*expected.MetricValue, *given.MetricValue)
		}

		if (given.DefaultValue != nil) != (expected.DefaultValue != nil) {
			return fmt.Errorf("Expected default value to be present: %t, received: %t",
				expected.DefaultValue != nil, given.DefaultValue != nil)
		} else if (given.DefaultValue != nil) && *given.DefaultValue != *expected.DefaultValue {
			return fmt.Errorf("Expected metric value: %g, received: %g",
				*expected.DefaultValue, *given.DefaultValue)
		}

		return nil
	}
}

func testAccCheckCloudWatchLogMetricFilterExists(n string, mf *cloudwatchlogs.MetricFilter) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
		metricFilter, err := lookupCloudWatchLogMetricFilter(conn, rs.Primary.ID, rs.Primary.Attributes["log_group_name"], nil)
		if err != nil {
			return err
		}

		*mf = *metricFilter

		return nil
	}
}

func testAccCheckAWSCloudWatchLogMetricFilterDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_cloudwatch_log_metric_filter" {
			continue
		}

		_, err := lookupCloudWatchLogMetricFilter(conn, rs.Primary.ID, rs.Primary.Attributes["log_group_name"], nil)
		if err == nil {
			return fmt.Errorf("MetricFilter Still Exists: %s", rs.Primary.ID)
		}
	}

	return nil
}

func testAccCheckCloudwatchLogMetricFilterManyExist(basename string, mf *cloudwatchlogs.MetricFilter) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		for i := 0; i < 15; i++ {
			n := fmt.Sprintf("%s.%d", basename, i)
			testfunc := testAccCheckCloudWatchLogMetricFilterExists(n, mf)
			err := testfunc(s)
			if err != nil {
				return err
			}
		}

		return nil
	}
}

func testAccAWSCloudWatchLogMetricFilterConfig(rInt int) string {
	return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" {
  name = "MyAppAccessCount-%d"
  pattern = ""
  log_group_name = "${aws_cloudwatch_log_group.dada.name}"

  metric_transformation {
  	name = "EventCount"
  	namespace = "YourNamespace"
  	value = "1"
  }
}

resource "aws_cloudwatch_log_group" "dada" {
	name = "MyApp/access-%d.log"
}
`, rInt, rInt)
}

func testAccAWSCloudWatchLogMetricFilterConfigModified(rInt int) string {
	return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" {
  name = "MyAppAccessCount-%d"
  pattern = <<PATTERN
{ $.errorCode = "AccessDenied" }
PATTERN
  log_group_name = "${aws_cloudwatch_log_group.dada.name}"

  metric_transformation {
  	name = "AccessDeniedCount"
  	namespace = "MyNamespace"
  	value = "2"
  	default_value = "1"
  }
}

resource "aws_cloudwatch_log_group" "dada" {
	name = "MyApp/access-%d.log"
}
`, rInt, rInt)
}

func testAccAWSCloudwatchLogMetricFilterConfigMany(rInt int) string {
	return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "count_dracula" {
	count = 15
	name = "MyAppCountLog-${count.index}-%d"
	pattern = "count ${count.index}"
	log_group_name = "${aws_cloudwatch_log_group.mama.name}"

	metric_transformation {
		name = "CountDracula-${count.index}"
		namespace = "CountNamespace"
		value = "1"
	}
}

resource "aws_cloudwatch_log_group" "mama" {
	name = "MyApp/count-log-%d.log"
}
`, rInt, rInt)
}
<|endoftext|>


<commit_before><commit_msg>v.io/jiri/runutil: fix a flaky test.<commit_after><|endoftext|>


<commit_before>package aws

import (
	"bytes"
	"crypto/sha256"
	"fmt"
	"io"
	"io/ioutil"
	"path"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/aws/aws-sdk-go/service/sns"
	"github.com/nerdalize/nerd/nerd"
	"github.com/nerdalize/nerd/nerd/data"
	"github.com/pkg/errors"
	"github.com/restic/chunker"
)

//DataClient holds a reference to an AWS session
type DataClient struct {
	Session *session.Session
	*DataClientConfig
}

//DataClientConfig provides config details to create a new DataClient.
type DataClientConfig struct {
	Credentials *credentials.Credentials
	Bucket      string
}

//NewDataClient creates a new data client that is capable of uploading and downloading (multiple) files.
func NewDataClient(conf *DataClientConfig) (*DataClient, error) {
	// TODO: Don't hardcode region
	sess, err := session.NewSession(&aws.Config{
		Credentials: conf.Credentials,
		Region:      aws.String(nerd.GetCurrentUser().Region),
	})
	if err != nil {
		return nil, fmt.Errorf("could not create AWS sessions: %v", err)
	}
	return &DataClient{
		Session:          sess,
		DataClientConfig: conf,
	}, nil
}

//Upload uploads a piece of data.
func (client *DataClient) Upload(key string, body io.ReadSeeker) error {
	// TODO: retries
	svc := s3.New(client.Session)
	params := &s3.PutObjectInput{
		Bucket: aws.String(client.Bucket), // Required
		Key:    aws.String(key),           // Required
		Body:   body,
	}
	_, err := svc.PutObject(params)
	if err != nil {
		return errors.Wrapf(err, "could not put key %v", key)
	}
	return nil
}

//ChunkedUpload uploads data from a io.Reader (`r`) as a list of chunks. The Key of every chunk uploaded will be written to the KeyReadWriter (`kw`).
//ChunkedDownload reports its progress (the amount of bytes uploaded) to the progressCh.
//It will start a maximum of `concurrency` concurrent go routines to upload in paralllel.
//`root` is used as the root path of the chunk in S3. Root will be concatenated with the key to make the full S3 object path.
func (client *DataClient) ChunkedUpload(r io.Reader, kw data.KeyReadWriter, concurrency int, root string, progressCh chan<- int64) (err error) {
	cr := chunker.New(r, chunker.Pol(0x3DA3358B4DC173))
	type result struct {
		err error
		k   data.Key
	}

	type item struct {
		chunk []byte
		size  int64
		resCh chan *result
		err   error
	}

	work := func(it *item) {
		k := data.Key(sha256.Sum256(it.chunk)) //hash
		key := path.Join(root, k.ToString())
		exists, err := client.Exists(key) //check existence
		if err != nil {
			it.resCh <- &result{fmt.Errorf("failed to check existence of '%x': %v", k, err), data.ZeroKey}
			return
		}

		if !exists {
			err = client.Upload(key, bytes.NewReader(it.chunk)) //if not exists put
			if err != nil {
				it.resCh <- &result{fmt.Errorf("failed to put chunk '%x': %v", k, err), data.ZeroKey}
				return
			}
		}
		progressCh <- int64(len(it.chunk))

		it.resCh <- &result{nil, k}
	}

	//fan out
	itemCh := make(chan *item, concurrency)
	go func() {
		defer close(itemCh)
		buf := make([]byte, chunker.MaxSize)
		for {
			chunk, err := cr.Next(buf)
			if err != nil {
				if err != io.EOF {
					itemCh <- &item{err: err}
				}
				break
			}

			it := &item{
				chunk: make([]byte, chunk.Length),
				resCh: make(chan *result),
			}

			copy(it.chunk, chunk.Data) //underlying buffer is switched out

			go work(it)  //create work
			itemCh <- it //send to fan-in thread for syncing results
		}
	}()

	//fan-in
	for it := range itemCh {
		if it.err != nil {
			return fmt.Errorf("failed to iterate: %v", it.err)
		}

		res := <-it.resCh
		if res.err != nil {
			return res.err
		}

		err = kw.WriteKey(res.k)
		if err != nil {
			return fmt.Errorf("failed to write key: %v", err)
		}
	}

	return nil
}

//Download downloads a single object.
func (client *DataClient) Download(key string) (io.ReadCloser, error) {
	var r io.ReadCloser
	NoOfRetries := 2
	for i := 0; i <= NoOfRetries; i++ {
		svc := s3.New(client.Session)
		params := &s3.GetObjectInput{
			Bucket: aws.String(client.Bucket), // Required
			Key:    aws.String(key),           // Required
		}
		resp, err := svc.GetObject(params)

		if err != nil {
			if i < NoOfRetries {
				continue
			}
			// TODO: fmt should be errors
			return nil, fmt.Errorf("failed to download '%v': %v", key, err)
		}
		r = resp.Body
		break
	}
	return r, nil
}

//Exists checks if a given object key exists on S3.
func (client *DataClient) Exists(objectKey string) (has bool, err error) {
	svc := s3.New(client.Session)

	params := &s3.HeadObjectInput{
		Bucket: aws.String(client.Bucket), // Required
		Key:    aws.String(objectKey),
	}
	_, err = svc.HeadObject(params)
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok && (aerr.Code() == s3.ErrCodeNoSuchKey || aerr.Code() == sns.ErrCodeNotFoundException) {
			return false, nil
		}
		return false, errors.Wrapf(err, "failed to check if key %v exists", objectKey)
	}
	return true, nil
}

//ChunkedDownload downloads a list of chunks and writes them to a io.Writer.
//ChunkedDownload reports its progress (the amount of bytes downloaded) to the progressCh.
//It will start a maximum of `concurrency` concurrent go routines to download in paralllel.
//`root` is used as the root path of the chunk in S3. Root will be concatenated with the Key read from `kr` to make the full S3 object path.
func (client *DataClient) ChunkedDownload(kr data.KeyReadWriter, cw io.Writer, concurrency int, root string, progressCh chan<- int64) (err error) {
	type result struct {
		err   error
		chunk []byte
	}

	type item struct {
		k     data.Key
		resCh chan *result
		err   error
	}

	work := func(it *item) {
		// TODO: add root
		r, err := client.Download(path.Join(root, it.k.ToString()))
		defer r.Close()
		if err != nil {
			it.resCh <- &result{fmt.Errorf("failed to get key '%s': %v", it.k, err), nil}
			return
		}

		chunk, err := ioutil.ReadAll(r)
		if err != nil {
			it.resCh <- &result{errors.Wrap(err, "failed to copy chunk to byte buffer"), nil}
		}

		progressCh <- int64(len(chunk))

		it.resCh <- &result{nil, chunk}
	}

	//fan out
	itemCh := make(chan *item, concurrency)
	go func() {
		defer close(itemCh)
		for {
			k, err := kr.ReadKey()
			if err != nil {
				if err != io.EOF {
					itemCh <- &item{err: err}
				}
				break
			}

			it := &item{
				k:     k,
				resCh: make(chan *result),
			}

			go work(it)  //create work
			itemCh <- it //send to fan-in thread for syncing results
		}
	}()

	//fan-in
	for it := range itemCh {
		if it.err != nil {
			return fmt.Errorf("failed to iterate: %v", it.err)
		}

		res := <-it.resCh
		if res.err != nil {
			return res.err
		}

		_, err = cw.Write(res.chunk)
		if err != nil {
			return fmt.Errorf("failed to write key: %v", err)
		}
	}

	return nil
}
<commit_msg>Remove magic constant<commit_after>package aws

import (
	"bytes"
	"crypto/sha256"
	"fmt"
	"io"
	"io/ioutil"
	"path"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/aws/aws-sdk-go/service/sns"
	"github.com/nerdalize/nerd/nerd"
	"github.com/nerdalize/nerd/nerd/data"
	"github.com/pkg/errors"
	"github.com/restic/chunker"
)

const (
	//uploadPolynomal is the polynomal that is used for chunked uploading.
	uploadPolynomal = 0x3DA3358B4DC173
)

//DataClient holds a reference to an AWS session
type DataClient struct {
	Session *session.Session
	*DataClientConfig
}

//DataClientConfig provides config details to create a new DataClient.
type DataClientConfig struct {
	Credentials *credentials.Credentials
	Bucket      string
}

//NewDataClient creates a new data client that is capable of uploading and downloading (multiple) files.
func NewDataClient(conf *DataClientConfig) (*DataClient, error) {
	// TODO: Don't hardcode region
	sess, err := session.NewSession(&aws.Config{
		Credentials: conf.Credentials,
		Region:      aws.String(nerd.GetCurrentUser().Region),
	})
	if err != nil {
		return nil, fmt.Errorf("could not create AWS sessions: %v", err)
	}
	return &DataClient{
		Session:          sess,
		DataClientConfig: conf,
	}, nil
}

//Upload uploads a piece of data.
func (client *DataClient) Upload(key string, body io.ReadSeeker) error {
	// TODO: retries
	svc := s3.New(client.Session)
	params := &s3.PutObjectInput{
		Bucket: aws.String(client.Bucket), // Required
		Key:    aws.String(key),           // Required
		Body:   body,
	}
	_, err := svc.PutObject(params)
	if err != nil {
		return errors.Wrapf(err, "could not put key %v", key)
	}
	return nil
}

//ChunkedUpload uploads data from a io.Reader (`r`) as a list of chunks. The Key of every chunk uploaded will be written to the KeyReadWriter (`kw`).
//ChunkedDownload reports its progress (the amount of bytes uploaded) to the progressCh.
//It will start a maximum of `concurrency` concurrent go routines to upload in paralllel.
//`root` is used as the root path of the chunk in S3. Root will be concatenated with the key to make the full S3 object path.
func (client *DataClient) ChunkedUpload(r io.Reader, kw data.KeyReadWriter, concurrency int, root string, progressCh chan<- int64) (err error) {
	cr := chunker.New(r, chunker.Pol(uploadPolynomal))
	type result struct {
		err error
		k   data.Key
	}

	type item struct {
		chunk []byte
		size  int64
		resCh chan *result
		err   error
	}

	work := func(it *item) {
		k := data.Key(sha256.Sum256(it.chunk)) //hash
		key := path.Join(root, k.ToString())
		exists, err := client.Exists(key) //check existence
		if err != nil {
			it.resCh <- &result{fmt.Errorf("failed to check existence of '%x': %v", k, err), data.ZeroKey}
			return
		}

		if !exists {
			err = client.Upload(key, bytes.NewReader(it.chunk)) //if not exists put
			if err != nil {
				it.resCh <- &result{fmt.Errorf("failed to put chunk '%x': %v", k, err), data.ZeroKey}
				return
			}
		}
		progressCh <- int64(len(it.chunk))

		it.resCh <- &result{nil, k}
	}

	//fan out
	itemCh := make(chan *item, concurrency)
	go func() {
		defer close(itemCh)
		buf := make([]byte, chunker.MaxSize)
		for {
			chunk, err := cr.Next(buf)
			if err != nil {
				if err != io.EOF {
					itemCh <- &item{err: err}
				}
				break
			}

			it := &item{
				chunk: make([]byte, chunk.Length),
				resCh: make(chan *result),
			}

			copy(it.chunk, chunk.Data) //underlying buffer is switched out

			go work(it)  //create work
			itemCh <- it //send to fan-in thread for syncing results
		}
	}()

	//fan-in
	for it := range itemCh {
		if it.err != nil {
			return fmt.Errorf("failed to iterate: %v", it.err)
		}

		res := <-it.resCh
		if res.err != nil {
			return res.err
		}

		err = kw.WriteKey(res.k)
		if err != nil {
			return fmt.Errorf("failed to write key: %v", err)
		}
	}

	return nil
}

//Download downloads a single object.
func (client *DataClient) Download(key string) (io.ReadCloser, error) {
	var r io.ReadCloser
	NoOfRetries := 2
	for i := 0; i <= NoOfRetries; i++ {
		svc := s3.New(client.Session)
		params := &s3.GetObjectInput{
			Bucket: aws.String(client.Bucket), // Required
			Key:    aws.String(key),           // Required
		}
		resp, err := svc.GetObject(params)

		if err != nil {
			if i < NoOfRetries {
				continue
			}
			// TODO: fmt should be errors
			return nil, fmt.Errorf("failed to download '%v': %v", key, err)
		}
		r = resp.Body
		break
	}
	return r, nil
}

//Exists checks if a given object key exists on S3.
func (client *DataClient) Exists(objectKey string) (has bool, err error) {
	svc := s3.New(client.Session)

	params := &s3.HeadObjectInput{
		Bucket: aws.String(client.Bucket), // Required
		Key:    aws.String(objectKey),
	}
	_, err = svc.HeadObject(params)
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok && (aerr.Code() == s3.ErrCodeNoSuchKey || aerr.Code() == sns.ErrCodeNotFoundException) {
			return false, nil
		}
		return false, errors.Wrapf(err, "failed to check if key %v exists", objectKey)
	}
	return true, nil
}

//ChunkedDownload downloads a list of chunks and writes them to a io.Writer.
//ChunkedDownload reports its progress (the amount of bytes downloaded) to the progressCh.
//It will start a maximum of `concurrency` concurrent go routines to download in paralllel.
//`root` is used as the root path of the chunk in S3. Root will be concatenated with the Key read from `kr` to make the full S3 object path.
func (client *DataClient) ChunkedDownload(kr data.KeyReadWriter, cw io.Writer, concurrency int, root string, progressCh chan<- int64) (err error) {
	type result struct {
		err   error
		chunk []byte
	}

	type item struct {
		k     data.Key
		resCh chan *result
		err   error
	}

	work := func(it *item) {
		// TODO: add root
		r, err := client.Download(path.Join(root, it.k.ToString()))
		defer r.Close()
		if err != nil {
			it.resCh <- &result{fmt.Errorf("failed to get key '%s': %v", it.k, err), nil}
			return
		}

		chunk, err := ioutil.ReadAll(r)
		if err != nil {
			it.resCh <- &result{errors.Wrap(err, "failed to copy chunk to byte buffer"), nil}
		}

		progressCh <- int64(len(chunk))

		it.resCh <- &result{nil, chunk}
	}

	//fan out
	itemCh := make(chan *item, concurrency)
	go func() {
		defer close(itemCh)
		for {
			k, err := kr.ReadKey()
			if err != nil {
				if err != io.EOF {
					itemCh <- &item{err: err}
				}
				break
			}

			it := &item{
				k:     k,
				resCh: make(chan *result),
			}

			go work(it)  //create work
			itemCh <- it //send to fan-in thread for syncing results
		}
	}()

	//fan-in
	for it := range itemCh {
		if it.err != nil {
			return fmt.Errorf("failed to iterate: %v", it.err)
		}

		res := <-it.resCh
		if res.err != nil {
			return res.err
		}

		_, err = cw.Write(res.chunk)
		if err != nil {
			return fmt.Errorf("failed to write key: %v", err)
		}
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package rpc implements the Ethereum JSON-RPC API.
package rpc

import (
	"encoding/json"
	"fmt"
	"reflect"
	"sync/atomic"

	"github.com/ethereum/go-ethereum/rpc/comms"
	"github.com/ethereum/go-ethereum/rpc/shared"
)

// Xeth is a native API interface to a remote node.
type Xeth struct {
	client comms.EthereumClient
	reqId  uint32
}

// NewXeth constructs a new native API interface to a remote node.
func NewXeth(client comms.EthereumClient) *Xeth {
	return &Xeth{
		client: client,
	}
}

// Call invokes a method with the given parameters are the remote node.
func (self *Xeth) Call(method string, params []interface{}) (map[string]interface{}, error) {
	// Assemble the json RPC request
	data, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	req := &shared.Request{
		Id:      atomic.AddUint32(&self.reqId, 1),
		Jsonrpc: "2.0",
		Method:  method,
		Params:  data,
	}
	// Send the request over and process the response
	if err := self.client.Send(req); err != nil {
		return nil, err
	}
	res, err := self.client.Recv()
	if err != nil {
		return nil, err
	}
	value, ok := res.(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("Invalid response type: have %v, want %v", reflect.TypeOf(res), reflect.TypeOf(make(map[string]interface{})))
	}
	return value, nil
}
<commit_msg>rpc: update the xeth over RPC API to use the success/failure messages<commit_after>// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package rpc implements the Ethereum JSON-RPC API.
package rpc

import (
	"encoding/json"
	"fmt"
	"reflect"
	"sync/atomic"

	"github.com/ethereum/go-ethereum/rpc/comms"
	"github.com/ethereum/go-ethereum/rpc/shared"
)

// Xeth is a native API interface to a remote node.
type Xeth struct {
	client comms.EthereumClient
	reqId  uint32
}

// NewXeth constructs a new native API interface to a remote node.
func NewXeth(client comms.EthereumClient) *Xeth {
	return &Xeth{
		client: client,
	}
}

// Call invokes a method with the given parameters are the remote node.
func (self *Xeth) Call(method string, params []interface{}) (map[string]interface{}, error) {
	// Assemble the json RPC request
	data, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	req := &shared.Request{
		Id:      atomic.AddUint32(&self.reqId, 1),
		Jsonrpc: "2.0",
		Method:  method,
		Params:  data,
	}
	// Send the request over and retrieve the response
	if err := self.client.Send(req); err != nil {
		return nil, err
	}
	res, err := self.client.Recv()
	if err != nil {
		return nil, err
	}
	// Ensure the response is valid, and extract the results
	success, isSuccessResponse := res.(*shared.SuccessResponse)
	failure, isFailureResponse := res.(*shared.ErrorResponse)
	switch {
	case isFailureResponse:
		return nil, fmt.Errorf("Method invocation failed: %v", failure.Error)

	case isSuccessResponse:
		return success.Result.(map[string]interface{}), nil

	default:
		return nil, fmt.Errorf("Invalid response type: %v", reflect.TypeOf(res))
	}
}
<|endoftext|>


<commit_before>package density

import (
	"fmt"

	"github.com/phil-mansfield/gotetra/render/geom"
)

// This is a giant clusterfuck.
type Buffer interface {
	// Array Management
	Slice(low, high int)
	Length() int
	Clear()

	// Getters and Setters
	Quantity() Quantity
	SetGridLocation(g *geom.GridLocation)
	SetVectors(vecs []geom.Vec) bool

	// Buffer Retrieval
	CountBuffer() (num []int, ok bool)
	ScalarBuffer() (vals []float64, ok bool)
	VectorBuffer() (vals [][3]float64, ok bool)
	FinalizedScalarBuffer() (vals []float32, ok bool)
	FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool)
}

var NilBuffer = &scalarBuffer{ []float64{} }

func NewBuffer(q Quantity, len, wlen int, g *geom.GridLocation) Buffer {
	switch q {
	case Density:
		return &densityBuffer{
			scalarBuffer{ make([]float64, len) },
		}
	case DensityGradient:
		return &gradientBuffer{
			scalarBuffer{ make([]float64, len) }, g,
		}
	case Velocity:
		return &velocityBuffer{
			vectorBuffer{ make([][3]float64, len) },
			&vectorBuffer{ make([][3]float64, wlen) },
			make([]int, len),
		}
	case VelocityDivergence:
		return &divergenceBuffer{
			vectorBuffer{ make([][3]float64, len) },
			&vectorBuffer{ make([][3]float64, wlen) },
			make([]int, len),
			g,
		}
	case VelocityCurl:
		return &curlBuffer{
			vectorBuffer{ make([][3]float64, len) },
			&vectorBuffer{ make([][3]float64, wlen) },
			make([]int, len),
			g,
		}
	default:
		panic(fmt.Sprintf("Unrecognized Quantity %v", q))
	}
	panic(":3")
}

func WrapperDensityBuffer(rhos []float64) Buffer {
	return &densityBuffer{ scalarBuffer{ rhos } }
}

/////////////////////////////////
// scalarBuffer implementation //
/////////////////////////////////

type scalarBuffer struct { vals []float64 }

// Array Manipulation //

func (buf *scalarBuffer) Slice(low, high int) {
	buf.vals = buf.vals[low: high]
}

func (buf *scalarBuffer) Length() int {
	return len(buf.vals)
}

func (buf *scalarBuffer) Clear() {
	for i := range buf.vals { buf.vals[i] = 0 }
}

// Getters and Setters //

func (b *scalarBuffer) Quantity() Quantity {
	panic("Qunatity() called on raw scalarBuffer.")
}

func (b *scalarBuffer) SetGridLocation(g *geom.GridLocation) { }

func (b *scalarBuffer) SetVectors(vecs []geom.Vec) bool { return false }

// Buffer Retreival //

func (buf *scalarBuffer) CountBuffer() (num []int, ok bool) {
	return nil, false
}

func (buf *scalarBuffer) ScalarBuffer() (vals []float64, ok bool) {
	return buf.vals, true
}

func (buf *scalarBuffer) VectorBuffer() (vals [][3]float64, ok bool) {
	return nil, false
}

func (buf *scalarBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	vals32 := make([]float32, len(buf.vals))
	for i, x := range buf.vals { vals32[i] = float32(x) }
	return vals32, true
}

func (buf *scalarBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	return nil, nil, nil, false
}

/////////////////////////////////
// vectorBuffer implementation //
/////////////////////////////////

type vectorBuffer struct { vecs [][3]float64 }

// Array Manipulation //

func (buf *vectorBuffer) Slice(low, high int) {
	buf.vecs = buf.vecs[low: high]
}

func (buf *vectorBuffer) Length() int {
	return len(buf.vecs)
}

func (buf *vectorBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
	}
}

// Getters and Setters //

func (b *vectorBuffer) Quantity() Quantity {
	panic("Qunatity() called on raw vectorBuffer.")
}

func (b *vectorBuffer) SetGridLocation(g *geom.GridLocation) { }

func (b *vectorBuffer) SetVectors(vecs []geom.Vec) bool {
	for i := range vecs {
		for j := 0; j < 3; j++ { b.vecs[i][j] = float64(vecs[i][j]) }
	}
	return true
}

// Buffer Retrieval //

func (buf *vectorBuffer) CountBuffer() (num []int, ok bool) {
	return nil, false
}

func (buf *vectorBuffer) ScalarBuffer() (vals []float64, ok bool) {
	return nil, false
}

func (buf *vectorBuffer) VectorBuffer() (vals [][3]float64, ok bool) {
	return buf.vecs, true
}

func (buf *vectorBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	return nil, false
}

func (buf *vectorBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	xs = make([]float32, len(buf.vecs))
	ys = make([]float32, len(buf.vecs))
	zs = make([]float32, len(buf.vecs))
	for i, vec := range buf.vecs {
		xs[i], ys[i], zs[i] = float32(vec[0]), float32(vec[1]), float32(vec[2])
	}
	return xs, ys, zs, true
}

//////////////////////////////////
// densityBuffer implementation //
//////////////////////////////////

type densityBuffer struct { scalarBuffer }

// Array Manipulation //

// scalarBuffer.Length

// Getters and Setters //

func (b *densityBuffer) Quantity() Quantity { return Density }

// scalarBuffer.SetGridLocation

// scalarBuffer.SetVectors

// Buffer Retrieval //

///////////////////////////////////
// gradientBuffer implementation //
///////////////////////////////////

type gradientBuffer struct {
	scalarBuffer
	g *geom.GridLocation
}

// Array Manipulation //

// vectorBuffer.Length

// Getters and Setters //

func (b *gradientBuffer) Quantity() Quantity { return DensityGradient }

func (b *gradientBuffer) SetGridLocation(g *geom.GridLocation) { b.g = g }

// vectorBuffer.SetGridLocation

// vectorBuffer.SetVectors

// Buffer Retrieval //

func (buf *gradientBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	return nil, false
}

func (buf *gradientBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	vals := make([]float32, len(buf.vals))
	for i, x := range buf.vals { vals[i] = float32(x) }
	out := [3][]float32 {
		make([]float32, len(buf.vals)),
		make([]float32, len(buf.vals)),
		make([]float32, len(buf.vals)),
	}

	buf.g.Gradient(vals, out, &geom.DerivOptions{ true, geom.None, 4 })
	return out[0], out[1], out[2], true
}

/////////////////////////////////
// velocityBuffer implemtation //
/////////////////////////////////

type velocityBuffer struct {
	vectorBuffer
	weights *vectorBuffer
	num []int
}

// Array Manipulation //

func (buf *velocityBuffer) Slice(low, high int) {
	buf.vectorBuffer.Slice(low, high)
	buf.num = buf.num[low: high]
}

func (buf *velocityBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
		buf.num[i] = 0
	}
}

// vectorBuffer.Length

// Getters and Setters //

func (b *velocityBuffer) Quantity() Quantity { return Velocity }

// vectorBuffer.SetGridLocation

// vectorBuffer.SetVectors

// Buffer Retrieval //

func (buf *velocityBuffer) CountBuffer() (num []int, ok bool) {
	return buf.num, true
}

func (buf *velocityBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	xs = make([]float32, len(buf.vecs))
	ys = make([]float32, len(buf.vecs))
	zs = make([]float32, len(buf.vecs))
	for i, vec := range buf.vecs {
		if i % (len(buf.vecs) / 30) == 0 {
			fmt.Println(i, buf.num[i], vec[0], vec[1], vec[2])
		}
		n := float32(buf.num[i])
		if buf.num[i] == 0 { continue }
		xs[i], ys[i], zs[i] = float32(vec[0])/n, float32(vec[1])/n, float32(vec[2])/n
	}
	return xs, ys, zs, true
}

///////////////////////////////////
// divergenceBuffer implemtation //
///////////////////////////////////

type divergenceBuffer struct {
	vectorBuffer
	weights *vectorBuffer
	num []int
	g *geom.GridLocation
}

// Array Manipulation //

func (buf *divergenceBuffer) Slice(low, high int) {
	buf.vectorBuffer.Slice(low, high)
	buf.num = buf.num[low: high]
}

func (buf *divergenceBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
		buf.num[i] = 0
	}
}

// scalarBuffer.Length

// Getters and Setters //

func (b *divergenceBuffer) Quantity() Quantity { return VelocityDivergence }

func (b *divergenceBuffer) SetGridLocation(g *geom.GridLocation) { b.g = g }

// scalarBuffer.SetVectors

// Buffer Retrieval //

func (buf *divergenceBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	out := make([]float32, len(buf.vecs))
	vecs := [3][]float32 {
		make([]float32, len(buf.vecs)),
		make([]float32, len(buf.vecs)),
		make([]float32, len(buf.vecs)),
	}

	for i, vec := range buf.vecs {
		n := float32(buf.num[i])
		if buf.num[i] == 0 { continue }
		vecs[0][i] = float32(vec[0])/n
		vecs[1][i] = float32(vec[0])/n
		vecs[2][i] = float32(vec[2])/n
	}

	buf.g.Divergence(vecs, out, &geom.DerivOptions{ true, geom.None, 4 })

	return out, true
}

func (buf *divergenceBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	return nil, nil, nil, false
}

func (buf *divergenceBuffer) CountBuffer() (num []int, ok bool) {
	return buf.num, true
}

/////////////////////////////
// curlBuffer implemtation //
/////////////////////////////

type curlBuffer struct {
	vectorBuffer
	weights *vectorBuffer
	num []int
	g *geom.GridLocation
}

// Array Manipulation //

func (buf *curlBuffer) Slice(low, high int) {
	buf.vectorBuffer.Slice(low, high)
	buf.num = buf.num[low: high]
}

func (buf *curlBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
		buf.num[i] = 0
	}
}

// vectorBuffer.Length

// Getters and Setters //

func (b *curlBuffer) Quantity() Quantity { return VelocityCurl }

func (b *curlBuffer) SetGridLocation(g *geom.GridLocation) { b.g = g }

// vectorBuffer.SetVectors

// Buffer Retrieval //

func (buf *curlBuffer) CountBuffer() (num []int, ok bool) {
	return buf.num, true
}

func (buf *curlBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	xs, oxs := make([]float32, len(buf.vecs)), make([]float32, len(buf.vecs))
	ys, oys := make([]float32, len(buf.vecs)), make([]float32, len(buf.vecs))
	zs, ozs := make([]float32, len(buf.vecs)), make([]float32, len(buf.vecs))
	for i, vec := range buf.vecs {
		n := float32(buf.num[i])
		if buf.num[i] == 0 { continue }
		xs[i], ys[i], zs[i] = float32(vec[0])/n, float32(vec[1])/n, float32(vec[2])/n
	}
	vecs := [3][]float32{ xs, ys, zs }
	out := [3][]float32{ oxs, oys, ozs }
	buf.g.Curl(vecs, out, &geom.DerivOptions{ true, geom.None, 4})
	return oxs, oys, ozs, true
}
<commit_msg>Removed comment<commit_after>package density

import (
	"fmt"

	"github.com/phil-mansfield/gotetra/render/geom"
)

type Buffer interface {
	// Array Management
	Slice(low, high int)
	Length() int
	Clear()

	// Getters and Setters
	Quantity() Quantity
	SetGridLocation(g *geom.GridLocation)
	SetVectors(vecs []geom.Vec) bool

	// Buffer Retrieval
	CountBuffer() (num []int, ok bool)
	ScalarBuffer() (vals []float64, ok bool)
	VectorBuffer() (vals [][3]float64, ok bool)
	FinalizedScalarBuffer() (vals []float32, ok bool)
	FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool)
}

var NilBuffer = &scalarBuffer{ []float64{} }

func NewBuffer(q Quantity, len, wlen int, g *geom.GridLocation) Buffer {
	switch q {
	case Density:
		return &densityBuffer{
			scalarBuffer{ make([]float64, len) },
		}
	case DensityGradient:
		return &gradientBuffer{
			scalarBuffer{ make([]float64, len) }, g,
		}
	case Velocity:
		return &velocityBuffer{
			vectorBuffer{ make([][3]float64, len) },
			&vectorBuffer{ make([][3]float64, wlen) },
			make([]int, len),
		}
	case VelocityDivergence:
		return &divergenceBuffer{
			vectorBuffer{ make([][3]float64, len) },
			&vectorBuffer{ make([][3]float64, wlen) },
			make([]int, len),
			g,
		}
	case VelocityCurl:
		return &curlBuffer{
			vectorBuffer{ make([][3]float64, len) },
			&vectorBuffer{ make([][3]float64, wlen) },
			make([]int, len),
			g,
		}
	default:
		panic(fmt.Sprintf("Unrecognized Quantity %v", q))
	}
	panic(":3")
}

func WrapperDensityBuffer(rhos []float64) Buffer {
	return &densityBuffer{ scalarBuffer{ rhos } }
}

/////////////////////////////////
// scalarBuffer implementation //
/////////////////////////////////

type scalarBuffer struct { vals []float64 }

// Array Manipulation //

func (buf *scalarBuffer) Slice(low, high int) {
	buf.vals = buf.vals[low: high]
}

func (buf *scalarBuffer) Length() int {
	return len(buf.vals)
}

func (buf *scalarBuffer) Clear() {
	for i := range buf.vals { buf.vals[i] = 0 }
}

// Getters and Setters //

func (b *scalarBuffer) Quantity() Quantity {
	panic("Qunatity() called on raw scalarBuffer.")
}

func (b *scalarBuffer) SetGridLocation(g *geom.GridLocation) { }

func (b *scalarBuffer) SetVectors(vecs []geom.Vec) bool { return false }

// Buffer Retreival //

func (buf *scalarBuffer) CountBuffer() (num []int, ok bool) {
	return nil, false
}

func (buf *scalarBuffer) ScalarBuffer() (vals []float64, ok bool) {
	return buf.vals, true
}

func (buf *scalarBuffer) VectorBuffer() (vals [][3]float64, ok bool) {
	return nil, false
}

func (buf *scalarBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	vals32 := make([]float32, len(buf.vals))
	for i, x := range buf.vals { vals32[i] = float32(x) }
	return vals32, true
}

func (buf *scalarBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	return nil, nil, nil, false
}

/////////////////////////////////
// vectorBuffer implementation //
/////////////////////////////////

type vectorBuffer struct { vecs [][3]float64 }

// Array Manipulation //

func (buf *vectorBuffer) Slice(low, high int) {
	buf.vecs = buf.vecs[low: high]
}

func (buf *vectorBuffer) Length() int {
	return len(buf.vecs)
}

func (buf *vectorBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
	}
}

// Getters and Setters //

func (b *vectorBuffer) Quantity() Quantity {
	panic("Qunatity() called on raw vectorBuffer.")
}

func (b *vectorBuffer) SetGridLocation(g *geom.GridLocation) { }

func (b *vectorBuffer) SetVectors(vecs []geom.Vec) bool {
	for i := range vecs {
		for j := 0; j < 3; j++ { b.vecs[i][j] = float64(vecs[i][j]) }
	}
	return true
}

// Buffer Retrieval //

func (buf *vectorBuffer) CountBuffer() (num []int, ok bool) {
	return nil, false
}

func (buf *vectorBuffer) ScalarBuffer() (vals []float64, ok bool) {
	return nil, false
}

func (buf *vectorBuffer) VectorBuffer() (vals [][3]float64, ok bool) {
	return buf.vecs, true
}

func (buf *vectorBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	return nil, false
}

func (buf *vectorBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	xs = make([]float32, len(buf.vecs))
	ys = make([]float32, len(buf.vecs))
	zs = make([]float32, len(buf.vecs))
	for i, vec := range buf.vecs {
		xs[i], ys[i], zs[i] = float32(vec[0]), float32(vec[1]), float32(vec[2])
	}
	return xs, ys, zs, true
}

//////////////////////////////////
// densityBuffer implementation //
//////////////////////////////////

type densityBuffer struct { scalarBuffer }

// Array Manipulation //

// scalarBuffer.Length

// Getters and Setters //

func (b *densityBuffer) Quantity() Quantity { return Density }

// scalarBuffer.SetGridLocation

// scalarBuffer.SetVectors

// Buffer Retrieval //

///////////////////////////////////
// gradientBuffer implementation //
///////////////////////////////////

type gradientBuffer struct {
	scalarBuffer
	g *geom.GridLocation
}

// Array Manipulation //

// vectorBuffer.Length

// Getters and Setters //

func (b *gradientBuffer) Quantity() Quantity { return DensityGradient }

func (b *gradientBuffer) SetGridLocation(g *geom.GridLocation) { b.g = g }

// vectorBuffer.SetGridLocation

// vectorBuffer.SetVectors

// Buffer Retrieval //

func (buf *gradientBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	return nil, false
}

func (buf *gradientBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	vals := make([]float32, len(buf.vals))
	for i, x := range buf.vals { vals[i] = float32(x) }
	out := [3][]float32 {
		make([]float32, len(buf.vals)),
		make([]float32, len(buf.vals)),
		make([]float32, len(buf.vals)),
	}

	buf.g.Gradient(vals, out, &geom.DerivOptions{ true, geom.None, 4 })
	return out[0], out[1], out[2], true
}

/////////////////////////////////
// velocityBuffer implemtation //
/////////////////////////////////

type velocityBuffer struct {
	vectorBuffer
	weights *vectorBuffer
	num []int
}

// Array Manipulation //

func (buf *velocityBuffer) Slice(low, high int) {
	buf.vectorBuffer.Slice(low, high)
	buf.num = buf.num[low: high]
}

func (buf *velocityBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
		buf.num[i] = 0
	}
}

// vectorBuffer.Length

// Getters and Setters //

func (b *velocityBuffer) Quantity() Quantity { return Velocity }

// vectorBuffer.SetGridLocation

// vectorBuffer.SetVectors

// Buffer Retrieval //

func (buf *velocityBuffer) CountBuffer() (num []int, ok bool) {
	return buf.num, true
}

func (buf *velocityBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	xs = make([]float32, len(buf.vecs))
	ys = make([]float32, len(buf.vecs))
	zs = make([]float32, len(buf.vecs))
	for i, vec := range buf.vecs {
		if i % (len(buf.vecs) / 30) == 0 {
			fmt.Println(i, buf.num[i], vec[0], vec[1], vec[2])
		}
		n := float32(buf.num[i])
		if buf.num[i] == 0 { continue }
		xs[i], ys[i], zs[i] = float32(vec[0])/n, float32(vec[1])/n, float32(vec[2])/n
	}
	return xs, ys, zs, true
}

///////////////////////////////////
// divergenceBuffer implemtation //
///////////////////////////////////

type divergenceBuffer struct {
	vectorBuffer
	weights *vectorBuffer
	num []int
	g *geom.GridLocation
}

// Array Manipulation //

func (buf *divergenceBuffer) Slice(low, high int) {
	buf.vectorBuffer.Slice(low, high)
	buf.num = buf.num[low: high]
}

func (buf *divergenceBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
		buf.num[i] = 0
	}
}

// scalarBuffer.Length

// Getters and Setters //

func (b *divergenceBuffer) Quantity() Quantity { return VelocityDivergence }

func (b *divergenceBuffer) SetGridLocation(g *geom.GridLocation) { b.g = g }

// scalarBuffer.SetVectors

// Buffer Retrieval //

func (buf *divergenceBuffer) FinalizedScalarBuffer() (vals []float32, ok bool) {
	out := make([]float32, len(buf.vecs))
	vecs := [3][]float32 {
		make([]float32, len(buf.vecs)),
		make([]float32, len(buf.vecs)),
		make([]float32, len(buf.vecs)),
	}

	for i, vec := range buf.vecs {
		n := float32(buf.num[i])
		if buf.num[i] == 0 { continue }
		vecs[0][i] = float32(vec[0])/n
		vecs[1][i] = float32(vec[0])/n
		vecs[2][i] = float32(vec[2])/n
	}

	buf.g.Divergence(vecs, out, &geom.DerivOptions{ true, geom.None, 4 })

	return out, true
}

func (buf *divergenceBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	return nil, nil, nil, false
}

func (buf *divergenceBuffer) CountBuffer() (num []int, ok bool) {
	return buf.num, true
}

/////////////////////////////
// curlBuffer implemtation //
/////////////////////////////

type curlBuffer struct {
	vectorBuffer
	weights *vectorBuffer
	num []int
	g *geom.GridLocation
}

// Array Manipulation //

func (buf *curlBuffer) Slice(low, high int) {
	buf.vectorBuffer.Slice(low, high)
	buf.num = buf.num[low: high]
}

func (buf *curlBuffer) Clear() {
	for i := range buf.vecs {
		buf.vecs[i][0], buf.vecs[i][1], buf.vecs[i][2] = 0, 0, 0
		buf.num[i] = 0
	}
}

// vectorBuffer.Length

// Getters and Setters //

func (b *curlBuffer) Quantity() Quantity { return VelocityCurl }

func (b *curlBuffer) SetGridLocation(g *geom.GridLocation) { b.g = g }

// vectorBuffer.SetVectors

// Buffer Retrieval //

func (buf *curlBuffer) CountBuffer() (num []int, ok bool) {
	return buf.num, true
}

func (buf *curlBuffer) FinalizedVectorBuffer() (xs, ys, zs []float32, ok bool) {
	xs, oxs := make([]float32, len(buf.vecs)), make([]float32, len(buf.vecs))
	ys, oys := make([]float32, len(buf.vecs)), make([]float32, len(buf.vecs))
	zs, ozs := make([]float32, len(buf.vecs)), make([]float32, len(buf.vecs))
	for i, vec := range buf.vecs {
		n := float32(buf.num[i])
		if buf.num[i] == 0 { continue }
		xs[i], ys[i], zs[i] = float32(vec[0])/n, float32(vec[1])/n, float32(vec[2])/n
	}
	vecs := [3][]float32{ xs, ys, zs }
	out := [3][]float32{ oxs, oys, ozs }
	buf.g.Curl(vecs, out, &geom.DerivOptions{ true, geom.None, 4})
	return oxs, oys, ozs, true
}
<|endoftext|>


<commit_before>package kit

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

// ShopifyResponse is a general response for all server requests. It will format
// errors from any bad responses from the server. If the response is Successful()
// then the data item that you requested should be defined. If it was a theme request
// then Theme will be defined. If you have mad an asset query then Assets will be
// defined. If you did an action on a single asset then Asset will be defined.
type ShopifyResponse struct {
	Type      requestType  `json:"-"`
	Host      string       `json:"host"`
	URL       *url.URL     `json:"url"`
	Code      int          `json:"status_code"`
	Theme     Theme        `json:"theme"`
	Asset     Asset        `json:"asset"`
	Assets    []Asset      `json:"assets"`
	EventType EventType    `json:"event_type"`
	Errors    requestError `json:"errors"`
}

func newShopifyResponse(rtype requestType, event EventType, resp *http.Response, err error) (*ShopifyResponse, Error) {
	if resp == nil || err != nil {
		return nil, kitError{err}
	}
	defer resp.Body.Close()

	newResponse := &ShopifyResponse{
		Type:      rtype,
		Host:      resp.Request.URL.Host,
		URL:       resp.Request.URL,
		Code:      resp.StatusCode,
		EventType: event,
	}

	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, kitError{err}
	}

	err = json.Unmarshal(bytes, &newResponse)
	if err != nil {
		reqErr := generalRequestError{}
		json.Unmarshal(bytes, &reqErr)
		newResponse.Errors.Add(reqErr)
	}

	return newResponse, newResponse.Error()
}

// Successful will return true if the response code >= 200 and < 300 and if no
// errors were returned from the server.
func (resp ShopifyResponse) Successful() bool {
	return resp.Code >= 200 && resp.Code < 300 && !resp.Errors.Any()
}

func (resp ShopifyResponse) String() string {
	return fmt.Sprintf(`[%s] Performed %s at %s
	Request: %s
	Theme: %s
	Asset: %s
	Assets: %s
	Errors: %s`,
		RedText(resp.Code),
		YellowText(resp.EventType),
		YellowText(resp.Host),
		YellowText(resp.URL),
		YellowText(resp.Theme),
		YellowText(resp.Asset),
		YellowText(resp.Assets),
		resp.Errors,
	)
}

func (resp ShopifyResponse) Error() Error {
	if !resp.Successful() {
		switch resp.Type {
		case themeRequest:
			return newThemeError(resp)
		case assetRequest:
			return newAssetError(resp)
		case listRequest:
			return newListError(resp)
		default:
			return kitError{resp.Errors}
		}
	}
	return nil
}
<commit_msg>dont return nil for a shopify response<commit_after>package kit

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

// ShopifyResponse is a general response for all server requests. It will format
// errors from any bad responses from the server. If the response is Successful()
// then the data item that you requested should be defined. If it was a theme request
// then Theme will be defined. If you have mad an asset query then Assets will be
// defined. If you did an action on a single asset then Asset will be defined.
type ShopifyResponse struct {
	Type      requestType  `json:"-"`
	Host      string       `json:"host"`
	URL       *url.URL     `json:"url"`
	Code      int          `json:"status_code"`
	Theme     Theme        `json:"theme"`
	Asset     Asset        `json:"asset"`
	Assets    []Asset      `json:"assets"`
	EventType EventType    `json:"event_type"`
	Errors    requestError `json:"errors"`
}

func newShopifyResponse(rtype requestType, event EventType, resp *http.Response, err error) (*ShopifyResponse, Error) {
	if resp == nil || err != nil {
		return &ShopifyResponse{
			Type:      rtype,
			EventType: event,
		}, kitError{err}
	}
	defer resp.Body.Close()

	newResponse := &ShopifyResponse{
		Type:      rtype,
		Host:      resp.Request.URL.Host,
		URL:       resp.Request.URL,
		Code:      resp.StatusCode,
		EventType: event,
	}

	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return newResponse, kitError{err}
	}

	err = json.Unmarshal(bytes, &newResponse)
	if err != nil {
		reqErr := generalRequestError{}
		json.Unmarshal(bytes, &reqErr)
		newResponse.Errors.Add(reqErr)
	}

	return newResponse, newResponse.Error()
}

// Successful will return true if the response code >= 200 and < 300 and if no
// errors were returned from the server.
func (resp ShopifyResponse) Successful() bool {
	return resp.Code >= 200 && resp.Code < 300 && !resp.Errors.Any()
}

func (resp ShopifyResponse) String() string {
	return fmt.Sprintf(`[%s] Performed %s at %s
	Request: %s
	Theme: %s
	Asset: %s
	Assets: %s
	Errors: %s`,
		RedText(resp.Code),
		YellowText(resp.EventType),
		YellowText(resp.Host),
		YellowText(resp.URL),
		YellowText(resp.Theme),
		YellowText(resp.Asset),
		YellowText(resp.Assets),
		resp.Errors,
	)
}

func (resp ShopifyResponse) Error() Error {
	if !resp.Successful() {
		switch resp.Type {
		case themeRequest:
			return newThemeError(resp)
		case assetRequest:
			return newAssetError(resp)
		case listRequest:
			return newListError(resp)
		default:
			return kitError{resp.Errors}
		}
	}
	return nil
}
<|endoftext|>


<commit_before>// Copyright 2015 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)

package storage

import (
	"sort"
	"time"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/gossip"
	"github.com/cockroachdb/cockroach/proto"
	"github.com/cockroachdb/cockroach/util"
	"github.com/cockroachdb/cockroach/util/log"
)

const (
	// splitQueueMaxSize is the max size of the split queue.
	splitQueueMaxSize = 100
	// splitQueueTimerDuration is the duration between splits of queued ranges.
	splitQueueTimerDuration = 0 * time.Second // zero duration to process splits greedily.
)

// splitQueue manages a queue of ranges slated to be split due to size
// or along intersecting accounting or zone config boundaries.
type splitQueue struct {
	*baseQueue
	db     *client.DB
	gossip *gossip.Gossip
}

// newSplitQueue returns a new instance of splitQueue.
func newSplitQueue(db *client.KV, gossip *gossip.Gossip) *splitQueue {
	sq := &splitQueue{
		db:     db.NewDB(),
		gossip: gossip,
	}
	sq.baseQueue = newBaseQueue("split", sq, splitQueueMaxSize)
	return sq
}

func (sq *splitQueue) needsLeaderLease() bool {
	return true
}

// shouldQueue determines whether a range should be queued for
// splitting. This is true if the range is intersected by any
// accounting or zone config prefix or if the range's size in
// bytes exceeds the limit for the zone.
func (sq *splitQueue) shouldQueue(now proto.Timestamp, rng *Range) (shouldQ bool, priority float64) {
	// Set priority to 1 in the event the range is split by acct or zone configs.
	if len(computeSplitKeys(sq.gossip, rng)) > 0 {
		priority = 1
		shouldQ = true
	}

	// Add priority based on the size of range compared to the max
	// size for the zone it's in.
	zone, err := lookupZoneConfig(sq.gossip, rng)
	if err != nil {
		log.Error(err)
		return
	}
	if ratio := float64(rng.stats.GetSize()) / float64(zone.RangeMaxBytes); ratio > 1 {
		priority += ratio
		shouldQ = true
	}
	return
}

// process synchronously invokes admin split for each proposed split key.
func (sq *splitQueue) process(now proto.Timestamp, rng *Range) error {
	// First handle case of splitting due to accounting and zone config maps.
	splitKeys := computeSplitKeys(sq.gossip, rng)
	if len(splitKeys) > 0 {
		log.Infof("splitting %s at keys %v", rng, splitKeys)
		for _, splitKey := range splitKeys {
			if _, err := sq.db.AdminSplit(splitKey, splitKey); err != nil {
				return util.Errorf("unable to split %s at key %q: %s", rng, splitKey, err)
			}
		}
		return nil
	}
	// Next handle case of splitting due to size.
	zone, err := lookupZoneConfig(sq.gossip, rng)
	if err != nil {
		return err
	}
	if float64(rng.stats.GetSize())/float64(zone.RangeMaxBytes) > 1 {
		log.Infof("splitting %s size=%d max=%d", rng, rng.stats.GetSize(), zone.RangeMaxBytes)
		if err = rng.AddCmd(rng.context(),
			client.Call{
				Args: &proto.AdminSplitRequest{
					RequestHeader: proto.RequestHeader{Key: rng.Desc().StartKey},
				},
				Reply: &proto.AdminSplitResponse{},
			}, true); err != nil {
			return err
		}
	}
	return nil
}

// timer returns interval between processing successive queued splits.
func (sq *splitQueue) timer() time.Duration {
	return splitQueueTimerDuration
}

// computeSplitKeys returns an array of keys at which the supplied
// range should be split, as computed by intersecting the range with
// accounting and zone config map boundaries.
func computeSplitKeys(g *gossip.Gossip, rng *Range) []proto.Key {
	// Now split the range into pieces by intersecting it with the
	// boundaries of the config map.
	splitKeys := proto.KeySlice{}
	for _, configKey := range []string{gossip.KeyConfigAccounting, gossip.KeyConfigZone} {
		info, err := g.GetInfo(configKey)
		if err != nil {
			log.Errorf("unable to fetch %s config from gossip: %s", configKey, err)
			continue
		}
		configMap := info.(PrefixConfigMap)
		splits, err := configMap.SplitRangeByPrefixes(rng.Desc().StartKey, rng.Desc().EndKey)
		if err != nil {
			log.Errorf("unable to split %s by prefix map %s", rng, configMap)
			continue
		}
		// Gather new splits.
		for _, split := range splits {
			if split.end.Less(rng.Desc().EndKey) {
				splitKeys = append(splitKeys, split.end)
			}
		}
	}

	// Sort and unique the combined split keys from intersections with
	// both the accounting and zone config maps.
	sort.Sort(splitKeys)
	var unique []proto.Key
	for i, key := range splitKeys {
		if i == 0 || !key.Equal(splitKeys[i-1]) {
			unique = append(unique, key)
		}
	}
	return unique
}

// lookupZoneConfig returns the zone config matching the range.
func lookupZoneConfig(g *gossip.Gossip, rng *Range) (proto.ZoneConfig, error) {
	zoneMap, err := g.GetInfo(gossip.KeyConfigZone)
	if err != nil || zoneMap == nil {
		return proto.ZoneConfig{}, util.Errorf("unable to lookup zone config for range %s: %s", rng, err)
	}
	prefixConfig := zoneMap.(PrefixConfigMap).MatchByPrefix(rng.Desc().StartKey)
	return *prefixConfig.Config.(*proto.ZoneConfig), nil
}
<commit_msg>Add FIXME to `splitQueue.process`<commit_after>// Copyright 2015 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)

package storage

import (
	"sort"
	"time"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/gossip"
	"github.com/cockroachdb/cockroach/proto"
	"github.com/cockroachdb/cockroach/util"
	"github.com/cockroachdb/cockroach/util/log"
)

const (
	// splitQueueMaxSize is the max size of the split queue.
	splitQueueMaxSize = 100
	// splitQueueTimerDuration is the duration between splits of queued ranges.
	splitQueueTimerDuration = 0 * time.Second // zero duration to process splits greedily.
)

// splitQueue manages a queue of ranges slated to be split due to size
// or along intersecting accounting or zone config boundaries.
type splitQueue struct {
	*baseQueue
	db     *client.DB
	gossip *gossip.Gossip
}

// newSplitQueue returns a new instance of splitQueue.
func newSplitQueue(db *client.KV, gossip *gossip.Gossip) *splitQueue {
	sq := &splitQueue{
		db:     db.NewDB(),
		gossip: gossip,
	}
	sq.baseQueue = newBaseQueue("split", sq, splitQueueMaxSize)
	return sq
}

func (sq *splitQueue) needsLeaderLease() bool {
	return true
}

// shouldQueue determines whether a range should be queued for
// splitting. This is true if the range is intersected by any
// accounting or zone config prefix or if the range's size in
// bytes exceeds the limit for the zone.
func (sq *splitQueue) shouldQueue(now proto.Timestamp, rng *Range) (shouldQ bool, priority float64) {
	// Set priority to 1 in the event the range is split by acct or zone configs.
	if len(computeSplitKeys(sq.gossip, rng)) > 0 {
		priority = 1
		shouldQ = true
	}

	// Add priority based on the size of range compared to the max
	// size for the zone it's in.
	zone, err := lookupZoneConfig(sq.gossip, rng)
	if err != nil {
		log.Error(err)
		return
	}
	if ratio := float64(rng.stats.GetSize()) / float64(zone.RangeMaxBytes); ratio > 1 {
		priority += ratio
		shouldQ = true
	}
	return
}

// process synchronously invokes admin split for each proposed split key.
func (sq *splitQueue) process(now proto.Timestamp, rng *Range) error {
	// First handle case of splitting due to accounting and zone config maps.
	splitKeys := computeSplitKeys(sq.gossip, rng)
	if len(splitKeys) > 0 {
		log.Infof("splitting %s at keys %v", rng, splitKeys)
		for _, splitKey := range splitKeys {
			if _, err := sq.db.AdminSplit(splitKey, splitKey); err != nil {
				return util.Errorf("unable to split %s at key %q: %s", rng, splitKey, err)
			}
		}
		return nil
	}
	// Next handle case of splitting due to size.
	zone, err := lookupZoneConfig(sq.gossip, rng)
	if err != nil {
		return err
	}
	// FIXME: why is this implementation not the same as the one above?
	if float64(rng.stats.GetSize())/float64(zone.RangeMaxBytes) > 1 {
		log.Infof("splitting %s size=%d max=%d", rng, rng.stats.GetSize(), zone.RangeMaxBytes)
		if err = rng.AddCmd(rng.context(),
			client.Call{
				Args: &proto.AdminSplitRequest{
					RequestHeader: proto.RequestHeader{Key: rng.Desc().StartKey},
				},
				Reply: &proto.AdminSplitResponse{},
			}, true); err != nil {
			return err
		}
	}
	return nil
}

// timer returns interval between processing successive queued splits.
func (sq *splitQueue) timer() time.Duration {
	return splitQueueTimerDuration
}

// computeSplitKeys returns an array of keys at which the supplied
// range should be split, as computed by intersecting the range with
// accounting and zone config map boundaries.
func computeSplitKeys(g *gossip.Gossip, rng *Range) []proto.Key {
	// Now split the range into pieces by intersecting it with the
	// boundaries of the config map.
	splitKeys := proto.KeySlice{}
	for _, configKey := range []string{gossip.KeyConfigAccounting, gossip.KeyConfigZone} {
		info, err := g.GetInfo(configKey)
		if err != nil {
			log.Errorf("unable to fetch %s config from gossip: %s", configKey, err)
			continue
		}
		configMap := info.(PrefixConfigMap)
		splits, err := configMap.SplitRangeByPrefixes(rng.Desc().StartKey, rng.Desc().EndKey)
		if err != nil {
			log.Errorf("unable to split %s by prefix map %s", rng, configMap)
			continue
		}
		// Gather new splits.
		for _, split := range splits {
			if split.end.Less(rng.Desc().EndKey) {
				splitKeys = append(splitKeys, split.end)
			}
		}
	}

	// Sort and unique the combined split keys from intersections with
	// both the accounting and zone config maps.
	sort.Sort(splitKeys)
	var unique []proto.Key
	for i, key := range splitKeys {
		if i == 0 || !key.Equal(splitKeys[i-1]) {
			unique = append(unique, key)
		}
	}
	return unique
}

// lookupZoneConfig returns the zone config matching the range.
func lookupZoneConfig(g *gossip.Gossip, rng *Range) (proto.ZoneConfig, error) {
	zoneMap, err := g.GetInfo(gossip.KeyConfigZone)
	if err != nil || zoneMap == nil {
		return proto.ZoneConfig{}, util.Errorf("unable to lookup zone config for range %s: %s", rng, err)
	}
	prefixConfig := zoneMap.(PrefixConfigMap).MatchByPrefix(rng.Desc().StartKey)
	return *prefixConfig.Config.(*proto.ZoneConfig), nil
}
<|endoftext|>


<commit_before>package fcomposite

import(
    "fmt"
    "bytes"
    "net/http"
    "github.com/ricallinson/forgery"
)

// The Writer used in place of stackr.Writer for buffering.
type BufferedResponseWriter struct {
    Headers http.Header
    Buffer *bytes.Buffer
    Status int
}

// Header returns the header map that will be sent by WriteHeader.
// Changing the header after a call to WriteHeader (or Write) has
// no effect.
func (this *BufferedResponseWriter) Header() (http.Header) {
    if this.Headers == nil {
        this.Headers = http.Header{}
    }
    return this.Headers
}

// Write writes the data to the connection as part of an HTTP reply.
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
// before writing the data.  If the Header does not contain a
// Content-Type line, Write adds a Content-Type set to the result of passing
// the initial 512 bytes of written data to DetectContentType.
func (this *BufferedResponseWriter) Write(b []byte) (int, error) {
    if this.Buffer == nil {
        this.Buffer = &bytes.Buffer{}
    }
    len, err := this.Buffer.Write(b)
    fmt.Println(string(this.Buffer.Bytes()))
    return len, err
}

// WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
// Thus explicit calls to WriteHeader are mainly used to
// send error codes.
func (this *BufferedResponseWriter) WriteHeader(code int) {
    this.Status = code
}

/*
    composite := fcomposite.Map{
        "header": func(req, res, next) {
            res.Send("Header string")
        },
        "body": func(req, res, next) {
            res.Render("page.html", "Body string")
        },
        "footer": func(req, res, next) {
            res.End("Footer string")
        },
        "tail": func(req, res, next) {
            res.Write("Tail string")
        },
        "close": func(req, res, next) {
            res.WriteBytes([]byte("Close string"))
        },
    }

    data := composite.Dispatch(req, res, next)
*/
type Map map[string]func(*f.Request, *f.Response, func())

/*
    The worker.
*/
func (this Map) Dispatch(req *f.Request, res *f.Response, next func()) (map[string]string) {

    done := map[string]string{}

    // Grab the res.Writer so we can put it back later.
    w := res.Response.Writer

    c := make(chan int, len(this))
    for id, fn := range this {
        go func(mapId string, mapFn func(*f.Request, *f.Response, func())) {
            // Clone the res so it can be changed in isolation.
            response := res.Clone(req)
            // Create a buffer.
            buffer := &BufferedResponseWriter{}
            // Replace res.Writer with BufferedResponseWriter so all the output can be captured.
            response.Response.Writer = buffer
            // Call the function.
            mapFn(req, response, next)
            // Add the buffered data to the done map.
            if buffer.Buffer != nil {
                done[mapId] = buffer.Buffer.String()
            }
            // TODO: Transfer headers to the real Response
            // ...
            // Return the channel
            c <- 1
        }(id, fn)
    }
    // Wait for all the channels to close.
    <-c

    // Put the res.Writer back.
    res.Response.Writer = w

    return done
}<commit_msg>Cleaned up bad idea<commit_after>package fcomposite

import(
    "fmt"
    "bytes"
    "net/http"
    "github.com/ricallinson/forgery"
)

// The Writer used in place of stackr.Writer for buffering.
type BufferedResponseWriter struct {
    Headers http.Header
    Buffer *bytes.Buffer
    Status int
}

// Header returns the header map that will be sent by WriteHeader.
// Changing the header after a call to WriteHeader (or Write) has
// no effect.
func (this *BufferedResponseWriter) Header() (http.Header) {
    if this.Headers == nil {
        this.Headers = http.Header{}
    }
    return this.Headers
}

// Write writes the data to the connection as part of an HTTP reply.
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
// before writing the data.  If the Header does not contain a
// Content-Type line, Write adds a Content-Type set to the result of passing
// the initial 512 bytes of written data to DetectContentType.
func (this *BufferedResponseWriter) Write(b []byte) (int, error) {
    if this.Buffer == nil {
        this.Buffer = &bytes.Buffer{}
    }
    len, err := this.Buffer.Write(b)
    fmt.Println(string(this.Buffer.Bytes()))
    return len, err
}

// WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
// Thus explicit calls to WriteHeader are mainly used to
// send error codes.
func (this *BufferedResponseWriter) WriteHeader(code int) {
    this.Status = code
}

/*
    composite := fcomposite.Map{
        "header": func(req, res, next) {
            res.Send("Header string")
        },
        "body": func(req, res, next) {
            res.Render("page.html", "Body string")
        },
        "footer": func(req, res, next) {
            res.End("Footer string")
        },
        "tail": func(req, res, next) {
            res.Write("Tail string")
        },
        "close": func(req, res, next) {
            res.WriteBytes([]byte("Close string"))
        },
    }

    data := composite.Dispatch(req, res, next)
*/
type Map map[string]func(*f.Request, *f.Response, func())

/*
    The worker.
*/
func (this Map) Dispatch(req *f.Request, res *f.Response, next func()) (map[string]string) {

    done := map[string]string{}

    // Grab the res.Writer so we can put it back later.
    w := res.Response.Writer

    c := make(chan int, len(this))
    for id, fn := range this {
        go func(mapId string, mapFn func(*f.Request, *f.Response, func())) {
            // Clone the res so it can be changed in isolation.
            response := res.Clone()
            // Create a buffer.
            buffer := &BufferedResponseWriter{}
            // Replace res.Writer with BufferedResponseWriter so all the output can be captured.
            response.Response.Writer = buffer
            // Call the function.
            mapFn(req, response, next)
            // Add the buffered data to the done map.
            if buffer.Buffer != nil {
                done[mapId] = buffer.Buffer.String()
            }
            // TODO: Transfer headers to the real Response
            // ...
            // Return the channel
            c <- 1
        }(id, fn)
    }
    // Wait for all the channels to close.
    <-c

    // Put the res.Writer back.
    res.Response.Writer = w

    return done
}<|endoftext|>


<commit_before>package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"

	e "github.com/techjanitor/pram-get/errors"
	"github.com/techjanitor/pram-get/models"
)

// TagsController handles tags pages
func TagsController(c *gin.Context) {

	// Get parameters from validate middleware
	params := c.MustGet("params").([]uint)

	// get search query if its there
	search := c.Query("search")

	fmt.Println(search)

	// Initialize model struct
	m := &models.TagsModel{
		Ib:   params[0],
		Term: search,
	}

	// Get the model which outputs JSON
	err := m.Get()
	if err == e.ErrNotFound {
		c.Set("controllerError", err)
		c.JSON(e.ErrorMessage(e.ErrNotFound))
		c.Error(err)
		return
	}
	if err != nil {
		c.Set("controllerError", err)
		c.JSON(e.ErrorMessage(e.ErrInternalError))
		c.Error(err)
		return
	}

	// Marshal the structs into JSON
	output, err := json.Marshal(m.Result)
	if err != nil {
		c.Set("controllerError", err)
		c.JSON(e.ErrorMessage(e.ErrInternalError))
		c.Error(err)
		return
	}

	// Hand off data to cache middleware
	c.Set("data", output)

	c.Writer.Header().Set("Content-Type", "application/json")
	c.Writer.Write(output)

	return

}
<commit_msg>add search ability to tags<commit_after>package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"

	e "github.com/techjanitor/pram-get/errors"
	"github.com/techjanitor/pram-get/models"
)

// TagsController handles tags pages
func TagsController(c *gin.Context) {

	// Get parameters from validate middleware
	params := c.MustGet("params").([]uint)

	// get search query if its there
	search := c.Query("search")

	fmt.Println(c.Query)

	// Initialize model struct
	m := &models.TagsModel{
		Ib:   params[0],
		Term: search,
	}

	// Get the model which outputs JSON
	err := m.Get()
	if err == e.ErrNotFound {
		c.Set("controllerError", err)
		c.JSON(e.ErrorMessage(e.ErrNotFound))
		c.Error(err)
		return
	}
	if err != nil {
		c.Set("controllerError", err)
		c.JSON(e.ErrorMessage(e.ErrInternalError))
		c.Error(err)
		return
	}

	// Marshal the structs into JSON
	output, err := json.Marshal(m.Result)
	if err != nil {
		c.Set("controllerError", err)
		c.JSON(e.ErrorMessage(e.ErrInternalError))
		c.Error(err)
		return
	}

	// Hand off data to cache middleware
	c.Set("data", output)

	c.Writer.Header().Set("Content-Type", "application/json")
	c.Writer.Write(output)

	return

}
<|endoftext|>


<commit_before>package swarm

import (
	"bytes"
	"fmt"
	"testing"
	"time"

	ci "github.com/jbenet/go-ipfs/crypto"
	msg "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)

func pong(ctx context.Context, swarm *Swarm) {
	for {
		select {
		case <-ctx.Done():
			return
		case m1 := <-swarm.Incoming:
			if bytes.Equal(m1.Data(), []byte("ping")) {
				m2 := msg.New(m1.Peer(), []byte("pong"))
				swarm.Outgoing <- m2
			}
		}
	}
}

func setupPeer(t *testing.T, id string, addr string) *peer.Peer {
	tcp, err := ma.NewMultiaddr(addr)
	if err != nil {
		t.Fatal(err)
	}

	mh, err := mh.FromHexString(id)
	if err != nil {
		t.Fatal(err)
	}

	p := &peer.Peer{ID: peer.ID(mh)}

	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
	if err != nil {
		t.Fatal(err)
	}
	p.PrivKey = sk
	p.PubKey = pk

	p.AddAddress(tcp)
	return p
}

func makeSwarms(ctx context.Context, t *testing.T, peers map[string]string) []*Swarm {
	swarms := []*Swarm{}

	for key, addr := range peers {
		local := setupPeer(t, key, addr)
		peerstore := peer.NewPeerstore()
		swarm, err := NewSwarm(ctx, local, peerstore)
		if err != nil {
			t.Fatal(err)
		}
		swarms = append(swarms, swarm)
	}

	return swarms
}

func TestSwarm(t *testing.T) {
	peers := map[string]string{
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a30": "/ip4/127.0.0.1/tcp/1234",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31": "/ip4/127.0.0.1/tcp/2345",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32": "/ip4/127.0.0.1/tcp/3456",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33": "/ip4/127.0.0.1/tcp/4567",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34": "/ip4/127.0.0.1/tcp/5678",
	}

	ctx := context.Background()
	swarms := makeSwarms(ctx, t, peers)

	// connect everyone
	for _, s := range swarms {
		peers, err := s.peers.All()
		if err != nil {
			t.Fatal(err)
		}

		for _, p := range *peers {
			fmt.Println("dialing")
			if _, err := s.Dial(p); err != nil {
				t.Fatal("error swarm dialing to peer", err)
			}
			fmt.Println("dialed")
		}
	}

	// ping/pong
	for _, s1 := range swarms {
		ctx, cancel := context.WithCancel(ctx)

		// setup all others to pong
		for _, s2 := range swarms {
			if s1 == s2 {
				continue
			}

			go pong(ctx, s2)
		}

		peers, err := s1.peers.All()
		if err != nil {
			t.Fatal(err)
		}

		MsgNum := 1000
		for k := 0; k < MsgNum; k++ {
			for _, p := range *peers {
				s1.Outgoing <- msg.New(p, []byte("ping"))
			}
		}

		got := map[u.Key]int{}
		for k := 0; k < (MsgNum * len(*peers)); k++ {
			msg := <-s1.Incoming
			if string(msg.Data()) != "pong" {
				t.Error("unexpected conn output", msg.Data)
			}

			n, _ := got[msg.Peer().Key()]
			got[msg.Peer().Key()] = n + 1
		}

		if len(*peers) != len(got) {
			t.Error("got less messages than sent")
		}

		for p, n := range got {
			if n != MsgNum {
				t.Error("peer did not get all msgs", p, n, "/", MsgNum)
			}
		}

		cancel()
		<-time.After(50 * time.Millisecond)
	}

	for _, s := range swarms {
		s.Close()
	}
}
<commit_msg>better peer gen<commit_after>package swarm

import (
	"bytes"
	"fmt"
	"testing"
	"time"

	ci "github.com/jbenet/go-ipfs/crypto"
	msg "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)

func pong(ctx context.Context, swarm *Swarm) {
	for {
		select {
		case <-ctx.Done():
			return
		case m1 := <-swarm.Incoming:
			if bytes.Equal(m1.Data(), []byte("ping")) {
				m2 := msg.New(m1.Peer(), []byte("pong"))
				swarm.Outgoing <- m2
			}
		}
	}
}

func setupPeer(t *testing.T, addr string) *peer.Peer {
	tcp, err := ma.NewMultiaddr(addr)
	if err != nil {
		t.Fatal(err)
	}

	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
	if err != nil {
		t.Fatal(err)
	}

	id, err := peer.IDFromPubKey(pk)
	if err != nil {
		t.Fatal(err)
	}

	p := &peer.Peer{ID: id}
	p.PrivKey = sk
	p.PubKey = pk
	p.AddAddress(tcp)
	return p, nil
}

func makeSwarms(ctx context.Context, t *testing.T, peers map[string]string) []*Swarm {
	swarms := []*Swarm{}

	for key, addr := range peers {
		local := setupPeer(t, addr)
		peerstore := peer.NewPeerstore()
		swarm, err := NewSwarm(ctx, local, peerstore)
		if err != nil {
			t.Fatal(err)
		}
		swarms = append(swarms, swarm)
	}

	return swarms
}

func TestSwarm(t *testing.T) {
	peers := map[string]string{
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a30": "/ip4/127.0.0.1/tcp/1234",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31": "/ip4/127.0.0.1/tcp/2345",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32": "/ip4/127.0.0.1/tcp/3456",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33": "/ip4/127.0.0.1/tcp/4567",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34": "/ip4/127.0.0.1/tcp/5678",
	}

	ctx := context.Background()
	swarms := makeSwarms(ctx, t, peers)

	// connect everyone
	for _, s := range swarms {
		peers, err := s.peers.All()
		if err != nil {
			t.Fatal(err)
		}

		for _, p := range *peers {
			fmt.Println("dialing")
			if _, err := s.Dial(p); err != nil {
				t.Fatal("error swarm dialing to peer", err)
			}
			fmt.Println("dialed")
		}
	}

	// ping/pong
	for _, s1 := range swarms {
		ctx, cancel := context.WithCancel(ctx)

		// setup all others to pong
		for _, s2 := range swarms {
			if s1 == s2 {
				continue
			}

			go pong(ctx, s2)
		}

		peers, err := s1.peers.All()
		if err != nil {
			t.Fatal(err)
		}

		MsgNum := 1000
		for k := 0; k < MsgNum; k++ {
			for _, p := range *peers {
				s1.Outgoing <- msg.New(p, []byte("ping"))
			}
		}

		got := map[u.Key]int{}
		for k := 0; k < (MsgNum * len(*peers)); k++ {
			msg := <-s1.Incoming
			if string(msg.Data()) != "pong" {
				t.Error("unexpected conn output", msg.Data)
			}

			n, _ := got[msg.Peer().Key()]
			got[msg.Peer().Key()] = n + 1
		}

		if len(*peers) != len(got) {
			t.Error("got less messages than sent")
		}

		for p, n := range got {
			if n != MsgNum {
				t.Error("peer did not get all msgs", p, n, "/", MsgNum)
			}
		}

		cancel()
		<-time.After(50 * time.Millisecond)
	}

	for _, s := range swarms {
		s.Close()
	}
}
<|endoftext|>