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

package quota

import (
	"fmt"
	"github.com/globocom/config"
	"github.com/globocom/tsuru/db"
	"labix.org/v2/mgo/bson"
	"launchpad.net/gocheck"
	"runtime"
	"sync"
	"testing"
)

func Test(t *testing.T) { gocheck.TestingT(t) }

var _ = gocheck.Suite(Suite{})

type Suite struct{}

func (Suite) SetUpSuite(c *gocheck.C) {
	config.Set("database:url", "127.0.0.1:27017")
	config.Set("database:name", "tsuru_quota_tests")
}

func (Suite) TearDownSuite(c *gocheck.C) {
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	conn.Apps().Database.DropDatabase()
}

func (Suite) TestCreate(c *gocheck.C) {
	err := Create("user@tsuru.io", 10)
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "user@tsuru.io"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	defer conn.Quota().Remove(bson.M{"user": "user@tsuru.io"})
	c.Assert(u.User, gocheck.Equals, "user@tsuru.io")
	c.Assert(u.Limit, gocheck.Equals, uint(10))
	c.Assert(u.Items, gocheck.HasLen, 0)
}

func (Suite) TestDuplicateQuota(c *gocheck.C) {
	err := Create("user@tsuru.io", 10)
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	defer conn.Quota().Remove(bson.M{"user": "user@tsuru.io"})
	err = Create("user@tsuru.io", 50)
	c.Assert(err, gocheck.Equals, ErrQuotaAlreadyExists)
}

func (Suite) TestDelete(c *gocheck.C) {
	err := Create("home@dreamtheater.com", 3)
	c.Assert(err, gocheck.IsNil)
	err = Delete("home@dreamtheater.com")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	count, err := conn.Quota().Find(bson.M{"user": "home@dreamtheater.com"}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(count, gocheck.Equals, 0)
}

func (Suite) TestDeleteQuotaNotFound(c *gocheck.C) {
	err := Delete("home@dreamtheater.com")
	c.Assert(err, gocheck.Equals, ErrQuotaNotFound)
}

func (Suite) TestReserve(c *gocheck.C) {
	err := Create("last@dreamtheater.com", 3)
	c.Assert(err, gocheck.IsNil)
	defer Delete("last@dreamtheater.com")
	err = Reserve("last@dreamtheater.com", "dt/1")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("last@dreamtheater.com", "dt/0")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "last@dreamtheater.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.DeepEquals, []string{"dt/1", "dt/0"})
}

func (Suite) TestReserveIsSafe(c *gocheck.C) {
	items := 300
	err := Create("spirit@dreamtheater.com", uint(items-items/2))
	c.Assert(err, gocheck.IsNil)
	defer Delete("spirit@dreamtheater.com")
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(24))
	var wg sync.WaitGroup
	wg.Add(items)
	for i := 0; i < items; i++ {
		go func(i int) {
			Reserve("spirit@dreamtheater.com", fmt.Sprintf("spirit/%d", i))
			wg.Done()
		}(i)
	}
	wg.Wait()
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "spirit@dreamtheater.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.HasLen, items-items/2)
}

func (Suite) TestReserveRepeatedItems(c *gocheck.C) {
	err := Create("spirit@dreamtheater.com", 500)
	c.Assert(err, gocheck.IsNil)
	defer Delete("spirit@dreamtheater.com")
	err = Reserve("spirit@dreamtheater.com", "spirit/0")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("spirit@dreamtheater.com", "spirit/0")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "spirit@dreamtheater.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.HasLen, 1)
}

func (Suite) TestReserveQuotaExceeded(c *gocheck.C) {
	err := Create("change@dreamtheater.com", 1)
	c.Assert(err, gocheck.IsNil)
	defer Delete("change@dreamtheater.com")
	err = Reserve("change@dreamtheater.com", "change/0")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("change@dreamtheater.com", "change/1")
	c.Assert(err, gocheck.Equals, ErrQuotaExceeded)
}

func (Suite) TestReserveQuotaNotFound(c *gocheck.C) {
	err := Reserve("home@dreamtheater.com", "something")
	c.Assert(err, gocheck.Equals, ErrQuotaNotFound)
}

func (Suite) TestRelease(c *gocheck.C) {
	err := Create("beyond@yes.com", 1)
	c.Assert(err, gocheck.IsNil)
	defer Delete("beyond@yes.com")
	err = Reserve("beyond@yes.com", "beyond/0")
	c.Assert(err, gocheck.IsNil)
	err = Release("beyond@yes.com", "beyond/0")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("beyond@yes.com", "beyond/1")
	c.Assert(err, gocheck.IsNil)
}

func (Suite) TestReleaseQuotaNotFound(c *gocheck.C) {
	err := Release("see@yes.com", "see/0")
	c.Assert(err, gocheck.Equals, ErrQuotaNotFound)
}

func (Suite) TestReleaseIsSafe(c *gocheck.C) {
	items := 100
	err := Create("looking@yes.com", uint(items))
	c.Assert(err, gocheck.IsNil)
	defer Delete("looking@yes.com")
	for i := 0; i < items; i++ {
		Reserve("looking@yes.com", fmt.Sprintf("looking/%d", i))
	}
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(16))
	var wg sync.WaitGroup
	for i := 0; i < items+items/2; i++ {
		wg.Add(1)
		go func(i int) {
			err := Release("looking@yes.com", fmt.Sprintf("looking/%d", i))
			c.Check(err, gocheck.IsNil)
			wg.Done()
		}(i)
	}
	wg.Wait()
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "looking@yes.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.HasLen, 0)
}
<commit_msg>quota: reduce parallelism in tests to make them more reliable<commit_after>// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package quota

import (
	"fmt"
	"github.com/globocom/config"
	"github.com/globocom/tsuru/db"
	"labix.org/v2/mgo/bson"
	"launchpad.net/gocheck"
	"runtime"
	"sync"
	"testing"
)

func Test(t *testing.T) { gocheck.TestingT(t) }

var _ = gocheck.Suite(Suite{})

type Suite struct{}

func (Suite) SetUpSuite(c *gocheck.C) {
	config.Set("database:url", "127.0.0.1:27017")
	config.Set("database:name", "tsuru_quota_tests")
}

func (Suite) TearDownSuite(c *gocheck.C) {
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	conn.Apps().Database.DropDatabase()
}

func (Suite) TestCreate(c *gocheck.C) {
	err := Create("user@tsuru.io", 10)
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "user@tsuru.io"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	defer conn.Quota().Remove(bson.M{"user": "user@tsuru.io"})
	c.Assert(u.User, gocheck.Equals, "user@tsuru.io")
	c.Assert(u.Limit, gocheck.Equals, uint(10))
	c.Assert(u.Items, gocheck.HasLen, 0)
}

func (Suite) TestDuplicateQuota(c *gocheck.C) {
	err := Create("user@tsuru.io", 10)
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	defer conn.Quota().Remove(bson.M{"user": "user@tsuru.io"})
	err = Create("user@tsuru.io", 50)
	c.Assert(err, gocheck.Equals, ErrQuotaAlreadyExists)
}

func (Suite) TestDelete(c *gocheck.C) {
	err := Create("home@dreamtheater.com", 3)
	c.Assert(err, gocheck.IsNil)
	err = Delete("home@dreamtheater.com")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	count, err := conn.Quota().Find(bson.M{"user": "home@dreamtheater.com"}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(count, gocheck.Equals, 0)
}

func (Suite) TestDeleteQuotaNotFound(c *gocheck.C) {
	err := Delete("home@dreamtheater.com")
	c.Assert(err, gocheck.Equals, ErrQuotaNotFound)
}

func (Suite) TestReserve(c *gocheck.C) {
	err := Create("last@dreamtheater.com", 3)
	c.Assert(err, gocheck.IsNil)
	defer Delete("last@dreamtheater.com")
	err = Reserve("last@dreamtheater.com", "dt/1")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("last@dreamtheater.com", "dt/0")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "last@dreamtheater.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.DeepEquals, []string{"dt/1", "dt/0"})
}

func (Suite) TestReserveIsSafe(c *gocheck.C) {
	items := 300
	err := Create("spirit@dreamtheater.com", uint(items-items/2))
	c.Assert(err, gocheck.IsNil)
	defer Delete("spirit@dreamtheater.com")
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(8))
	var wg sync.WaitGroup
	wg.Add(items)
	for i := 0; i < items; i++ {
		go func(i int) {
			Reserve("spirit@dreamtheater.com", fmt.Sprintf("spirit/%d", i))
			wg.Done()
		}(i)
	}
	wg.Wait()
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "spirit@dreamtheater.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.HasLen, items-items/2)
}

func (Suite) TestReserveRepeatedItems(c *gocheck.C) {
	err := Create("spirit@dreamtheater.com", 500)
	c.Assert(err, gocheck.IsNil)
	defer Delete("spirit@dreamtheater.com")
	err = Reserve("spirit@dreamtheater.com", "spirit/0")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("spirit@dreamtheater.com", "spirit/0")
	c.Assert(err, gocheck.IsNil)
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "spirit@dreamtheater.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.HasLen, 1)
}

func (Suite) TestReserveQuotaExceeded(c *gocheck.C) {
	err := Create("change@dreamtheater.com", 1)
	c.Assert(err, gocheck.IsNil)
	defer Delete("change@dreamtheater.com")
	err = Reserve("change@dreamtheater.com", "change/0")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("change@dreamtheater.com", "change/1")
	c.Assert(err, gocheck.Equals, ErrQuotaExceeded)
}

func (Suite) TestReserveQuotaNotFound(c *gocheck.C) {
	err := Reserve("home@dreamtheater.com", "something")
	c.Assert(err, gocheck.Equals, ErrQuotaNotFound)
}

func (Suite) TestRelease(c *gocheck.C) {
	err := Create("beyond@yes.com", 1)
	c.Assert(err, gocheck.IsNil)
	defer Delete("beyond@yes.com")
	err = Reserve("beyond@yes.com", "beyond/0")
	c.Assert(err, gocheck.IsNil)
	err = Release("beyond@yes.com", "beyond/0")
	c.Assert(err, gocheck.IsNil)
	err = Reserve("beyond@yes.com", "beyond/1")
	c.Assert(err, gocheck.IsNil)
}

func (Suite) TestReleaseQuotaNotFound(c *gocheck.C) {
	err := Release("see@yes.com", "see/0")
	c.Assert(err, gocheck.Equals, ErrQuotaNotFound)
}

func (Suite) TestReleaseIsSafe(c *gocheck.C) {
	items := 100
	err := Create("looking@yes.com", uint(items))
	c.Assert(err, gocheck.IsNil)
	defer Delete("looking@yes.com")
	for i := 0; i < items; i++ {
		Reserve("looking@yes.com", fmt.Sprintf("looking/%d", i))
	}
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(8))
	var wg sync.WaitGroup
	for i := 0; i < items+items/2; i++ {
		wg.Add(1)
		go func(i int) {
			err := Release("looking@yes.com", fmt.Sprintf("looking/%d", i))
			c.Check(err, gocheck.IsNil)
			wg.Done()
		}(i)
	}
	wg.Wait()
	conn, err := db.Conn()
	c.Assert(err, gocheck.IsNil)
	defer conn.Close()
	var u usage
	err = conn.Quota().Find(bson.M{"user": "looking@yes.com"}).One(&u)
	c.Assert(err, gocheck.IsNil)
	c.Assert(u.Items, gocheck.HasLen, 0)
}
<|endoftext|>


<commit_before>package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"github.com/telepresenceio/telepresence/v2/pkg/client"
)

const apiRoot = "https://sw.bakerstreet.io/kubeception/api/klusters/"

func kubeceptionRequest(ctx context.Context, client *http.Client, httpVerb, token, clusterName string, queryParams map[string]string) (string, error) {
	req, err := http.NewRequestWithContext(ctx, httpVerb, apiRoot+clusterName, nil)
	if err != nil {
		return "", fmt.Errorf("Unable to build %s request: %w", httpVerb, err)
	}
	q := req.URL.Query()
	for k, v := range queryParams {
		q.Add(k, v)
	}
	req.URL.RawQuery = q.Encode()
	req.Header.Add("Authorization", "Bearer "+token)
	resp, err := client.Do(req)
	if err != nil {
		return "", fmt.Errorf("Error in request: %w", err)
	}
	defer resp.Body.Close()
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("Unable to read response body: %w", err)
	}
	body := string(b)
	if resp.StatusCode >= 300 || resp.StatusCode < 200 {
		return "", fmt.Errorf("Status code was %d. Response body: %s", resp.StatusCode, body)
	}
	return body, nil
}

func run() error {
	if len(os.Args) != 3 {
		return fmt.Errorf("Usage: %s <create|destroy> <cluster-name>", os.Args[0])
	}
	verb := os.Args[1]
	clusterName := os.Args[2]

	token, ok := os.LookupEnv("DEV_TELEPRESENCE_KUBECEPTION_TOKEN")
	if !ok {
		return fmt.Errorf("Please set the DEV_TELEPRESENCE_KUBECEPTION_TOKEN environment variable to a valid kubeception token")
	}
	cli := &http.Client{}
	switch verb {
	case "create":
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
		defer cancel()
		err := client.Retry(ctx, "kubeception", func(ctx context.Context) error {
			ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
			defer cancel()
			kubeconfig, err := kubeceptionRequest(ctx, cli, "PUT", token, clusterName, map[string]string{"wait": "true", "timeoutSecs": "7200", "version": "1.19"})
			if err != nil {
				return err
			}
			fmt.Println(kubeconfig)
			return nil
		}, 2*time.Second, 10*time.Second)
		if err != nil {
			return err
		}
	case "destroy":
		ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
		defer cancel()
		_, err := kubeceptionRequest(ctx, cli, "DELETE", token, clusterName, map[string]string{})
		if err != nil {
			return err
		}
		fmt.Println("Cluster destroyed! Have a nice day.")
	default:
		return fmt.Errorf("Invalid parameter %s, must be one of create or destroy", verb)
	}

	return nil
}

func main() {
	err := run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
}
<commit_msg>Try the preview provider for kubeception.<commit_after>package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"github.com/telepresenceio/telepresence/v2/pkg/client"
)

const apiRoot = "https://sw.bakerstreet.io/kubeception/api/klusters/"

func kubeceptionRequest(ctx context.Context, client *http.Client, httpVerb, token, clusterName string, queryParams map[string]string) (string, error) {
	req, err := http.NewRequestWithContext(ctx, httpVerb, apiRoot+clusterName, nil)
	if err != nil {
		return "", fmt.Errorf("Unable to build %s request: %w", httpVerb, err)
	}
	q := req.URL.Query()
	for k, v := range queryParams {
		q.Add(k, v)
	}
	req.URL.RawQuery = q.Encode()
	req.Header.Add("Authorization", "Bearer "+token)
	resp, err := client.Do(req)
	if err != nil {
		return "", fmt.Errorf("Error in request: %w", err)
	}
	defer resp.Body.Close()
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("Unable to read response body: %w", err)
	}
	body := string(b)
	if resp.StatusCode >= 300 || resp.StatusCode < 200 {
		return "", fmt.Errorf("Status code was %d. Response body: %s", resp.StatusCode, body)
	}
	return body, nil
}

func run() error {
	if len(os.Args) != 3 {
		return fmt.Errorf("Usage: %s <create|destroy> <cluster-name>", os.Args[0])
	}
	verb := os.Args[1]
	clusterName := os.Args[2]

	token, ok := os.LookupEnv("DEV_TELEPRESENCE_KUBECEPTION_TOKEN")
	if !ok {
		return fmt.Errorf("Please set the DEV_TELEPRESENCE_KUBECEPTION_TOKEN environment variable to a valid kubeception token")
	}
	cli := &http.Client{}
	switch verb {
	case "create":
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
		defer cancel()
		err := client.Retry(ctx, "kubeception", func(ctx context.Context) error {
			ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
			defer cancel()
			kubeconfig, err := kubeceptionRequest(ctx, cli, "PUT", token, clusterName, map[string]string{"wait": "true", "timeoutSecs": "7200", "version": "1.19", "provider": "preview"})
			if err != nil {
				return err
			}
			fmt.Println(kubeconfig)
			return nil
		}, 2*time.Second, 10*time.Second)
		if err != nil {
			return err
		}
	case "destroy":
		ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
		defer cancel()
		_, err := kubeceptionRequest(ctx, cli, "DELETE", token, clusterName, map[string]string{})
		if err != nil {
			return err
		}
		fmt.Println("Cluster destroyed! Have a nice day.")
	default:
		return fmt.Errorf("Invalid parameter %s, must be one of create or destroy", verb)
	}

	return nil
}

func main() {
	err := run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package gherkin

import (
	"fmt"
	"strings"
	"unicode/utf8"
)

type (
	Argument interface{}

	PickleTag struct {
		Location Location `json:"location"`
		Name     string   `json:"name"`
	}

	PickleString struct {
		Location Location `json:"location"`
		Content  string   `json:"content"`
	}

	PickleCell struct {
		Location Location `json:"location"`
		Value    string   `json:"value"`
	}

	PickleRow struct {
		Cells []*PickleCell `json:"cells"`
	}

	PickleTable struct {
		Rows []*PickleRow `json:"rows"`
	}

	PickleStep struct {
		Text      string     `json:"text"`
		Arguments []Argument `json:"arguments"`
		Locations []Location `json:"locations"`
	}

	Pickle struct {
		Name      string        `json:"name"`
		Language  string        `json:"language"`
		Steps     []*PickleStep `json:"steps"`
		Tags      []*PickleTag  `json:"tags"`
		Locations []Location    `json:"locations"`
	}
)

func (gd *GherkinDocument) Pickles() []*Pickle {
	pickles := make([]*Pickle, 0)
	if gd.Feature == nil {
		return pickles
	}

	bgSteps := make([]*PickleStep, 0)
	for _, ch := range gd.Feature.Children {
		switch t := ch.(type) {
		case *Background:
			bgSteps = append(bgSteps, pickleSteps(t.Steps)...)
		case *Scenario:
			if len(t.Steps) == 0 {
				continue
			}
			steps := append(bgSteps, pickleSteps(t.Steps)...)
			tags := pickleTags(append(gd.Feature.Tags, t.Tags...))
			pickles = append(pickles, &Pickle{
				Steps:     steps,
				Tags:      tags,
				Name:      t.Name,
				Language:  gd.Feature.Language,
				Locations: []Location{*t.Location},
			})
		case *ScenarioOutline:
			if len(t.Steps) == 0 {
				continue
			}

			for _, examples := range t.Examples {
				if examples.TableHeader == nil {
					continue
				}
				keys := examples.TableHeader.Cells
				for _, example := range examples.TableBody {
					vals := example.Cells
					tags := pickleTags(append(gd.Feature.Tags, append(t.Tags, examples.Tags...)...))

					var steps []*PickleStep

					// translate steps based on example
					for _, step := range t.Steps {
						text := step.Text
						for i, key := range keys {
							text = strings.Replace(text, "<"+key.Value+">", vals[i].Value, -1)
						}

						loc := Location{
							Line:   step.Location.Line,
							Column: step.Location.Column + utf8.RuneCountInString(step.Keyword),
						}

						steps = append(steps, &PickleStep{
							Text:      text,
							Arguments: pickleArgument(step.Argument, keys, vals),
							Locations: []Location{*example.Location, loc},
						})
					}

					// translate pickle name
					name := t.Name
					for i, key := range keys {
						name = strings.Replace(name, "<"+key.Value+">", vals[i].Value, -1)
					}

					pickles = append(pickles, &Pickle{
						Steps:     append(bgSteps, steps...),
						Tags:      tags,
						Name:      name,
						Language:  gd.Feature.Language,
						Locations: []Location{*example.Location, *t.Location},
					})
				}
			}
		default:
			panic(fmt.Sprintf("unexpected %T feature child", ch))
		}
	}
	return pickles
}

func pickleTags(tags []*Tag) []*PickleTag {
	ptags := make([]*PickleTag, len(tags))
	for i, tag := range tags {
		ptags[i] = &PickleTag{
			Location: *tag.Location,
			Name:     tag.Name,
		}
	}
	return ptags
}

func pickleSteps(steps []*Step) []*PickleStep {
	psteps := make([]*PickleStep, len(steps))
	for i, step := range steps {
		loc := Location{
			Line:   step.Location.Line,
			Column: step.Location.Column + utf8.RuneCountInString(step.Keyword),
		}
		psteps[i] = &PickleStep{
			Text:      step.Text,
			Arguments: pickleArgument(step.Argument, nil, nil),
			Locations: []Location{loc},
		}
	}
	return psteps
}

func pickleArgument(arg interface{}, keys, vals []*TableCell) []Argument {
	trans := func(s string) string {
		if keys == nil || vals == nil {
			return s
		}

		for i, key := range keys {
			s = strings.Replace(s, "<"+key.Value+">", vals[i].Value, -1)
		}

		return s
	}

	args := make([]Argument, 0)
	switch t := arg.(type) {
	case *DocString:
		args = append(args, &PickleString{
			Location: *t.Location,
			Content:  trans(t.Content),
		})
	case *DataTable:
		rows := make([]*PickleRow, len(t.Rows))
		for i, row := range t.Rows {
			cells := make([]*PickleCell, len(row.Cells))
			for i, cell := range row.Cells {
				cells[i] = &PickleCell{
					Location: *cell.Location,
					Value:    trans(cell.Value),
				}
			}
			rows[i] = &PickleRow{Cells: cells}
		}
		args = append(args, &PickleTable{Rows: rows})
	case nil:
		break
	default:
		panic(fmt.Sprintf("unexpected %T step argument", arg))
	}

	return args
}
<commit_msg>[Gherkin] (Go) Create empty pickle for empty scenarios.<commit_after>package gherkin

import (
	"fmt"
	"strings"
	"unicode/utf8"
)

type (
	Argument interface{}

	PickleTag struct {
		Location Location `json:"location"`
		Name     string   `json:"name"`
	}

	PickleString struct {
		Location Location `json:"location"`
		Content  string   `json:"content"`
	}

	PickleCell struct {
		Location Location `json:"location"`
		Value    string   `json:"value"`
	}

	PickleRow struct {
		Cells []*PickleCell `json:"cells"`
	}

	PickleTable struct {
		Rows []*PickleRow `json:"rows"`
	}

	PickleStep struct {
		Text      string     `json:"text"`
		Arguments []Argument `json:"arguments"`
		Locations []Location `json:"locations"`
	}

	Pickle struct {
		Name      string        `json:"name"`
		Language  string        `json:"language"`
		Steps     []*PickleStep `json:"steps"`
		Tags      []*PickleTag  `json:"tags"`
		Locations []Location    `json:"locations"`
	}
)

func (gd *GherkinDocument) Pickles() []*Pickle {
	pickles := make([]*Pickle, 0)
	if gd.Feature == nil {
		return pickles
	}

	bgSteps := make([]*PickleStep, 0)
	for _, ch := range gd.Feature.Children {
		switch t := ch.(type) {
		case *Background:
			bgSteps = append(bgSteps, pickleSteps(t.Steps)...)
		case *Scenario:
			steps := make([]*PickleStep, 0)
			if len(t.Steps) > 0 {
				steps = append(bgSteps, pickleSteps(t.Steps)...)
			}
			tags := pickleTags(append(gd.Feature.Tags, t.Tags...))
			pickles = append(pickles, &Pickle{
				Steps:     steps,
				Tags:      tags,
				Name:      t.Name,
				Language:  gd.Feature.Language,
				Locations: []Location{*t.Location},
			})
		case *ScenarioOutline:
			for _, examples := range t.Examples {
				if examples.TableHeader == nil {
					continue
				}
				keys := examples.TableHeader.Cells
				for _, example := range examples.TableBody {
					vals := example.Cells
					tags := pickleTags(append(gd.Feature.Tags, append(t.Tags, examples.Tags...)...))

					steps := make([]*PickleStep, 0)

					// translate steps based on example
					for _, step := range t.Steps {
						text := step.Text
						for i, key := range keys {
							text = strings.Replace(text, "<"+key.Value+">", vals[i].Value, -1)
						}

						loc := Location{
							Line:   step.Location.Line,
							Column: step.Location.Column + utf8.RuneCountInString(step.Keyword),
						}

						steps = append(steps, &PickleStep{
							Text:      text,
							Arguments: pickleArgument(step.Argument, keys, vals),
							Locations: []Location{*example.Location, loc},
						})
					}

					// translate pickle name
					name := t.Name
					for i, key := range keys {
						name = strings.Replace(name, "<"+key.Value+">", vals[i].Value, -1)
					}

					if len(steps) > 0 {
						steps = append(bgSteps, steps...)
					}
					pickles = append(pickles, &Pickle{
						Steps:     steps,
						Tags:      tags,
						Name:      name,
						Language:  gd.Feature.Language,
						Locations: []Location{*example.Location, *t.Location},
					})
				}
			}
		default:
			panic(fmt.Sprintf("unexpected %T feature child", ch))
		}
	}
	return pickles
}

func pickleTags(tags []*Tag) []*PickleTag {
	ptags := make([]*PickleTag, len(tags))
	for i, tag := range tags {
		ptags[i] = &PickleTag{
			Location: *tag.Location,
			Name:     tag.Name,
		}
	}
	return ptags
}

func pickleSteps(steps []*Step) []*PickleStep {
	psteps := make([]*PickleStep, len(steps))
	for i, step := range steps {
		loc := Location{
			Line:   step.Location.Line,
			Column: step.Location.Column + utf8.RuneCountInString(step.Keyword),
		}
		psteps[i] = &PickleStep{
			Text:      step.Text,
			Arguments: pickleArgument(step.Argument, nil, nil),
			Locations: []Location{loc},
		}
	}
	return psteps
}

func pickleArgument(arg interface{}, keys, vals []*TableCell) []Argument {
	trans := func(s string) string {
		if keys == nil || vals == nil {
			return s
		}

		for i, key := range keys {
			s = strings.Replace(s, "<"+key.Value+">", vals[i].Value, -1)
		}

		return s
	}

	args := make([]Argument, 0)
	switch t := arg.(type) {
	case *DocString:
		args = append(args, &PickleString{
			Location: *t.Location,
			Content:  trans(t.Content),
		})
	case *DataTable:
		rows := make([]*PickleRow, len(t.Rows))
		for i, row := range t.Rows {
			cells := make([]*PickleCell, len(row.Cells))
			for i, cell := range row.Cells {
				cells[i] = &PickleCell{
					Location: *cell.Location,
					Value:    trans(cell.Value),
				}
			}
			rows[i] = &PickleRow{Cells: cells}
		}
		args = append(args, &PickleTable{Rows: rows})
	case nil:
		break
	default:
		panic(fmt.Sprintf("unexpected %T step argument", arg))
	}

	return args
}
<|endoftext|>


<commit_before>package gocli

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"unicode/utf8"
)

func NewTable() *Table {
	return &Table{
		Columns:   [][]string{},
		Lengths:   map[int]int{},
		Separator: "\t",
	}
}

type Table struct {
	Columns   [][]string
	Lengths   map[int]int
	Separator string

	SortBy int
}

func (t *Table) Select(message string) int {
	for {
		fmt.Fprintf(os.Stdout, t.StringWithIndex()+"\n"+message+": ")
		scanner := bufio.NewScanner(os.Stdin)
		scanner.Scan()
		i, e := strconv.Atoi(scanner.Text())
		if e == nil {
			if i > 0 && i <= len(t.Columns) {
				return i - 1
			}
		}
	}
}

func (t *Table) Len() int { return len(t.Columns) }

func (t *Table) Swap(a, b int) { t.Columns[a], t.Columns[b] = t.Columns[b], t.Columns[a] }

func (t *Table) Less(a, b int) bool {
	if len(t.Columns[a]) <= t.SortBy {
		return false
	} else if len(t.Columns[b]) <= t.SortBy {
		return true
	} else {
		return fmt.Sprint(t.Columns[a][t.SortBy]) <= fmt.Sprintf(t.Columns[b][t.SortBy])
	}
	return true
}

func (t *Table) String() string {
	return strings.Join(t.Lines(false), "\n")
}

func (t *Table) StringWithIndex() string {
	return strings.Join(t.Lines(true), "\n")
}

var uncolorRegexp = regexp.MustCompile("\033\\[38;5;\\d+m([^\033]+)\033\\[0m")

func stringLength(s string) int {
	return utf8.RuneCountInString((uncolorRegexp.ReplaceAllString(s, "$1")))
}

func (t *Table) Lines(printIndex bool) (lines []string) {
	for row, col := range t.Columns {
		cl := []string{}
		if printIndex {
			col = append([]string{strconv.Itoa(row + 1)}, col...)
		}
		for i, v := range col {
			theLen := t.Lengths[i]
			if printIndex {
				if i == 0 {
					theLen = intLength(len(t.Columns))
				} else {
					theLen = t.Lengths[i-1]
				}
			}
			pad := theLen - stringLength(v)
			cl = append(cl, v+strings.Repeat(" ", pad))
		}
		lines = append(lines, strings.Join(cl, t.Separator))
	}
	return
}

func intLength(i int) int {
	if i == 0 {
		return 1
	} else if i < 0 {
		return intLength(int(math.Abs(float64(i)))) + 1
	}
	return int(math.Ceil(math.Log10(float64(i + 1))))
}

func (t *Table) AddStrings(list []string) {
	for i, s := range list {
		length := stringLength(s)
		if width := t.Lengths[i]; width < length {
			t.Lengths[i] = length
		}
	}
	t.Columns = append(t.Columns, list)
}

// Add adds a column to the table
func (t *Table) Add(cols ...interface{}) {
	converted := make([]string, 0, len(cols))
	for _, v := range cols {
		converted = append(converted, fmt.Sprint(v))
	}
	t.AddStrings(converted)
}

// Dereferencing pointers if not nil
// TODO: Please someone tell me, how to do this right!
func (t *Table) AddP(cols ...interface{}) {
	converted := make([]string, 0, len(cols))
	var str string
	for _, v := range cols {
		if value := reflect.ValueOf(v); value.Kind() == reflect.Ptr {
			indirect := reflect.Indirect(value)
			switch {
			case indirect != reflect.Zero(value.Type()) && indirect.IsValid() == true:
				switch {
				case indirect.Kind() == reflect.String:
					str = fmt.Sprint(indirect.String())
				case indirect.Kind() == reflect.Int:
					str = fmt.Sprint(indirect.Int())
				case indirect.Kind() == reflect.Float32:
					str = fmt.Sprint(indirect.Float())
				case indirect.Kind() == reflect.Bool:
					str = fmt.Sprint(indirect.Bool())
				default:
					str = ""
				}
			default:
				str = ""
			}
		} else {
			str = fmt.Sprint(v)
		}
		converted = append(converted, str)
	}
	t.AddStrings(converted)
}
<commit_msg>outputs more often in AddP<commit_after>package gocli

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"unicode/utf8"
)

func NewTable() *Table {
	return &Table{
		Columns:   [][]string{},
		Lengths:   map[int]int{},
		Separator: "\t",
	}
}

type Table struct {
	Columns   [][]string
	Lengths   map[int]int
	Separator string

	SortBy int
}

func (t *Table) Select(message string) int {
	for {
		fmt.Fprintf(os.Stdout, t.StringWithIndex()+"\n"+message+": ")
		scanner := bufio.NewScanner(os.Stdin)
		scanner.Scan()
		i, e := strconv.Atoi(scanner.Text())
		if e == nil {
			if i > 0 && i <= len(t.Columns) {
				return i - 1
			}
		}
	}
}

func (t *Table) Len() int { return len(t.Columns) }

func (t *Table) Swap(a, b int) { t.Columns[a], t.Columns[b] = t.Columns[b], t.Columns[a] }

func (t *Table) Less(a, b int) bool {
	if len(t.Columns[a]) <= t.SortBy {
		return false
	} else if len(t.Columns[b]) <= t.SortBy {
		return true
	} else {
		return fmt.Sprint(t.Columns[a][t.SortBy]) <= fmt.Sprintf(t.Columns[b][t.SortBy])
	}
	return true
}

func (t *Table) String() string {
	return strings.Join(t.Lines(false), "\n")
}

func (t *Table) StringWithIndex() string {
	return strings.Join(t.Lines(true), "\n")
}

var uncolorRegexp = regexp.MustCompile("\033\\[38;5;\\d+m([^\033]+)\033\\[0m")

func stringLength(s string) int {
	return utf8.RuneCountInString((uncolorRegexp.ReplaceAllString(s, "$1")))
}

func (t *Table) Lines(printIndex bool) (lines []string) {
	for row, col := range t.Columns {
		cl := []string{}
		if printIndex {
			col = append([]string{strconv.Itoa(row + 1)}, col...)
		}
		for i, v := range col {
			theLen := t.Lengths[i]
			if printIndex {
				if i == 0 {
					theLen = intLength(len(t.Columns))
				} else {
					theLen = t.Lengths[i-1]
				}
			}
			pad := theLen - stringLength(v)
			cl = append(cl, v+strings.Repeat(" ", pad))
		}
		lines = append(lines, strings.Join(cl, t.Separator))
	}
	return
}

func intLength(i int) int {
	if i == 0 {
		return 1
	} else if i < 0 {
		return intLength(int(math.Abs(float64(i)))) + 1
	}
	return int(math.Ceil(math.Log10(float64(i + 1))))
}

func (t *Table) AddStrings(list []string) {
	for i, s := range list {
		length := stringLength(s)
		if width := t.Lengths[i]; width < length {
			t.Lengths[i] = length
		}
	}
	t.Columns = append(t.Columns, list)
}

// Add adds a column to the table
func (t *Table) Add(cols ...interface{}) {
	converted := make([]string, 0, len(cols))
	for _, v := range cols {
		converted = append(converted, fmt.Sprint(v))
	}
	t.AddStrings(converted)
}

// Dereferencing pointers if not nil
// TODO: Please someone tell me, how to do this right!
func (t *Table) AddP(cols ...interface{}) {
	converted := make([]string, 0, len(cols))
	var str string
	for _, v := range cols {
		if value := reflect.ValueOf(v); value.Kind() == reflect.Ptr {
			indirect := reflect.Indirect(value)
			switch {
			case indirect != reflect.Zero(value.Type()) && indirect.IsValid() == true:
				switch {
				case indirect.Kind() == reflect.String:
					str = fmt.Sprint(indirect.String())
				case indirect.Kind() == reflect.Int:
					str = fmt.Sprint(indirect.Int())
				case indirect.Kind() == reflect.Float32:
					str = fmt.Sprint(indirect.Float())
				case indirect.Kind() == reflect.Bool:
					str = fmt.Sprint(indirect.Bool())
				case indirect.Kind() == reflect.Slice:
					str = fmt.Sprint(v)
				default:
					str = fmt.Sprint(v)
				}
			default:
				str = ""
			}
		} else {
			str = fmt.Sprint(v)
		}
		converted = append(converted, str)
	}
	t.AddStrings(converted)
}
<|endoftext|>


<commit_before>package androidbinary

import (
	"encoding/binary"
	"io"
	"os"
)

type TableFile struct {
	stringPool    *ResStringPool
	tablePackages []*TablePackage
}

type ResTableHeader struct {
	Header       ResChunkHeader
	PackageCount uint32
}

type ResTablePackage struct {
	Header         ResChunkHeader
	Id             uint32
	Name           [128]uint16
	TypeStrings    uint32
	LastPublicType uint32
	KeyStrings     uint32
	LastPublicKey  uint32
}

type TablePackage struct {
	Header      ResTablePackage
	TypeStrings *ResStringPool
	KeyStrings  *ResStringPool
	TableTypes  []*TableType
}

type ResTableType struct {
	Header       ResChunkHeader
	Id           uint8
	Res0         uint8
	Res1         uint16
	EntryCount   uint32
	EntriesStart uint32
	Config       ResTableConfig
}

type ResTableConfig struct {
	Size   uint32
	Imsi   uint32
	Locale struct {
		Language [2]uint8
		Country  [2]uint8
	}
	ScreenType   uint32
	Input        uint32
	ScreenSize   uint32
	Version      uint32
	ScreenConfig uint32
}

type TableType struct {
	Header  *ResTableType
	Entries []TableEntry
}

type ResTableEntry struct {
	Size  uint16
	Flags uint16
	Key   ResStringPoolRef
}

type TableEntry struct {
	Key   *ResTableEntry
	Value *ResValue
	Flags uint32
}

type ResTableTypeSpec struct {
	Header     ResChunkHeader
	Id         uint8
	Res0       uint8
	Res1       uint16
	EntryCount uint32
}

func (f *TableFile) readTable(sr *io.SectionReader) error {
	header := new(ResTableHeader)
	binary.Read(sr, binary.LittleEndian, header)
	f.tablePackages = make([]*TablePackage, header.PackageCount)

	offset := int64(header.Header.HeaderSize)
	for offset < int64(header.Header.Size) {
		chunkHeader, err := f.readChunk(sr, offset)
		if err != nil {
			return err
		}
		offset += int64(chunkHeader.Size)
	}
	return nil
}

func (f *TableFile) readChunk(r io.ReaderAt, offset int64) (*ResChunkHeader, error) {
	sr := io.NewSectionReader(r, offset, 1<<63-1-offset)
	chunkHeader := &ResChunkHeader{}
	sr.Seek(0, os.SEEK_SET)
	if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
		return nil, err
	}

	var err error
	sr.Seek(0, os.SEEK_SET)
	numTablePackages := 0
	switch chunkHeader.Type {
	case RES_TABLE_TYPE:
		err = f.readTable(sr)
	case RES_STRING_POOL_TYPE:
		f.stringPool, err = ReadStringPool(sr)
	case RES_TABLE_PACKAGE_TYPE:
		var tablePackage *TablePackage
		tablePackage, err = ReadTablePackage(sr)
		f.tablePackages[numTablePackages] = tablePackage
		numTablePackages++
	}
	if err != nil {
		return nil, err
	}

	return chunkHeader, nil
}

func readTablePackage(sr *io.SectionReader) (*TablePackage, error) {
	tablePackage := new(TablePackage)
	header := new(ResTablePackage)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}

	srTypes := io.NewSectionReader(sr, int64(header.TypeStrings), int64(header.Header.Size-header.TypeStrings))
	if typeStrings, err := readStringPool(srTypes); err == nil {
		tablePackage.TypeStrings = typeStrings
	} else {
		return nil, err
	}

	srKeys := io.NewSectionReader(sr, int64(header.KeyStrings), int64(header.Header.Size-header.KeyStrings))
	if keyStrings, err := readStringPool(srKeys); err == nil {
		tablePackage.KeyStrings = keyStrings
	} else {
		return nil, err
	}

	offset := int64(header.Header.HeaderSize)
	for offset < int64(header.Header.Size) {
		chunkHeader := &ResChunkHeader{}
		sr.Seek(offset, os.SEEK_SET)
		if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
			return nil, err
		}

		var err error
		chunkReader := io.NewSectionReader(sr, offset, int64(chunkHeader.Size))
		sr.Seek(offset, os.SEEK_SET)
		switch chunkHeader.Type {
		case RES_TABLE_TYPE_TYPE:
			var tableType *TableType
			tableType, err = readTableType(chunkReader)
			tablePackage.TableTypes = append(tablePackage.TableTypes, tableType)
		case RES_TABLE_TYPE_SPEC_TYPE:
			_, err = readTableTypeSpec(chunkReader)
		}
		if err != nil {
			return nil, err
		}
		offset += int64(chunkHeader.Size)
	}

	return tablePackage, nil
}

func readTableType(sr *io.SectionReader) (*TableType, error) {
	header := new(ResTableType)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}

	entryIndexes := make([]uint32, header.EntryCount)
	sr.Seek(int64(header.Header.HeaderSize), os.SEEK_SET)
	if err := binary.Read(sr, binary.LittleEndian, entryIndexes); err != nil {
		return nil, err
	}

	entries := make([]TableEntry, header.EntryCount)
	for i, index := range entryIndexes {
		if index == 0xFFFFFFFF {
			continue
		}
		sr.Seek(int64(header.EntriesStart+index), os.SEEK_SET)
		var key ResTableEntry
		binary.Read(sr, binary.LittleEndian, &key)
		entries[i].Key = &key

		var val ResValue
		binary.Read(sr, binary.LittleEndian, &val)
		entries[i].Value = &val
	}
	return &TableType{
		header,
		entries,
	}, nil
}

func readTableTypeSpec(sr *io.SectionReader) ([]uint32, error) {
	header := new(ResTableTypeSpec)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}

	flags := make([]uint32, header.EntryCount)
	sr.Seek(int64(header.Header.HeaderSize), os.SEEK_SET)
	if err := binary.Read(sr, binary.LittleEndian, flags); err != nil {
		return nil, err
	}
	return flags, nil
}
<commit_msg>ファイルからリソースを読み込む関数追加<commit_after>package androidbinary

import (
	"encoding/binary"
	"io"
	"os"
)

type TableFile struct {
	stringPool    *ResStringPool
	tablePackages []*TablePackage
}

type ResTableHeader struct {
	Header       ResChunkHeader
	PackageCount uint32
}

type ResTablePackage struct {
	Header         ResChunkHeader
	Id             uint32
	Name           [128]uint16
	TypeStrings    uint32
	LastPublicType uint32
	KeyStrings     uint32
	LastPublicKey  uint32
}

type TablePackage struct {
	Header      ResTablePackage
	TypeStrings *ResStringPool
	KeyStrings  *ResStringPool
	TableTypes  []*TableType
}

type ResTableType struct {
	Header       ResChunkHeader
	Id           uint8
	Res0         uint8
	Res1         uint16
	EntryCount   uint32
	EntriesStart uint32
	Config       ResTableConfig
}

type ResTableConfig struct {
	Size   uint32
	Imsi   uint32
	Locale struct {
		Language [2]uint8
		Country  [2]uint8
	}
	ScreenType   uint32
	Input        uint32
	ScreenSize   uint32
	Version      uint32
	ScreenConfig uint32
}

type TableType struct {
	Header  *ResTableType
	Entries []TableEntry
}

type ResTableEntry struct {
	Size  uint16
	Flags uint16
	Key   ResStringPoolRef
}

type TableEntry struct {
	Key   *ResTableEntry
	Value *ResValue
	Flags uint32
}

type ResTableTypeSpec struct {
	Header     ResChunkHeader
	Id         uint8
	Res0       uint8
	Res1       uint16
	EntryCount uint32
}

func NewTableFile(r io.ReaderAt) (*TableFile, error) {
	f := new(TableFile)
	sr := io.NewSectionReader(r, 0, 1<<63-1)

	header := new(ResTableHeader)
	binary.Read(sr, binary.LittleEndian, header)
	f.tablePackages = make([]*TablePackage, header.PackageCount)

	offset := int64(header.Header.HeaderSize)
	for offset < int64(header.Header.Size) {
		chunkHeader, err := f.readChunk(sr, offset)
		if err != nil {
			return nil, err
		}
		offset += int64(chunkHeader.Size)
	}
	return f, nil
}

func (f *TableFile) readChunk(r io.ReaderAt, offset int64) (*ResChunkHeader, error) {
	sr := io.NewSectionReader(r, offset, 1<<63-1-offset)
	chunkHeader := &ResChunkHeader{}
	sr.Seek(0, os.SEEK_SET)
	if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
		return nil, err
	}

	var err error
	sr.Seek(0, os.SEEK_SET)
	numTablePackages := 0
	switch chunkHeader.Type {
	case RES_STRING_POOL_TYPE:
		f.stringPool, err = readStringPool(sr)
	case RES_TABLE_PACKAGE_TYPE:
		var tablePackage *TablePackage
		tablePackage, err = readTablePackage(sr)
		f.tablePackages[numTablePackages] = tablePackage
		numTablePackages++
	}
	if err != nil {
		return nil, err
	}

	return chunkHeader, nil
}

func readTablePackage(sr *io.SectionReader) (*TablePackage, error) {
	tablePackage := new(TablePackage)
	header := new(ResTablePackage)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}

	srTypes := io.NewSectionReader(sr, int64(header.TypeStrings), int64(header.Header.Size-header.TypeStrings))
	if typeStrings, err := readStringPool(srTypes); err == nil {
		tablePackage.TypeStrings = typeStrings
	} else {
		return nil, err
	}

	srKeys := io.NewSectionReader(sr, int64(header.KeyStrings), int64(header.Header.Size-header.KeyStrings))
	if keyStrings, err := readStringPool(srKeys); err == nil {
		tablePackage.KeyStrings = keyStrings
	} else {
		return nil, err
	}

	offset := int64(header.Header.HeaderSize)
	for offset < int64(header.Header.Size) {
		chunkHeader := &ResChunkHeader{}
		sr.Seek(offset, os.SEEK_SET)
		if err := binary.Read(sr, binary.LittleEndian, chunkHeader); err != nil {
			return nil, err
		}

		var err error
		chunkReader := io.NewSectionReader(sr, offset, int64(chunkHeader.Size))
		sr.Seek(offset, os.SEEK_SET)
		switch chunkHeader.Type {
		case RES_TABLE_TYPE_TYPE:
			var tableType *TableType
			tableType, err = readTableType(chunkReader)
			tablePackage.TableTypes = append(tablePackage.TableTypes, tableType)
		case RES_TABLE_TYPE_SPEC_TYPE:
			_, err = readTableTypeSpec(chunkReader)
		}
		if err != nil {
			return nil, err
		}
		offset += int64(chunkHeader.Size)
	}

	return tablePackage, nil
}

func readTableType(sr *io.SectionReader) (*TableType, error) {
	header := new(ResTableType)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}

	entryIndexes := make([]uint32, header.EntryCount)
	sr.Seek(int64(header.Header.HeaderSize), os.SEEK_SET)
	if err := binary.Read(sr, binary.LittleEndian, entryIndexes); err != nil {
		return nil, err
	}

	entries := make([]TableEntry, header.EntryCount)
	for i, index := range entryIndexes {
		if index == 0xFFFFFFFF {
			continue
		}
		sr.Seek(int64(header.EntriesStart+index), os.SEEK_SET)
		var key ResTableEntry
		binary.Read(sr, binary.LittleEndian, &key)
		entries[i].Key = &key

		var val ResValue
		binary.Read(sr, binary.LittleEndian, &val)
		entries[i].Value = &val
	}
	return &TableType{
		header,
		entries,
	}, nil
}

func readTableTypeSpec(sr *io.SectionReader) ([]uint32, error) {
	header := new(ResTableTypeSpec)
	if err := binary.Read(sr, binary.LittleEndian, header); err != nil {
		return nil, err
	}

	flags := make([]uint32, header.EntryCount)
	sr.Seek(int64(header.Header.HeaderSize), os.SEEK_SET)
	if err := binary.Read(sr, binary.LittleEndian, flags); err != nil {
		return nil, err
	}
	return flags, nil
}
<|endoftext|>


<commit_before>package qb

import (
	"fmt"
	"strings"
)

// NewTable generates a new table pointer given table name, column and table constraints
func NewTable(name string, columns []Column, constraints []Constraint) *Table {
	return &Table{
		name:        name,
		columns:     columns,
		constraints: constraints,
		builder:     NewBuilder(),
		primaryCols: []string{},
		refs:        []ref{},
	}
}

// Table is the base abstraction for any sql table
type Table struct {
	name        string
	columns     []Column
	constraints []Constraint
	builder     *Builder
	primaryCols []string
	refs        []ref
}

// SQL generates create table syntax of table
func (t *Table) SQL() string {

	cols := []string{}
	for _, v := range t.columns {
		cols = append(cols, v.SQL())
	}

	constraints := []string{}

	// build primary key constraints using primaryCols
	if len(t.primaryCols) > 0 {
		constraints = append(constraints, fmt.Sprintf("PRIMARY KEY(%s)", strings.Join(t.primaryCols, ", ")))
	}

	// build foreign key constraints using refCols
	for _, ref := range t.refs {
		constraints = append(constraints, fmt.Sprintf("FOREIGN KEY(%s) REFERENCES %s(%s)", strings.Join(ref.cols, ", "), ref.refTable, strings.Join(ref.refCols, ", ")))
	}

	for _, v := range t.constraints {
		constraints = append(constraints, v.Name)
	}

	sql, _, _ := t.builder.CreateTable(t.name, cols, constraints).Build()
	return sql
}

// AddColumn appends a new column to current table
func (t *Table) AddColumn(column Column) {
	t.columns = append(t.columns, column)
}

// AddConstraint appends a new constraint to current table
func (t *Table) AddConstraint(c Constraint) {
	t.constraints = append(t.constraints, c)
}

// AddPrimary appends a primary column that will be lazily built as a primary key constraint
func (t *Table) AddPrimary(col string) {
	t.primaryCols = append(t.primaryCols, col)
}

// AddRef appends a new reference struct that will be lazily built as a foreign key constraint
func (t *Table) AddRef(col string, refTable string, refCol string) {

	if len(t.refs) > 0 {
		for k, ref := range t.refs {
			if refTable == ref.refTable {
				t.refs[k].cols = append(t.refs[k].cols, fmt.Sprintf("`%s`", col))
				t.refs[k].refCols = append(t.refs[k].refCols, fmt.Sprintf("`%s`", refCol))
				return
			}
		}
	}

	r := ref{[]string{}, refTable, []string{}}
	r.cols = append(r.cols, col)
	r.refCols = append(r.refCols, refCol)
	t.refs = append(t.refs, r)
}

// Constraints returns the constraint slice of current table
func (t *Table) Constraints() []Constraint {
	return t.constraints
}

type ref struct {
	cols     []string
	refTable string
	refCols  []string
}
<commit_msg>add query changes to table.SQL() function, add initial Insert function<commit_after>package qb

import (
	"fmt"
	"strings"
)

// NewTable generates a new table pointer given table name, column and table constraints
func NewTable(name string, columns []Column, constraints []Constraint) *Table {
	return &Table{
		name:        name,
		columns:     columns,
		constraints: constraints,
		builder:     NewBuilder(),
		primaryCols: []string{},
		refs:        []ref{},
	}
}

// Table is the base abstraction for any sql table
type Table struct {
	name        string
	columns     []Column
	constraints []Constraint
	builder     *Builder
	primaryCols []string
	refs        []ref
}

// SQL generates create table syntax of table
func (t *Table) SQL() string {

	cols := []string{}
	for _, v := range t.columns {
		cols = append(cols, v.SQL())
	}

	constraints := []string{}

	// build primary key constraints using primaryCols
	if len(t.primaryCols) > 0 {
		constraints = append(constraints, fmt.Sprintf("PRIMARY KEY(%s)", strings.Join(t.primaryCols, ", ")))
	}

	// build foreign key constraints using refCols
	for _, ref := range t.refs {
		constraints = append(constraints, fmt.Sprintf("FOREIGN KEY(%s) REFERENCES %s(%s)", strings.Join(ref.cols, ", "), ref.refTable, strings.Join(ref.refCols, ", ")))
	}

	for _, v := range t.constraints {
		constraints = append(constraints, v.Name)
	}

	query, err := t.builder.CreateTable(t.name, cols, constraints).Build()
	if err != nil {
		return ""
	}

	return query.SQL()
}

// AddColumn appends a new column to current table
func (t *Table) AddColumn(column Column) {
	t.columns = append(t.columns, column)
}

// AddConstraint appends a new constraint to current table
func (t *Table) AddConstraint(c Constraint) {
	t.constraints = append(t.constraints, c)
}

// AddPrimary appends a primary column that will be lazily built as a primary key constraint
func (t *Table) AddPrimary(col string) {
	t.primaryCols = append(t.primaryCols, col)
}

type ref struct {
	cols     []string
	refTable string
	refCols  []string
}

// AddRef appends a new reference struct that will be lazily built as a foreign key constraint
func (t *Table) AddRef(col string, refTable string, refCol string) {

	if len(t.refs) > 0 {
		for k, ref := range t.refs {
			if refTable == ref.refTable {
				t.refs[k].cols = append(t.refs[k].cols, fmt.Sprintf("`%s`", col))
				t.refs[k].refCols = append(t.refs[k].refCols, fmt.Sprintf("`%s`", refCol))
				return
			}
		}
	}

	r := ref{[]string{}, refTable, []string{}}
	r.cols = append(r.cols, col)
	r.refCols = append(r.refCols, refCol)
	t.refs = append(t.refs, r)
}

// Constraints returns the constraint slice of current table
func (t *Table) Constraints() []Constraint {
	return t.constraints
}

func (t *Table) Insert(kv map[string]interface{}) (*Query, error) {

	keys := make([]string, len(kv))
	vals := make([]interface{}, len(kv))

	for k, v := range kv {
		keys = append(keys, k)
		vals = append(vals, v)
	}

	return t.builder.Insert(t.name, keys...).Values(vals...).Build()
}<|endoftext|>


<commit_before>package controllers

import (
	"github.com/abustany/goexiv"
	"github.com/robfig/revel"
	"gollery/app"
	"gollery/app/common"
	"gollery/thumbnailer"
	"gollery/utils"
	"net/http"
	"os"
	"path"
	"strings"
)

type Albums struct {
	*revel.Controller
}

type AlbumInfo struct {
	Name string `json:"name"`
}

type AlbumData struct {
	Name     string         `json:"name"`
	Pictures []*PictureInfo `json:"pictures"`
}

type PictureInfo struct {
	Path     string            `json:"path"`
	Metadata map[string]string `json:"metadata"`
}

var ExifKeys = []string{
	"Exif.Photo.ExifVersion",
	"Exif.Image.Make",
	"Exif.Image.Model",
	"Exif.Photo.DateTimeOriginal",
	"Exif.Photo.ExposureTime",
	"Exif.Photo.ShutterSpeedValue",
	"Exif.Photo.FNumber",
	"Exif.Photo.ApertureValue",
	"Exif.Photo.ExposureBiasValue",
	"Exif.Photo.Flash",
	"Exif.Photo.FocalLength",
	"Exif.Photo.FocalLengthIn35mmFilm",
	"Exif.Photo.SubjectDistance",
	"Exif.Photo.ISOSpeedRatings",
	"Exif.Photo.ExposureProgram",
	"Exif.Photo.MeteringMode",
	"Exif.Image.ImageWidth",
	"Exif.Photo.PixelXDimension",
	"Exif.Image.ImageLength",
	"Exif.Photo.PixelYDimension",
	"Exif.Image.Copyright",
	"Exif.Photo.UserComment",
	"Exif.GPSInfo.GPSAltitudeRef",
	"Exif.GPSInfo.GPSAltitude",
	"Exif.GPSInfo.GPSLatitudeRef",
	"Exif.GPSInfo.GPSLatitude",
	"Exif.GPSInfo.GPSLongitudeRef",
	"Exif.GPSInfo.GPSLongitude",
}

func (c *Albums) Index() revel.Result {
	watchedPaths := app.Monitor.WatchedDirectories()
	dirs := make([]*AlbumInfo, 0, len(watchedPaths))

	for _, dir := range watchedPaths {
		if dir == common.RootDir || !strings.HasPrefix(dir, common.RootDir) {
			continue
		}

		dir = dir[1+len(common.RootDir):]
		dirs = append(dirs, &AlbumInfo{
			Name: dir,
		})
	}

	c.Response.Status = http.StatusOK
	c.Response.ContentType = "application/json"
	return c.RenderJson(dirs)
}

func getMetadata(filePath string) (map[string]string, error) {
	fd, err := goexiv.Open(filePath)

	if err != nil {
		return nil, utils.WrapError(err, "Cannot open file to read metadata")
	}

	err = fd.ReadMetadata()

	if err != nil {
		return nil, utils.WrapError(err, "Cannot read metadata")
	}

	data := fd.GetExifData()

	metadata := make(map[string]string, len(ExifKeys))

	for _, key := range ExifKeys {
		val, err := data.FindKey(key)

		if err != nil {
			return nil, utils.WrapError(err, "Invalid EXIF key requested")
		}

		if val == nil {
			continue
		}

		metadata[key] = val.String()
	}

	return metadata, nil
}

func (c *Albums) Show(name string) revel.Result {
	revel.INFO.Printf("Loading album %s", name)

	dirPath := path.Join(common.RootDir, name)
	dirFd, err := os.Open(dirPath)

	if os.IsNotExist(err) {
		c.Response.Status = http.StatusNotFound
		c.Response.ContentType = "application/json"
		return c.RenderJson(struct{}{})
	}

	if err != nil {
		c.Response.Status = http.StatusInternalServerError
		return c.RenderError(utils.WrapError(err, "Cannot open album"))
	}

	fis, err := dirFd.Readdir(-1)

	if err != nil {
		c.Response.Status = http.StatusInternalServerError
		return c.RenderError(utils.WrapError(err, "Cannot read album"))
	}

	albumData := &AlbumData{
		Name:     name,
		Pictures: make([]*PictureInfo, 0, len(fis)),
	}

	for _, info := range fis {
		filePath := path.Join(dirPath, info.Name())

		hasThumbnail, err := app.Thumbnailer.HasThumbnail(filePath, thumbnailer.THUMB_SMALL)

		if err != nil {
			c.Response.Status = http.StatusInternalServerError
			c.RenderError(utils.WrapError(err, "Cannot check for thumbnail"))
		}

		if !hasThumbnail {
			continue
		}

		metadata, err := getMetadata(filePath)

		if err != nil {
			revel.ERROR.Printf("Cannot read metadata for file '%s': %s", filePath, err)
			metadata = map[string]string{}
		}

		picInfo := &PictureInfo{
			Path:     info.Name(),
			Metadata: metadata,
		}

		albumData.Pictures = append(albumData.Pictures, picInfo)
	}

	return c.RenderJson(albumData)
}
<commit_msg>albums: Return a cover picture for each album in the list<commit_after>package controllers

import (
	"github.com/abustany/goexiv"
	"github.com/robfig/revel"
	"gollery/app"
	"gollery/app/common"
	"gollery/thumbnailer"
	"gollery/utils"
	"math/rand"
	"net/http"
	"os"
	"path"
	"strings"
)

type Albums struct {
	*revel.Controller
}

type AlbumInfo struct {
	Name  string `json:"name"`
	Cover string `json:"cover"`
}

type AlbumData struct {
	Name     string         `json:"name"`
	Pictures []*PictureInfo `json:"pictures"`
}

type PictureInfo struct {
	Path     string            `json:"path"`
	Metadata map[string]string `json:"metadata"`
}

var ExifKeys = []string{
	"Exif.Photo.ExifVersion",
	"Exif.Image.Make",
	"Exif.Image.Model",
	"Exif.Photo.DateTimeOriginal",
	"Exif.Photo.ExposureTime",
	"Exif.Photo.ShutterSpeedValue",
	"Exif.Photo.FNumber",
	"Exif.Photo.ApertureValue",
	"Exif.Photo.ExposureBiasValue",
	"Exif.Photo.Flash",
	"Exif.Photo.FocalLength",
	"Exif.Photo.FocalLengthIn35mmFilm",
	"Exif.Photo.SubjectDistance",
	"Exif.Photo.ISOSpeedRatings",
	"Exif.Photo.ExposureProgram",
	"Exif.Photo.MeteringMode",
	"Exif.Image.ImageWidth",
	"Exif.Photo.PixelXDimension",
	"Exif.Image.ImageLength",
	"Exif.Photo.PixelYDimension",
	"Exif.Image.Copyright",
	"Exif.Photo.UserComment",
	"Exif.GPSInfo.GPSAltitudeRef",
	"Exif.GPSInfo.GPSAltitude",
	"Exif.GPSInfo.GPSLatitudeRef",
	"Exif.GPSInfo.GPSLatitude",
	"Exif.GPSInfo.GPSLongitudeRef",
	"Exif.GPSInfo.GPSLongitude",
}

func (c *Albums) Index() revel.Result {
	watchedPaths := app.Monitor.WatchedDirectories()
	dirs := make([]*AlbumInfo, 0, len(watchedPaths))

	for _, dir := range watchedPaths {
		if dir == common.RootDir || !strings.HasPrefix(dir, common.RootDir) {
			continue
		}

		dir = dir[1+len(common.RootDir):]

		cover, err := c.getAlbumCover(dir)

		if err != nil {
			revel.ERROR.Printf("Cannot lookup cover for album '%s': %s", dir, err)
		}

		dirs = append(dirs, &AlbumInfo{
			Name:  dir,
			Cover: cover,
		})
	}

	c.Response.Status = http.StatusOK
	c.Response.ContentType = "application/json"
	return c.RenderJson(dirs)
}

func (c *Albums) getAlbumCover(name string) (string, error) {
	fis, err := c.listPictures(name)

	if err != nil {
		return "", utils.WrapError(err, "Cannot list album '%s'", name)
	}

	for i := 0; i < 10; i++ {
		fi := fis[rand.Int31n(int32(len(fis)))]
		filePath := path.Join(name, fi.Name())

		hasThumbnail, err := app.Thumbnailer.HasThumbnail(filePath, thumbnailer.THUMB_SMALL)

		if err != nil {
			return "", utils.WrapError(err, "Cannot check if thumbnail exists for '%s'", filePath)
		}

		if !hasThumbnail {
			continue
		}

		return filePath, nil
	}

	return "", nil
}

func getMetadata(filePath string) (map[string]string, error) {
	fd, err := goexiv.Open(filePath)

	if err != nil {
		return nil, utils.WrapError(err, "Cannot open file to read metadata")
	}

	err = fd.ReadMetadata()

	if err != nil {
		return nil, utils.WrapError(err, "Cannot read metadata")
	}

	data := fd.GetExifData()

	metadata := make(map[string]string, len(ExifKeys))

	for _, key := range ExifKeys {
		val, err := data.FindKey(key)

		if err != nil {
			return nil, utils.WrapError(err, "Invalid EXIF key requested")
		}

		if val == nil {
			continue
		}

		metadata[key] = val.String()
	}

	return metadata, nil
}

func (c *Albums) listPictures(name string) ([]os.FileInfo, error) {
	dirPath := path.Join(common.RootDir, name)
	dirFd, err := os.Open(dirPath)

	if os.IsNotExist(err) {
		return nil, nil
	}

	if err != nil {
		return nil, utils.WrapError(err, "Cannot open folder")
	}

	defer dirFd.Close()

	fis, err := dirFd.Readdir(-1)

	if err != nil {
		return nil, utils.WrapError(err, "Cannot list folder")
	}

	return fis, nil
}

func (c *Albums) Show(name string) revel.Result {
	revel.INFO.Printf("Loading album %s", name)

	fis, err := c.listPictures(name)

	if err != nil {
		c.Response.Status = http.StatusInternalServerError
		return c.RenderError(utils.WrapError(err, "Cannot read album"))
	}

	albumData := &AlbumData{
		Name:     name,
		Pictures: make([]*PictureInfo, 0, len(fis)),
	}

	dirPath := path.Join(common.RootDir, name)

	for _, info := range fis {
		filePath := path.Join(dirPath, info.Name())

		hasThumbnail, err := app.Thumbnailer.HasThumbnail(filePath, thumbnailer.THUMB_SMALL)

		if err != nil {
			c.Response.Status = http.StatusInternalServerError
			c.RenderError(utils.WrapError(err, "Cannot check for thumbnail"))
		}

		if !hasThumbnail {
			continue
		}

		metadata, err := getMetadata(filePath)

		if err != nil {
			revel.ERROR.Printf("Cannot read metadata for file '%s': %s", filePath, err)
			metadata = map[string]string{}
		}

		picInfo := &PictureInfo{
			Path:     info.Name(),
			Metadata: metadata,
		}

		albumData.Pictures = append(albumData.Pictures, picInfo)
	}

	return c.RenderJson(albumData)
}
<|endoftext|>


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

/*
Package runtime contains operations that interact with Go's runtime system,
such as functions to control goroutines. It also includes the low-level type information
used by the reflect package; see reflect's documentation for the programmable
interface to the run-time type system.

Environment Variables

The following environment variables ($name or %name%, depending on the host
operating system) control the run-time behavior of Go programs. The meanings
and use may change from release to release.

The GOGC variable sets the initial garbage collection target percentage.
A collection is triggered when the ratio of freshly allocated data to live data
remaining after the previous collection reaches this percentage. The default
is GOGC=100. Setting GOGC=off disables the garbage collector entirely.
The runtime/debug package's SetGCPercent function allows changing this
percentage at run time. See https://golang.org/pkg/runtime/debug/#SetGCPercent.

The GODEBUG variable controls debugging variables within the runtime.
It is a comma-separated list of name=val pairs setting these named variables:

	allocfreetrace: setting allocfreetrace=1 causes every allocation to be
	profiled and a stack trace printed on each object's allocation and free.

	clobberfree: setting clobberfree=1 causes the garbage collector to
	clobber the memory content of an object with bad content when it frees
	the object.

	cgocheck: setting cgocheck=0 disables all checks for packages
	using cgo to incorrectly pass Go pointers to non-Go code.
	Setting cgocheck=1 (the default) enables relatively cheap
	checks that may miss some errors.  Setting cgocheck=2 enables
	expensive checks that should not miss any errors, but will
	cause your program to run slower.

	efence: setting efence=1 causes the allocator to run in a mode
	where each object is allocated on a unique page and addresses are
	never recycled.

	gccheckmark: setting gccheckmark=1 enables verification of the
	garbage collector's concurrent mark phase by performing a
	second mark pass while the world is stopped.  If the second
	pass finds a reachable object that was not found by concurrent
	mark, the garbage collector will panic.

	gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
	print information about the internal state of the concurrent pacer.

	gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
	onto smaller stacks. In this mode, a goroutine's stack can only grow.

	gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
	making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
	also disables concurrent sweeping after the garbage collection finishes.

	gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
	error at each collection, summarizing the amount of memory collected and the
	length of the pause. The format of this line is subject to change.
	Currently, it is:
		gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
	where the fields are as follows:
		gc #        the GC number, incremented at each GC
		@#s         time in seconds since program start
		#%          percentage of time spent in GC since program start
		#+...+#     wall-clock/CPU times for the phases of the GC
		#->#-># MB  heap size at GC start, at GC end, and live heap
		# MB goal   goal heap size
		# P         number of processors used
	The phases are stop-the-world (STW) sweep termination, concurrent
	mark and scan, and STW mark termination. The CPU times
	for mark/scan are broken down in to assist time (GC performed in
	line with allocation), background GC time, and idle GC time.
	If the line ends with "(forced)", this GC was forced by a
	runtime.GC() call.

	inittrace: setting inittrace=1 causes the runtime to emit a single line to standard
	error for each package with init work, summarizing the execution time and memory
	allocation. No information is printed for inits executed as part of plugin loading
	and for packages without both user defined and compiler generated init work.
	The format of this line is subject to change. Currently, it is:
		init # @#ms, # ms clock, # bytes, # allocs
	where the fields are as follows:
		init #      the package name
		@# ms       time in milliseconds when the init started since program start
		# clock     wall-clock time for package initialization work
		# bytes     memory allocated on the heap
		# allocs    number of heap allocations

	madvdontneed: setting madvdontneed=0 will use MADV_FREE
	instead of MADV_DONTNEED on Linux when returning memory to the
	kernel. This is more efficient, but means RSS numbers will
	drop only when the OS is under memory pressure.

	memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
	When set to 0 memory profiling is disabled.  Refer to the description of
	MemProfileRate for the default value.

	invalidptr: invalidptr=1 (the default) causes the garbage collector and stack
	copier to crash the program if an invalid pointer value (for example, 1)
	is found in a pointer-typed location. Setting invalidptr=0 disables this check.
	This should only be used as a temporary workaround to diagnose buggy code.
	The real fix is to not store integers in pointer-typed locations.

	sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
	with a trivial allocator that obtains memory from the operating system and
	never reclaims any memory.

	scavtrace: setting scavtrace=1 causes the runtime to emit a single line to standard
	error, roughly once per GC cycle, summarizing the amount of work done by the
	scavenger as well as the total amount of memory returned to the operating system
	and an estimate of physical memory utilization. The format of this line is subject
	to change, but currently it is:
		scav # # KiB work, # KiB total, #% util
	where the fields are as follows:
		scav #       the scavenge cycle number
		# KiB work   the amount of memory returned to the OS since the last line
		# KiB total  the total amount of memory returned to the OS
		#% util      the fraction of all unscavenged memory which is in-use
	If the line ends with "(forced)", then scavenging was forced by a
	debug.FreeOSMemory() call.

	scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
	detailed multiline info every X milliseconds, describing state of the scheduler,
	processors, threads and goroutines.

	schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
	error every X milliseconds, summarizing the scheduler state.

	tracebackancestors: setting tracebackancestors=N extends tracebacks with the stacks at
	which goroutines were created, where N limits the number of ancestor goroutines to
	report. This also extends the information returned by runtime.Stack. Ancestor's goroutine
	IDs will refer to the ID of the goroutine at the time of creation; it's possible for this
	ID to be reused for another goroutine. Setting N to 0 will report no ancestry information.

	asyncpreemptoff: asyncpreemptoff=1 disables signal-based
	asynchronous goroutine preemption. This makes some loops
	non-preemptible for long periods, which may delay GC and
	goroutine scheduling. This is useful for debugging GC issues
	because it also disables the conservative stack scanning used
	for asynchronously preempted goroutines.

The net, net/http, and crypto/tls packages also refer to debugging variables in GODEBUG.
See the documentation for those packages for details.

The GOMAXPROCS variable limits the number of operating system threads that
can execute user-level Go code simultaneously. There is no limit to the number of threads
that can be blocked in system calls on behalf of Go code; those do not count against
the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
the limit.

The GORACE variable configures the race detector, for programs built using -race.
See https://golang.org/doc/articles/race_detector.html for details.

The GOTRACEBACK variable controls the amount of output generated when a Go
program fails due to an unrecovered panic or an unexpected runtime condition.
By default, a failure prints a stack trace for the current goroutine,
eliding functions internal to the run-time system, and then exits with exit code 2.
The failure prints stack traces for all goroutines if there is no current goroutine
or the failure is internal to the run-time.
GOTRACEBACK=none omits the goroutine stack traces entirely.
GOTRACEBACK=single (the default) behaves as described above.
GOTRACEBACK=all adds stack traces for all user-created goroutines.
GOTRACEBACK=system is like ``all'' but adds stack frames for run-time functions
and shows goroutines created internally by the run-time.
GOTRACEBACK=crash is like ``system'' but crashes in an operating system-specific
manner instead of exiting. For example, on Unix systems, the crash raises
SIGABRT to trigger a core dump.
For historical reasons, the GOTRACEBACK settings 0, 1, and 2 are synonyms for
none, all, and system, respectively.
The runtime/debug package's SetTraceback function allows increasing the
amount of output at run time, but it cannot reduce the amount below that
specified by the environment variable.
See https://golang.org/pkg/runtime/debug/#SetTraceback.

The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
the set of Go environment variables. They influence the building of Go programs
(see https://golang.org/cmd/go and https://golang.org/pkg/go/build).
GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
constants or functions in this package, but they do not influence the execution
of the run-time system.
*/
package runtime

import (
	"internal/goarch"
	"internal/goos"
)

// Caller reports file and line number information about function invocations on
// the calling goroutine's stack. The argument skip is the number of stack frames
// to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
// meaning of skip differs between Caller and Callers.) The return values report the
// program counter, file name, and line number within the file of the corresponding
// call. The boolean ok is false if it was not possible to recover the information.
func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
	rpc := make([]uintptr, 1)
	n := callers(skip+1, rpc[:])
	if n < 1 {
		return
	}
	frame, _ := CallersFrames(rpc).Next()
	return frame.PC, frame.File, frame.Line, frame.PC != 0
}

// Callers fills the slice pc with the return program counters of function invocations
// on the calling goroutine's stack. The argument skip is the number of stack frames
// to skip before recording in pc, with 0 identifying the frame for Callers itself and
// 1 identifying the caller of Callers.
// It returns the number of entries written to pc.
//
// To translate these PCs into symbolic information such as function
// names and line numbers, use CallersFrames. CallersFrames accounts
// for inlined functions and adjusts the return program counters into
// call program counters. Iterating over the returned slice of PCs
// directly is discouraged, as is using FuncForPC on any of the
// returned PCs, since these cannot account for inlining or return
// program counter adjustment.
func Callers(skip int, pc []uintptr) int {
	// runtime.callers uses pc.array==nil as a signal
	// to print a stack trace. Pick off 0-length pc here
	// so that we don't let a nil pc slice get to it.
	if len(pc) == 0 {
		return 0
	}
	return callers(skip, pc)
}

var defaultGOROOT string // set by cmd/link

// GOROOT returns the root of the Go tree. It uses the
// GOROOT environment variable, if set at process start,
// or else the root used during the Go build.
func GOROOT() string {
	s := gogetenv("GOROOT")
	if s != "" {
		return s
	}
	return defaultGOROOT
}

// buildVersion is the Go tree's version string at build time.
//
// If any GOEXPERIMENTs are set to non-default values, it will include
// "X:<GOEXPERIMENT>".
//
// This is set by the linker.
//
// This is accessed by "go version <binary>".
var buildVersion string

// Version returns the Go tree's version string.
// It is either the commit hash and date at the time of the build or,
// when possible, a release tag like "go1.3".
func Version() string {
	return buildVersion
}

// GOOS is the running program's operating system target:
// one of darwin, freebsd, linux, and so on.
// To view possible combinations of GOOS and GOARCH, run "go tool dist list".
const GOOS string = goos.GOOS

// GOARCH is the running program's architecture target:
// one of 386, amd64, arm, s390x, and so on.
const GOARCH string = goarch.GOARCH
<commit_msg>runtime: remove reference to crypto/tls GODEBUG usage<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
Package runtime contains operations that interact with Go's runtime system,
such as functions to control goroutines. It also includes the low-level type information
used by the reflect package; see reflect's documentation for the programmable
interface to the run-time type system.

Environment Variables

The following environment variables ($name or %name%, depending on the host
operating system) control the run-time behavior of Go programs. The meanings
and use may change from release to release.

The GOGC variable sets the initial garbage collection target percentage.
A collection is triggered when the ratio of freshly allocated data to live data
remaining after the previous collection reaches this percentage. The default
is GOGC=100. Setting GOGC=off disables the garbage collector entirely.
The runtime/debug package's SetGCPercent function allows changing this
percentage at run time. See https://golang.org/pkg/runtime/debug/#SetGCPercent.

The GODEBUG variable controls debugging variables within the runtime.
It is a comma-separated list of name=val pairs setting these named variables:

	allocfreetrace: setting allocfreetrace=1 causes every allocation to be
	profiled and a stack trace printed on each object's allocation and free.

	clobberfree: setting clobberfree=1 causes the garbage collector to
	clobber the memory content of an object with bad content when it frees
	the object.

	cgocheck: setting cgocheck=0 disables all checks for packages
	using cgo to incorrectly pass Go pointers to non-Go code.
	Setting cgocheck=1 (the default) enables relatively cheap
	checks that may miss some errors.  Setting cgocheck=2 enables
	expensive checks that should not miss any errors, but will
	cause your program to run slower.

	efence: setting efence=1 causes the allocator to run in a mode
	where each object is allocated on a unique page and addresses are
	never recycled.

	gccheckmark: setting gccheckmark=1 enables verification of the
	garbage collector's concurrent mark phase by performing a
	second mark pass while the world is stopped.  If the second
	pass finds a reachable object that was not found by concurrent
	mark, the garbage collector will panic.

	gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
	print information about the internal state of the concurrent pacer.

	gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
	onto smaller stacks. In this mode, a goroutine's stack can only grow.

	gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
	making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
	also disables concurrent sweeping after the garbage collection finishes.

	gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
	error at each collection, summarizing the amount of memory collected and the
	length of the pause. The format of this line is subject to change.
	Currently, it is:
		gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
	where the fields are as follows:
		gc #        the GC number, incremented at each GC
		@#s         time in seconds since program start
		#%          percentage of time spent in GC since program start
		#+...+#     wall-clock/CPU times for the phases of the GC
		#->#-># MB  heap size at GC start, at GC end, and live heap
		# MB goal   goal heap size
		# P         number of processors used
	The phases are stop-the-world (STW) sweep termination, concurrent
	mark and scan, and STW mark termination. The CPU times
	for mark/scan are broken down in to assist time (GC performed in
	line with allocation), background GC time, and idle GC time.
	If the line ends with "(forced)", this GC was forced by a
	runtime.GC() call.

	inittrace: setting inittrace=1 causes the runtime to emit a single line to standard
	error for each package with init work, summarizing the execution time and memory
	allocation. No information is printed for inits executed as part of plugin loading
	and for packages without both user defined and compiler generated init work.
	The format of this line is subject to change. Currently, it is:
		init # @#ms, # ms clock, # bytes, # allocs
	where the fields are as follows:
		init #      the package name
		@# ms       time in milliseconds when the init started since program start
		# clock     wall-clock time for package initialization work
		# bytes     memory allocated on the heap
		# allocs    number of heap allocations

	madvdontneed: setting madvdontneed=0 will use MADV_FREE
	instead of MADV_DONTNEED on Linux when returning memory to the
	kernel. This is more efficient, but means RSS numbers will
	drop only when the OS is under memory pressure.

	memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
	When set to 0 memory profiling is disabled.  Refer to the description of
	MemProfileRate for the default value.

	invalidptr: invalidptr=1 (the default) causes the garbage collector and stack
	copier to crash the program if an invalid pointer value (for example, 1)
	is found in a pointer-typed location. Setting invalidptr=0 disables this check.
	This should only be used as a temporary workaround to diagnose buggy code.
	The real fix is to not store integers in pointer-typed locations.

	sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
	with a trivial allocator that obtains memory from the operating system and
	never reclaims any memory.

	scavtrace: setting scavtrace=1 causes the runtime to emit a single line to standard
	error, roughly once per GC cycle, summarizing the amount of work done by the
	scavenger as well as the total amount of memory returned to the operating system
	and an estimate of physical memory utilization. The format of this line is subject
	to change, but currently it is:
		scav # # KiB work, # KiB total, #% util
	where the fields are as follows:
		scav #       the scavenge cycle number
		# KiB work   the amount of memory returned to the OS since the last line
		# KiB total  the total amount of memory returned to the OS
		#% util      the fraction of all unscavenged memory which is in-use
	If the line ends with "(forced)", then scavenging was forced by a
	debug.FreeOSMemory() call.

	scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
	detailed multiline info every X milliseconds, describing state of the scheduler,
	processors, threads and goroutines.

	schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
	error every X milliseconds, summarizing the scheduler state.

	tracebackancestors: setting tracebackancestors=N extends tracebacks with the stacks at
	which goroutines were created, where N limits the number of ancestor goroutines to
	report. This also extends the information returned by runtime.Stack. Ancestor's goroutine
	IDs will refer to the ID of the goroutine at the time of creation; it's possible for this
	ID to be reused for another goroutine. Setting N to 0 will report no ancestry information.

	asyncpreemptoff: asyncpreemptoff=1 disables signal-based
	asynchronous goroutine preemption. This makes some loops
	non-preemptible for long periods, which may delay GC and
	goroutine scheduling. This is useful for debugging GC issues
	because it also disables the conservative stack scanning used
	for asynchronously preempted goroutines.

The net and net/http packages also refer to debugging variables in GODEBUG.
See the documentation for those packages for details.

The GOMAXPROCS variable limits the number of operating system threads that
can execute user-level Go code simultaneously. There is no limit to the number of threads
that can be blocked in system calls on behalf of Go code; those do not count against
the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
the limit.

The GORACE variable configures the race detector, for programs built using -race.
See https://golang.org/doc/articles/race_detector.html for details.

The GOTRACEBACK variable controls the amount of output generated when a Go
program fails due to an unrecovered panic or an unexpected runtime condition.
By default, a failure prints a stack trace for the current goroutine,
eliding functions internal to the run-time system, and then exits with exit code 2.
The failure prints stack traces for all goroutines if there is no current goroutine
or the failure is internal to the run-time.
GOTRACEBACK=none omits the goroutine stack traces entirely.
GOTRACEBACK=single (the default) behaves as described above.
GOTRACEBACK=all adds stack traces for all user-created goroutines.
GOTRACEBACK=system is like ``all'' but adds stack frames for run-time functions
and shows goroutines created internally by the run-time.
GOTRACEBACK=crash is like ``system'' but crashes in an operating system-specific
manner instead of exiting. For example, on Unix systems, the crash raises
SIGABRT to trigger a core dump.
For historical reasons, the GOTRACEBACK settings 0, 1, and 2 are synonyms for
none, all, and system, respectively.
The runtime/debug package's SetTraceback function allows increasing the
amount of output at run time, but it cannot reduce the amount below that
specified by the environment variable.
See https://golang.org/pkg/runtime/debug/#SetTraceback.

The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
the set of Go environment variables. They influence the building of Go programs
(see https://golang.org/cmd/go and https://golang.org/pkg/go/build).
GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
constants or functions in this package, but they do not influence the execution
of the run-time system.
*/
package runtime

import (
	"internal/goarch"
	"internal/goos"
)

// Caller reports file and line number information about function invocations on
// the calling goroutine's stack. The argument skip is the number of stack frames
// to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
// meaning of skip differs between Caller and Callers.) The return values report the
// program counter, file name, and line number within the file of the corresponding
// call. The boolean ok is false if it was not possible to recover the information.
func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
	rpc := make([]uintptr, 1)
	n := callers(skip+1, rpc[:])
	if n < 1 {
		return
	}
	frame, _ := CallersFrames(rpc).Next()
	return frame.PC, frame.File, frame.Line, frame.PC != 0
}

// Callers fills the slice pc with the return program counters of function invocations
// on the calling goroutine's stack. The argument skip is the number of stack frames
// to skip before recording in pc, with 0 identifying the frame for Callers itself and
// 1 identifying the caller of Callers.
// It returns the number of entries written to pc.
//
// To translate these PCs into symbolic information such as function
// names and line numbers, use CallersFrames. CallersFrames accounts
// for inlined functions and adjusts the return program counters into
// call program counters. Iterating over the returned slice of PCs
// directly is discouraged, as is using FuncForPC on any of the
// returned PCs, since these cannot account for inlining or return
// program counter adjustment.
func Callers(skip int, pc []uintptr) int {
	// runtime.callers uses pc.array==nil as a signal
	// to print a stack trace. Pick off 0-length pc here
	// so that we don't let a nil pc slice get to it.
	if len(pc) == 0 {
		return 0
	}
	return callers(skip, pc)
}

var defaultGOROOT string // set by cmd/link

// GOROOT returns the root of the Go tree. It uses the
// GOROOT environment variable, if set at process start,
// or else the root used during the Go build.
func GOROOT() string {
	s := gogetenv("GOROOT")
	if s != "" {
		return s
	}
	return defaultGOROOT
}

// buildVersion is the Go tree's version string at build time.
//
// If any GOEXPERIMENTs are set to non-default values, it will include
// "X:<GOEXPERIMENT>".
//
// This is set by the linker.
//
// This is accessed by "go version <binary>".
var buildVersion string

// Version returns the Go tree's version string.
// It is either the commit hash and date at the time of the build or,
// when possible, a release tag like "go1.3".
func Version() string {
	return buildVersion
}

// GOOS is the running program's operating system target:
// one of darwin, freebsd, linux, and so on.
// To view possible combinations of GOOS and GOARCH, run "go tool dist list".
const GOOS string = goos.GOOS

// GOARCH is the running program's architecture target:
// one of 386, amd64, arm, s390x, and so on.
const GOARCH string = goarch.GOARCH
<|endoftext|>


<commit_before>// @author       sigu-399
// @description  An implementation of JSON Reference - Go language
// @created      26-02-2013

package gojsonreference

import (

)

<commit_msg>added struct, constructor and parser interfaces<commit_after>// @author       sigu-399
// @description  An implementation of JSON Reference - Go language
// @created      26-02-2013

package gojsonreference

import ()

func NewJsonReference(jsonReferenceString string) (JsonReference, error) {

	var r JsonReference
	err := r.parse(jsonReferenceString)
	return r, err

}

type JsonReference struct {
}

func (r *JsonReference) parse(JsonReferenceString string) error {

	var err error

	return err
}
<|endoftext|>


<commit_before>package server

import (
	"sync"

	"github.com/prometheus/client_golang/prometheus"

	"hockeypuck/hkp/storage"
)

var metrics = struct {
	keysAdded   prometheus.Gauge
	keysUpdated prometheus.Gauge
	// TODO(pjdc): Track KeyNotChanged? i.e. .Upsert called yielding no change
}{
	keysAdded: prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "hockeypuck",
			Name:      "keys_added",
			Help:      "New keys added since startup",
		},
	),
	keysUpdated: prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "hockeypuck",
			Name:      "keys_updated",
			Help:      "Keys updated since startup",
		},
	),
}

var metricsRegister sync.Once

func registerMetrics() {
	metricsRegister.Do(func() {
		prometheus.MustRegister(metrics.keysAdded)
		prometheus.MustRegister(metrics.keysUpdated)
	})
}

func metricsStorageNotifier(kc storage.KeyChange) error {
	switch kc.(type) {
	case storage.KeyAdded:
		metrics.keysAdded.Inc()
	case storage.KeyReplaced:
		metrics.keysUpdated.Inc()
	}
	return nil
}
<commit_msg>change keysAdded and keysUpdated to counters<commit_after>package server

import (
	"sync"

	"github.com/prometheus/client_golang/prometheus"

	"hockeypuck/hkp/storage"
)

var metrics = struct {
	keysAdded   prometheus.Counter
	keysUpdated prometheus.Counter
	// TODO(pjdc): Track KeyNotChanged? i.e. .Upsert called yielding no change
}{
	keysAdded: prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "hockeypuck",
			Name:      "keys_added",
			Help:      "New keys added since startup",
		},
	),
	keysUpdated: prometheus.NewCounter(
		prometheus.CounterOpts{
			Namespace: "hockeypuck",
			Name:      "keys_updated",
			Help:      "Keys updated since startup",
		},
	),
}

var metricsRegister sync.Once

func registerMetrics() {
	metricsRegister.Do(func() {
		prometheus.MustRegister(metrics.keysAdded)
		prometheus.MustRegister(metrics.keysUpdated)
	})
}

func metricsStorageNotifier(kc storage.KeyChange) error {
	switch kc.(type) {
	case storage.KeyAdded:
		metrics.keysAdded.Inc()
	case storage.KeyReplaced:
		metrics.keysUpdated.Inc()
	}
	return nil
}
<|endoftext|>