collagen

command module
v0.0.0-...-bcb4304 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2019 License: MIT Imports: 1 Imported by: 0

README

Collagen

Build Status

Collagen generates methods to operate on collection for a specific type.

Installation

You can install Collagen using go get:

go get github.com/Szymongib/collagen

Usage

Collagen generated methods operates on collection type, which is type alias on a slice of structs.

For the following Friend struct:

type Friend struct {
    Name string
    Age  int
}

The generated collection type will be:

type Friends []Friend

To generate the collection type as well as all methods, run:

collagen --name Friend

or annotate the struct using go generate:

//go:generate collagen --name Friend
type Friend struct {
    Name string
    Age  int
}

and run:

go generate

To specify the name of the collection type, use the --plural option:

collagen --name Friend --plural FriendCollection

Now you can use generated methods directly on the collection type:

friends := FriendCollection{
	{Name: "Jon", Age: 21},
	{Name: "James", Age: 44},
	{Name: "Jaine", Age: 25},
}

friendsBelowThirty := friends.Filter(func(f Friend) bool {
	return f.Age < 30
})

hasFriendOlderThanForty := friends.Exists(func(f Friend) bool {
	return f.Age > 40
})

var sliceOfFriends []Friend = friends.ToSlice()

To checkout more, see the examples.

Collection methods

Collagen can generate the following methods:

  • Contains
  • Drop
  • Exists
  • Filter
  • Map
  • Take

To generate only subset of methods use --functions flag, with coma-separated names of the methods. For example, to generate only Filter, Exists and Map methods for the Friend struct, run:

collagen --name Friend --functions "Filter,Exists,Map"

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cmd
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL