All Projects → erizocosmico → Go Itergen

erizocosmico / Go Itergen

Licence: mit
Code generation tool for Go that generates common functions for iterable types such as map and filter for slices and channels.

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to Go Itergen

Flapigen Rs
Tool for connecting programs or libraries written in Rust with other languages
Stars: ✭ 473 (+437.5%)
Mutual labels:  codegen
Graphqlgen
⚙️ Generate type-safe resolvers based upon your GraphQL Schema
Stars: ✭ 796 (+804.55%)
Mutual labels:  codegen
Magiql
🌐 💫 Simple and powerful GraphQL Client, love child of react-query ❤️ relay
Stars: ✭ 45 (-48.86%)
Mutual labels:  codegen
Weaver
Dependency Injection framework for Swift (iOS/macOS/Linux)
Stars: ✭ 546 (+520.45%)
Mutual labels:  codegen
Astring
🌳 Tiny and fast JavaScript code generator from an ESTree-compliant AST.
Stars: ✭ 757 (+760.23%)
Mutual labels:  codegen
Graphql Java Codegen Maven Plugin
Maven plugin for graphql-java-codegen
Stars: ✭ 17 (-80.68%)
Mutual labels:  codegen
Guardrail
Principled code generation from OpenAPI specifications
Stars: ✭ 396 (+350%)
Mutual labels:  codegen
Cppast.codegen
An extensible library providing C# PInvoke codegen from C/C++ files for .NET
Stars: ✭ 65 (-26.14%)
Mutual labels:  codegen
Esview
此项目已暂停开发
Stars: ✭ 761 (+764.77%)
Mutual labels:  codegen
Fugen
Command line tool for exporting resources and generating code from your Figma files
Stars: ✭ 41 (-53.41%)
Mutual labels:  codegen
Wire
Compile-time Dependency Injection for Go
Stars: ✭ 7,091 (+7957.95%)
Mutual labels:  codegen
Inkwell
It's a New Kind of Wrapper for Exposing LLVM (Safely)
Stars: ✭ 732 (+731.82%)
Mutual labels:  codegen
Moq
Interface mocking tool for go generate
Stars: ✭ 941 (+969.32%)
Mutual labels:  codegen
Oto
Go driven rpc code generation tool for right now.
Stars: ✭ 478 (+443.18%)
Mutual labels:  codegen
Kpoet
An expressive DSL built on top of JavaPoet to make writing code almost as easy as writing the code yourself.
Stars: ✭ 58 (-34.09%)
Mutual labels:  codegen
Nswag
The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
Stars: ✭ 4,825 (+5382.95%)
Mutual labels:  codegen
Graphql Code Generator
A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
Stars: ✭ 7,993 (+8982.95%)
Mutual labels:  codegen
Aarkay Archived
AarKay is a language independent code generation framework. This project has been deprecated and is no longer maintained.
Stars: ✭ 69 (-21.59%)
Mutual labels:  codegen
Variable Injector
Continuous Integration Tool for Swift Projects
Stars: ✭ 63 (-28.41%)
Mutual labels:  codegen
Codegen
A model-view based code generator written in Java
Stars: ✭ 36 (-59.09%)
Mutual labels:  codegen

go-itergen Build Status

go-itergen addresses a common and big problem of go: no maps, no filters, no nothing. If you come from a functional background that could be really frustrating. Since generics are not going to be around for a while code generation is the only way we have to achieve such things.

This is a naive attempt to make easier deal with this kind of operations over iterable types without having to write every single time the same code over and over.

It also has a very nice feature: it works with channels, not just slices. So, you can map, filter, concat and reduce channels. And even convert them to arrays. All without always having to write the same boilerplate code. TL;DR: channel manipulation made easy.

Install

go get -u github.com/erizocosmico/go-itergen/cmd/...

Available operations

go-itergen generates the following functions for a slice type:

  • Map: apply a function to every element and return a slice/channel with the modifications. It actually returns a XXXIterMapResult, which will have a set of operations to convert the interface{} result to other types.
  • Filter: apply a function and will return a slice/channel with all the elements whose result was true.
  • All (only for slices): will return true if all the elements return true after applying the given function.
  • Some (only for slices): will return true if any of the elements return true after applying the given function.
  • Concat:
    • In the case of a slice, will return a new array with the contents of a and b given a.Concat(b).
    • In the case of a channel, will return a new channel multiplexing all the other given channels. E.g: a.Concat(b, c, d) will return a channel with all the items sent to a, b, c and d.
  • Find (only for slices): will return the item and the index of the first occurrence that returns true after applying the given function.
  • ForEach: will execute a function for every item in the slice/channel.
  • Reverse (only for slices): will return the slice in reversed order.
  • Splice (only for slices): will return a new slice with a number of items removed after the given start.
  • Reduce: applies a function against an accumulator and each value of the slice/channel (from first to last) to reduce it to a single value of the given type.
  • Array (only for channels): converts the channel into an array. The operation blocks, but can be done in a goroutine and you will be notified via the done parameter.

You can choose which operations you want for your type, that is, if you don't need Map or another function it won't be generated.

Generate code

You just have to add that to a file in the package you want the code to be generated in.

//go:generate go-itergen -t "float64" --pkg="mypkg" --map="string" --map="int" --filter --all --some --foreach --concat --find --reverse --splice --reduce="string" --reduce="int"

Or execute the binary:

go-itergen -t "float64" --pkg="mypkg" --map="string" --map="int" --filter --all --some --foreach --concat --find --reverse --splice --reduce="string" --reduce="int"

Types from external packages

If you want to generate an iterable type for an external package, you can do that with :. For example, let's say we want a os.File iterable. We would do it like this:

go-itergen -t "os:os.File" --pkg="mypkg" --map="string"

Note that what goes before the : is what would go in the import, and that you have to type the full type name. This is because of how Go packages work. One can not guarantee that github.com/foo/go-bar will be go-bar.Foo. It may be bar.Foo. And thus, it has to be specified.

Another example:

go-itergen -t "golang.org/x/net/context:context.Context" --pkg="mypkg" --map="github.com/foo/ctx:ctx.MyCtx"

Take a look at the map. We can also specify the external packages in map and reduce arguments.

Example

For examples of generated code see the examples folder. Contains a file with a chan float64 iterable and another with a float64 slice iterable.

Usage example:

func main() {
  rounded, err := NewFloat64Iter(1.2, 2.4, 3.5, 5.6).Filter(func(n float64) bool {
		return n > 2.0
  }).Map(func(int i, n float64) interface{} {
    return int(n)
  }).ToInt()
  fmt.Println(rounded) // [3 5]
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].