From baee0d51f82815abf23370a8fa2105c251efefb6 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Wed, 1 May 2019 00:02:08 +1000 Subject: [PATCH] generate: require ".*" to generate all functions (#66) As the number of available functions increase, the usefulness or generating all of them lessens. This is a breaking change but a necessary one. You will now need to specify `foo.*` to generate all functions for `foo`. --- README.md | 22 +++++++++++----------- main.go | 6 +++++- pie/car.go | 2 +- pie/currencies.go | 2 +- pie/float64s.go | 2 +- pie/ints.go | 2 +- pie/strings.go | 2 +- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a0a556e..97cd917 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ focuses on type safety, performance and immutability. - [Quick Start](#quick-start) * [Built-in Types](#built-in-types) - * [Custom Types And Structs](#custom-types-and-structs) + * [Custom Types](#custom-types) * [Limiting Functions Generated](#limiting-functions-generated) - [Functions](#functions) - [FAQ](#faq) - * [What are the requirements?](#what-are-the-requirements) - * [What are the goals of `pie`?](#what-are-the-goals-of-pie) - * [Can I contribute?](#how-do-i-contribute-a-function) - * [Why is the emoji a slice of pizza instead of a pie?](#why-is-the-emoji-a-slice-of-pizza-instead-of-a-pie) + * [What are the requirements?](#what-are-the-requirements-) + * [What are the goals of `pie`?](#what-are-the-goals-of--pie--) + * [How do I contribute a function?](#how-do-i-contribute-a-function-) + * [Why is the emoji a slice of pizza instead of a pie?](#why-is-the-emoji-a-slice-of-pizza-instead-of-a-pie-) # Quick Start @@ -57,7 +57,7 @@ func main() { } ``` -## Custom Types And Structs +## Custom Types Annotate the slice type in your source code: @@ -66,7 +66,7 @@ type Car struct { Name, Color string } -//go:generate pie Cars +//go:generate pie Cars.* type Cars []Car ``` @@ -109,7 +109,7 @@ cars.Unselect(func (car Car) { ## Limiting Functions Generated -By default all functions will be generated. This is easy to get going but +The `.*` can be used to generate all functions. This is easy to get going but creates a lot of unused code. You can limit the functions generated by chaining the function names with a dot syntax, like: @@ -129,7 +129,7 @@ This will only generate `myInts.Average`, `myInts.Sum` and `myStrings.Select`. | `AreSorted` | ✓ | ✓ | | | n | Check if the slice is already sorted. | | `AreUnique` | ✓ | ✓ | | | n | Check if the slice contains only unique elements. | | `Average` | | ✓ | | | n | The average (mean) value, or a zeroed value. | -| `Bottom` | ✓ | ✓ | ✓ | | n | Gets n elements from bottom. | +| `Bottom` | ✓ | ✓ | ✓ | | n | Gets n elements from bottom. | | `Contains` | ✓ | ✓ | ✓ | | n | Check if the value exists in the slice. | | `Extend` | ✓ | ✓ | ✓ | | n | A new slice with the elements from each slice appended to the end. | | `First` | ✓ | ✓ | ✓ | | 1 | The first element, or a zeroed value. | @@ -146,8 +146,8 @@ This will only generate `myInts.Average`, `myInts.Sum` and `myStrings.Select`. | `Select` | ✓ | ✓ | ✓ | | n | A new slice containing only the elements that returned true from the condition. | | `Sort` | ✓ | ✓ | | | n⋅log(n) | Return a new sorted slice. | | `Sum` | | ✓ | | | n | Sum (total) of all elements. | -| `Shuffle` | ✓ | ✓ |✓ | | n | Returns a new shuffled slice. | -| `Top` | ✓ | ✓ | ✓ | | n | Gets several elements from top(head of slice).| +| `Shuffle` | ✓ | ✓ | ✓ | | n | Returns a new shuffled slice. | +| `Top` | ✓ | ✓ | ✓ | | n | Gets several elements from top(head of slice).| | `ToStrings` | ✓ | ✓ | ✓ | | n | Transforms each element to a string. | | `Transform` | ✓ | ✓ | ✓ | | n | A new slice where each element has been transformed. | | `Unique` | ✓ | ✓ | | | n⋅log(n) | Return a new slice with only unique elements. | diff --git a/main.go b/main.go index 4fa0689..de4b553 100644 --- a/main.go +++ b/main.go @@ -141,7 +141,7 @@ func main() { var templates []string for _, function := range functions.Functions { - if len(fns) > 0 && !pie.Strings(fns).Contains(function.Name) { + if fns[0] != "*" && !pie.Strings(fns).Contains(function.Name) { continue } @@ -210,5 +210,9 @@ func main() { func getFunctionsFromArg(arg string) (mapOrSliceType string, fns []string) { parts := strings.Split(arg, ".") + if len(parts) < 2 { + panic("must specify at least one function or *: " + arg) + } + return parts[0], parts[1:] } diff --git a/pie/car.go b/pie/car.go index 8181825..aa6c1a3 100644 --- a/pie/car.go +++ b/pie/car.go @@ -1,6 +1,6 @@ package pie -//go:generate pie cars carPointers +//go:generate pie cars.* carPointers.* type cars []car type carPointers []*car diff --git a/pie/currencies.go b/pie/currencies.go index 2432807..609e836 100644 --- a/pie/currencies.go +++ b/pie/currencies.go @@ -4,7 +4,7 @@ type currency struct { NumericCode, Exponent int } -//go:generate pie currencies +//go:generate pie currencies.* type currencies map[string]currency var isoCurrencies = currencies{ diff --git a/pie/float64s.go b/pie/float64s.go index ee85eb0..a381f10 100644 --- a/pie/float64s.go +++ b/pie/float64s.go @@ -1,4 +1,4 @@ package pie -//go:generate pie Float64s +//go:generate pie Float64s.* type Float64s []float64 diff --git a/pie/ints.go b/pie/ints.go index 36db18c..18eef6c 100644 --- a/pie/ints.go +++ b/pie/ints.go @@ -1,6 +1,6 @@ package pie -//go:generate pie Ints +//go:generate pie Ints.* type Ints []int //go:generate pie myInts.Sum.Average diff --git a/pie/strings.go b/pie/strings.go index 7cfff8f..649679b 100644 --- a/pie/strings.go +++ b/pie/strings.go @@ -1,4 +1,4 @@ package pie -//go:generate pie Strings +//go:generate pie Strings.* type Strings []string