<commit_before>package dyntab_test

import (
	"errors"
	"github.com/simonschneider/dyntab"
	"os"
	"reflect"
	"strconv"
	"time"
)

type (
	MyTime struct{ time.Time }
	info   struct {
		name        string
		secret      string `tab:"-"`
		description string `tab:"desc"`
	}
	container struct {
		info
		number int64 `tab:"number of smth"`
		T      MyTime
		Loc    time.Location `tab:"location"`
	}
	containers []container
)

func (t MyTime) MarshalText() (text []byte, err error) {
	return []byte(t.Format("2006-01-02")), nil
}

func Loc2String(i interface{}) (string, error) {
	l, ok := i.(time.Location)
	if ok {
		return "in: " + l.String(), nil
	}
	return "", errors.New("not time.location")
}

func (c containers) Footer() ([]string, error) {
	sum := int64(0)
	for _, n := range c {
		sum += n.number
	}
	return []string{"", "Total", strconv.Itoa(int(sum)), "", " "}, nil
}

func Example() {
	cont := containers{
		container{
			info: info{
				name:        "hello",
				secret:      "pretty",
				description: "world",
			},
			number: int64(2),
			T:      MyTime{time.Unix(1355270400, 0)},
			Loc:    *time.UTC,
		},
		container{
			info: info{
				name:        "good",
				secret:      "sweet",
				description: "bye",
			},
			number: int64(4),
			T:      MyTime{time.Unix(1355270400, 0)},
			Loc:    *time.UTC,
		},
	}

	dyntab.NewTable().
		SetData(cont).
		Recurse([]reflect.Type{
			reflect.TypeOf(info{}),
			reflect.TypeOf(container{})}).
		Specialize([]dyntab.ToSpecialize{{
			reflect.TypeOf(time.Location{}),
			Loc2String}}).
		PrintTo(os.Stdout)
	// Output:
	// +-------+-------+----------------+------------+----------+
	// | NAME  | DESC  | NUMBER OF SMTH |     T      | LOCATION |
	// +-------+-------+----------------+------------+----------+
	// | hello | world |              2 | 2012-12-12 | in: UTC  |
	// | good  | bye   |              4 | 2012-12-12 | in: UTC  |
	// +-------+-------+----------------+------------+----------+
	// |         TOTAL |       6        |                       |
	// +-------+-------+----------------+------------+----------+
}<commit_msg>fix vet warnings about unkeyed access<commit_after>package dyntab_test

import (
	"errors"
	"github.com/simonschneider/dyntab"
	"os"
	"reflect"
	"strconv"
	"time"
)

type (
	MyTime struct{ time.Time }
	info   struct {
		name        string
		secret      string `tab:"-"`
		description string `tab:"desc"`
	}
	container struct {
		info
		number int64 `tab:"number of smth"`
		T      MyTime
		Loc    time.Location `tab:"location"`
	}
	containers []container
)

func (t MyTime) MarshalText() (text []byte, err error) {
	return []byte(t.Format("2006-01-02")), nil
}

func Loc2String(i interface{}) (string, error) {
	l, ok := i.(time.Location)
	if ok {
		return "in: " + l.String(), nil
	}
	return "", errors.New("not time.location")
}

func (c containers) Footer() ([]string, error) {
	sum := int64(0)
	for _, n := range c {
		sum += n.number
	}
	return []string{"", "Total", strconv.Itoa(int(sum)), "", " "}, nil
}

func Example() {
	cont := containers{
		container{
			info: info{
				name:        "hello",
				secret:      "pretty",
				description: "world",
			},
			number: int64(2),
			T:      MyTime{time.Unix(1355270400, 0)},
			Loc:    *time.UTC,
		},
		container{
			info: info{
				name:        "good",
				secret:      "sweet",
				description: "bye",
			},
			number: int64(4),
			T:      MyTime{time.Unix(1355270400, 0)},
			Loc:    *time.UTC,
		},
	}

	dyntab.NewTable().
		SetData(cont).
		Recurse([]reflect.Type{
			reflect.TypeOf(info{}),
			reflect.TypeOf(container{})}).
		Specialize([]dyntab.ToSpecialize{{
			Type:     reflect.TypeOf(time.Location{}),
			ToString: Loc2String}}).
		PrintTo(os.Stdout)
	// Output:
	// +-------+-------+----------------+------------+----------+
	// | NAME  | DESC  | NUMBER OF SMTH |     T      | LOCATION |
	// +-------+-------+----------------+------------+----------+
	// | hello | world |              2 | 2012-12-12 | in: UTC  |
	// | good  | bye   |              4 | 2012-12-12 | in: UTC  |
	// +-------+-------+----------------+------------+----------+
	// |         TOTAL |       6        |                       |
	// +-------+-------+----------------+------------+----------+
}
{'fraction_non_alphanumeric': 0.26490066225165565, 'fraction_numerical': 0.0293282876064333, 'mean_word_length': 1.7143774069319642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '170001', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1459, 'n_words': 311}