Giter Club home page Giter Club logo

grizzly's Introduction

Grizzly

Build Status codecov

Grizzly allows you to use collections in GO without generics. With Grizzly you can use the methods Map, Filter, Find, etc.

Usage with generation

Install Grizzly:

$ go get github.com/matroskin13/grizzly

And update your working file:

//go:generate grizzly generate main.go

package main

import (
    "fmt"
)

//grizzly:generate
type User struct {
    Id   int
    Name string
    Age  int
}

func main() {
    users := NewUserCollection([]*User{
        {Id: 1, Name: "John", Age: 20},
        {Id: 2, Name: "Tom", Age: 22},
        {Id: 3, Name: "Billy", Age: 20},
        {Id: 4, Name: "Mister X", Age: 30},
    })

    youngUsers := users.Filter(func (user *User) bool {
        return user.Age < 30
    })

    youngUsersIds := youngUsers.MapToInt(func (user *User) int {
        return user.Id
    })

    fmt.Println("young users ids", youngUsersIds)
}

And run go generate:

$ go generate

Generate from config

Create file grizzly.json in your root directory

{
  "collections": [
    {
      "name": "user",
      "types": {
        "id": "int",
        "name": "string",
        "age": "int"
      }
    },
    {
      "name": "city",
      "types": {
        "cityId": "int"
      }
    }
  ]
}

And run Grizzly

$ grizzly update

Now you can use the collection code:

package test

import (
    "fmt"
    "test/collections"
)

func main() {
    users := collections.NewUserCollection([]*collections.User{
        {Id: 1, Name: "John", Age: 20},
        {Id: 2, Name: "Tom", Age: 22},
        {Id: 3, Name: "Billy", Age: 20},
        {Id: 4, Name: "Mister X", Age: 30},
    })

    city := collections.NewCitiesCollection([]*collections.Cities{
        {CityId: 1},
    }).Find(func (city *collections.Cities) bool {
        return true
    })

    youngUsers := users.Filter(func (user *collections.User) bool {
        return user.Age < 30
    })

    Tom := youngUsers.Find(func (user *collections.User) bool {
        return user.Name == "Tom"
    })

    youngUsersIds := youngUsers.MapToInt(func (user *collections.User) int {
        return user.Id
    })

    uniqAges := users.UniqByAge()
    sortedAges := users.SortByAge("asc").MapToInt(func (user *collections.User) int {
        return user.Age
    })

    fmt.Println("tom", Tom)
    fmt.Println("young users ids", youngUsersIds)
    fmt.Println("first city", city)
    fmt.Println("uniq ages", uniqAges)
    fmt.Println("sorted ages", sortedAges)
}

You can also specify the required methods:

{
  "name": "User",
  "types": {
    "id": "int",
    "name": "string",
    "age": "int"
  },
  "methods": ["find", "filter"]
}

List of default methods: "find", "filter", "maps", "array", "get", "uniq", "sort"

Methods of collection

The following methods will be available for the collection:

grizzly.json

{
  "collections": [
    {
      "name": "user",
      "types": {
        "id": "int",
        "name": "string"
      }
    }
  ]
}

by go generate

//grizzly:generate
type User struct {
	Id   int
	Name string
}
func NewUserCollection(items []*User) *UserCollection
func NewEmptyUserCollection() *UserCollection

func (c *UserCollection) Len() int

func (c *UserCollection) ForEach(callback func(item *User))

func (c *UserCollection) Filter(callback SearchCallbackUser) *UserCollection

func (c *UserCollection) Find(callback SearchCallbackUser) *User

func (c *UserCollection) Get(index int) (model *User)

func (c *UserCollection) MapToInt(callback func(item *User) int) []int

func (c *UserCollection) MapToString(callback func(item *User) string) []string

func (c *UserCollection) Pop() *User

func (c *UserCollection) Push(item *User) *UserCollection

func (c *UserCollection) Shift() *User

func (c *UserCollection) SortById(mode string) *UserCollection

func (c *UserCollection) SortByName(mode string) *UserCollection

func (c *UserCollection) UniqById() *UserCollection

func (c *UserCollection) UniqByName() *UserCollection

func (c *UserCollection) Unshift(item *User) *UserCollection

func (c *UserCollection) ForEach(callback func(item *User))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.