Skip to content

Commit

Permalink
generate: require ".*" to generate all functions (#66)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
elliotchance authored Apr 30, 2019
1 parent 63d6c53 commit baee0d5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -57,7 +57,7 @@ func main() {
}
```

## Custom Types And Structs
## Custom Types

Annotate the slice type in your source code:

Expand All @@ -66,7 +66,7 @@ type Car struct {
Name, Color string
}

//go:generate pie Cars
//go:generate pie Cars.*
type Cars []Car
```

Expand Down Expand Up @@ -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:

Expand All @@ -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. |
Expand All @@ -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. |
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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:]
}
2 changes: 1 addition & 1 deletion pie/car.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pie

//go:generate pie cars carPointers
//go:generate pie cars.* carPointers.*
type cars []car
type carPointers []*car

Expand Down
2 changes: 1 addition & 1 deletion pie/currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pie/float64s.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pie

//go:generate pie Float64s
//go:generate pie Float64s.*
type Float64s []float64
2 changes: 1 addition & 1 deletion pie/ints.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pie

//go:generate pie Ints
//go:generate pie Ints.*
type Ints []int

//go:generate pie myInts.Sum.Average
Expand Down
2 changes: 1 addition & 1 deletion pie/strings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pie

//go:generate pie Strings
//go:generate pie Strings.*
type Strings []string

0 comments on commit baee0d5

Please sign in to comment.