All Projects → OneOfOne → genx

OneOfOne / genx

Licence: Apache-2.0 license
GenX: Generics For Go, Yet Again.

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to genx

go-generics
Go的泛型文档、代码、讨论
Stars: ✭ 43 (+16.22%)
Mutual labels:  generics
generics
Deprecated! See https://github.com/golang-design/go2generics.
Stars: ✭ 26 (-29.73%)
Mutual labels:  generics
ttlcache
An in-memory cache with item expiration and generics
Stars: ✭ 468 (+1164.86%)
Mutual labels:  generics
go-ringbuf
Lock-free MPMC Ring Buffer (Generic) for SMP, in golang. Some posts in chinese:
Stars: ✭ 43 (+16.22%)
Mutual labels:  generics
Java-Programs
Java Practiced Problems including concepts of OOPS, Interface, String , Collection.
Stars: ✭ 51 (+37.84%)
Mutual labels:  generics
ViewControllersFactory
Instantiate your ViewControllers easily with this small factory class
Stars: ✭ 26 (-29.73%)
Mutual labels:  generics
Arrow Meta
Functional companion to Kotlin's Compiler
Stars: ✭ 246 (+564.86%)
Mutual labels:  generics
rueidis
A Fast Golang Redis RESP3 client that supports Client Side Caching, Auto Pipelining, Generics OM, RedisJSON, RedisBloom, RediSearch, RedisAI, RedisGears, etc.
Stars: ✭ 422 (+1040.54%)
Mutual labels:  generics
parco
🏇🏻 generalist, fast and tiny binary parser and compiler generator, powered by Go 1.18+ Generics
Stars: ✭ 57 (+54.05%)
Mutual labels:  generics
ConfuserExPlugins
Transforms all types to generics, and all constructor calls and method calls into generic call factories.
Stars: ✭ 32 (-13.51%)
Mutual labels:  generics
rust-amplify
Amplifying Rust language capabilities: multiple generic trait implementations, type wrappers, bit-precise numerics, derive macros
Stars: ✭ 38 (+2.7%)
Mutual labels:  generics
Java-Interview-Programs
Core Java Projects with complete source code
Stars: ✭ 48 (+29.73%)
Mutual labels:  generics
heidi
heidi : tidy data in Haskell
Stars: ✭ 24 (-35.14%)
Mutual labels:  generics
lancet
A comprehensive, efficient, and reusable util function library of go.
Stars: ✭ 2,228 (+5921.62%)
Mutual labels:  generics
SwiftRadix
Easily convert integers to binary/hex/octal strings and back again with clean functional syntax.
Stars: ✭ 34 (-8.11%)
Mutual labels:  generics
Gengen
A Go source transformation tool for generics
Stars: ✭ 253 (+583.78%)
Mutual labels:  generics
truthy
Package truthy provides truthy condition testing with Go generics
Stars: ✭ 31 (-16.22%)
Mutual labels:  generics
go-callbag
golang implementation of Callbag
Stars: ✭ 19 (-48.65%)
Mutual labels:  generics
Stack
A Type-Safe, Thread-Safe-ish approach to CoreData in Swift
Stars: ✭ 47 (+27.03%)
Mutual labels:  generics
go2generics
🧪 A chunk of experiments and demos about Go 2 generics design (type parameter & type set)
Stars: ✭ 146 (+294.59%)
Mutual labels:  generics

GenX : Generics For Go, Yet Again.

GoDoc Build Status Report Card

Install

go get github.com/OneOfOne/genx/...

Features

  • It can be easily used with go generate, from the command line or as a library.
  • cmd/genx Uses local files, packages, and optionally uses go get (with the -get flag) if the remote package doesn't exist.
  • You can rewrite, remove and change pretty much everything.
  • Allows you to merge a package of multiple files into a single one.
  • Safely remove functions and struct fields.
  • Automatically passes all code through x/tools/imports (aka goimports).
  • If you intend on generating files in the same package, you may add // +build genx to your template(s).
  • Transparently handles genny's generic.Type.
  • Supports a few seeds.
  • Adds build tags based on the types you pass, so you can target specific types (ex: // +build genx_t_string or // +build genx_vt_builtin )
  • Automatically handles nil returns, will return the zero value of the type.
  • Doesn't need modifying the source package if there's only one type involved.

Examples:

Package:

//go:generate genx -pkg ./internal/cmap -t KT=interface{} -t VT=interface{} -m -o ./cmap.go
//go:generate genx -pkg ./internal/cmap -n stringcmap -t KT=string -t VT=interface{} -fld HashFn -fn DefaultKeyHasher -s "cm.HashFn=hashers.Fnv32" -m -o ./stringcmap/cmap.go

Advanced type targeting:

Input:

Output:

Single File:

➤ genx -f github.com/OneOfOne/cmap/lmap.go -t "KT=string,VT=int" -fn "NewLMap,NewLMapSize=NewStringInt" -n main -v -o ./lmap_string_int.go

Modifying an external library that doesn't specifically support generics:

Using fatih's excellent set library:

# -fn IntSlice -fnStringSlice to remove unneeded functions.
➤ genx -pkg github.com/fatih/set -t 'interface{}=uint64' -fn IntSlice -fn StringSlice -v -n uint64set -o ./uint64set

Target native types with a fallback: seeds/sort

➤ genx -seed sort -t T=string -n main
...
func SortStrings(s []string, reverse bool) { ... }
...
➤ genx -seed sort -t T=*pkg.OtherType -n main
...
func SortPkgTypes(s []string, less func(i, j int) bool) { ... }
...

Sets: seeds/set

package set

type KeyType interface{}

type KeyTypeSet map[KeyType]struct{}

func NewKeyTypeSet() KeyTypeSet { return KeyTypeSet{} }

func (s KeyTypeSet) Add(vals ...KeyType) {
	for i := range vals {
		s[vals[i]] = struct{}{}
	}
}

func (s KeyTypeSet) Has(val KeyType) (ok bool) {
	_, ok = s[val]
	return
}

func (s KeyTypeSet) Delete(vals ...KeyType) {
	for i := range vals {
		delete(s, vals[i])
	}
}

func (s KeyTypeSet) Keys() (out []KeyType) {
	out = make([]KeyType, 0, len(s))
	for k := range s {
		out = append(out, k)
	}
	return
}
  • Command: genx -seed set -t KeyType=string -fn Keys

  • Output:

package set

type StringSet map[string]struct{}

func NewStringSet() StringSet { return StringSet{} }

func (s StringSet) Add(vals ...string) {
	for i := range vals {
		s[vals[i]] = struct{}{}
	}
}

func (s StringSet) Has(val string) (ok bool) {
	_, ok = s[val]
	return
}

func (s StringSet) Delete(vals ...string) {
	for i := range vals {
		delete(s, vals[i])
	}
}

FAQ

Why?

Mostly a learning experience, also I needed it and the other options available didn't do what I needed.

For Example I needed to remove a field from the struct and change all usage of it for stringcmap.

TODO

  • Documentation.
  • Documention for using the library rather than the commandline.
  • Support package tests.
  • Handle removing comments properly rather than using regexp.
  • More seeds.
  • Add proper examples.
  • Support specialized functions by type.
  • Support removing structs and their methods.

Credits

BUGS

Usage (cmd/genx):

➤ genx -h
NAME:
   genx - Generics For Go, Yet Again.

USAGE:
   genx [global options] command [command options] [arguments...]

VERSION:
   v0.5

AUTHOR:
   Ahmed <OneOfOne> W. <oneofone+genx <a.t> gmail <dot> com>

COMMANDS:
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --seed seed-name                  alias for -pkg github.com/OneOfOne/genx/seeds/seed-name
   --in file, -f file                file to process, use `-` to process stdin.
   --package package, --pkg package  package to process.
   --name name, -n name              package name to use for output, uses the input package's name by default.
   --type type, -t type              generic type names to remove or rename (ex: -t 'KV=string,KV=interface{}' -t RemoveThisType).
   --selector selector, -s selector  selectors to remove or rename (ex: -s 'cm.HashFn=hashers.Fnv32' -s 'x.Call=Something').
   --field field, --fld field        struct fields to remove or rename (ex: -fld HashFn -fld privateFunc=PublicFunc).
   --func func, --fn func            functions to remove or rename (ex: -fn NotNeededFunc -fn Something=SomethingElse).
   --out value, -o value             output dir if parsing a package or output filename if you want the output to be merged. (default: "/dev/stdout")
   --tags value                      go extra build tags, used for parsing and automatically passed to any go subcommands.
   --goFlags flags                   extra flags to pass to go subcommands flags (ex: --goFlags '-race')
   --get                             go get the package if it doesn't exist (default: false)
   --verbose, -v                     verbose output (default: false)
   --help, -h                        show help (default: false)
   --version, -V                     print the version (default: false)

Contributions

  • All contributions are welcome, just open a pull request.

License

Apache v2.0 (see LICENSE file).

Copyright 2016-2017 Ahmed <OneOfOne> W.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
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].