Skip to content

Commit

Permalink
Merge pull request #7 from OneOfOne/refactor
Browse files Browse the repository at this point in the history
Refactor

Fixes #6
Updates #4
  • Loading branch information
OneOfOne authored Aug 31, 2017
2 parents 6d64535 + e09bea0 commit cf38f87
Show file tree
Hide file tree
Showing 9 changed files with 751 additions and 454 deletions.
29 changes: 29 additions & 0 deletions .vscode/cSpell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// cSpell Settings
{
// Version of the setting file. Always 0.1
"version": "0.1",
// language - current active spelling language
"language": "en",
// words - list of words to be always considered correct
"words": [
"Iface",
"astrewrite",
"cmap",
"geireplacer",
"genny",
"genx",
"irepl",
"lmap",
"sflags",
"tions",
"tmpl",
"vals",
"xsel"
],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
"flagWords": [
"hte"
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.enabled": true
}
86 changes: 35 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* If you intend on generating files in the same package, you may add `// +build genx` to your template(s).
* Transparently handles [genny](https://github.com/cheekybits/genny)'s `generic.Type`.
* Supports a few [seeds](https://github.com/OneOfOne/genx/tree/master/seeds/).
* Adds build tags based on the types you pass, so you can target specifc types (ex: `// +build genx_t_string` or `// +build genx_vt_builtin` )
* 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.

Expand Down Expand Up @@ -52,7 +52,7 @@
➤ 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
```

### Modifing an external library that doesn't specifically support generics:
### Modifying an external library that doesn't specifically support generics:
Using [fatih](https://github.com/fatih)'s excellent [set](https://github.com/fatih/set) library:

```
Expand Down Expand Up @@ -150,7 +150,7 @@ For Example I needed to remove a field from the struct and change all usage of i
* Handle removing comments properly rather than using regexp.
* More seeds.
* ~~Add proper examples.~~
* ~~Support specalized functions by type.~~
* ~~Support specialized functions by type.~~
* ~~Support removing structs and their methods.~~

## Credits
Expand All @@ -160,54 +160,38 @@ For Example I needed to remove a field from the struct and change all usage of i

## Usage ([`cmd/genx`](https://github.com/OneOfOne/genx/tree/master/cmd/genx/main.go)):
```
usage: genx [-t T=type] [-s xx.xx=[yy.]yy] [-fld struct-field-to-remove] [-fn func-to-remove] [-tags "go build tags"]
[-m] [-n package-name] [-pkg input package] [-f input file] [-o output file or dir]
Types:
The -t/-s flags supports full package paths or short ones and letting goimports handle it.
-t "KV=string
-t "M=*cmap.CMap"
-t "M=github.com/OneOfOne/cmap.*CMap"
-s "cm.HashFn=github.com/OneOfOne/cmap/hashers#H.Fnv32"
-s "cm.HashFn=github.com/OneOfOne/cmap/hashers.Fnv32"
-s "cm.HashFn=hashers.Fnv32"
-t "RemoveThisType"
-fld "RemoveThisStructField,OtherField=NewFieldName"
-fn "RemoveThisFunc,OtherFunc=NewFuncName"
Examples:
genx -pkg github.com/OneOfOne/cmap -t "KT=interface{},VT=interface{}" -m -n cmap -o ./cmap.go
genx -f github.com/OneOfOne/cmap/lmap.go -t "KT=string,VT=int" -fn "NewLMap,NewLMapSize=NewStringInt" -n main -o ./lmap_string_int.go
genx -pkg github.com/OneOfOne/cmap -n stringcmap -t KT=string -t VT=interface{} -fld HashFn \
-fn DefaultKeyHasher -s "cm.HashFn=hashers.Fnv32" -m -o ./stringcmap/cmap.go
Flags:
-f file
file to parse
-fld field
struct fields to remove or rename (ex: -fld HashFn -fld priv=Pub)
-fn value
func`s to remove or rename (ex: -fn NotNeededFunc -fn Something=SomethingElse)
-get
go get the package if it doesn't exist
-goFlags flags
extra go get flags (ex: -goFlags '-t -race')
-n package name
package name sets the output package name, uses input package name if empty.
-o string
output dir if parsing a package or output filename if parsing a file (default "/dev/stdin")
-pkg package
package to parse
-s selector spec
selector specs to remove or rename (ex: -s 'cm.HashFn=hashers.Fnv32' -s 'x.Call=Something')
-seed <seed>
alias for -m -pkg github.com/OneOfOne/seeds/<seed>
-t type spec
generic type specs to remove or rename (ex: -t 'KV=string,KV=interface{}' -t RemoveThisType)
-tags tags
go build tags, used for parsing and automatically passed to go get.
-v verbose
➤ 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
Expand Down
47 changes: 34 additions & 13 deletions all_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,66 @@ package genx

import "github.com/cheekybits/genny/generic"

type KT generic.Type
type VT interface{}
type T generic.Type

type BothKT struct {
type (
KT interface{}
VT interface{}
)
type TypeWithKT struct {
K KT
V VT

Call func(k KT) VT
RemoveMeToo int // comment
Call func(k KT) VT
RemoveMe VT // RemoveMe comment

Iface interface{}
}

// RemoveMe comment
func (b *BothKT) RemoveMe() {
// MethodWithPtr comment
func (b *TypeWithKT) MethodWithPtr() {
b.K = new(KT)
b.V = new(VT)
}

// some comment
func (b BothKT) RemoveMe2() {
// MethodWithValue comment
func (b TypeWithKT) MethodWithValue() {
b.K = new(KT)
b.V = new(VT)
}

func DoParam(b *TypeWithKT) {}
func DoRes() *TypeWithKT { return nil }
func DoBoth(b *TypeWithKT) *TypeWithKT { return nil }

func DoStuff(k ...KT) VT {
var b BothKT
b := &TypeWithKT{
RemoveMe: nil,
}
return b.Call(k[0])
}

func DoStuffTwo(k ...KT) VT {
var b TypeWithKT
return b.RemoveMe
}

func ReturnVT() VT {
return nil
}

var (
m map[KT]VT
ktCh chan KT
vtCh chan VT
kvCh chan Both
kvCh chan TypeWithKT
ktA [100]KT
vtA [100]VT
a [100]Both
a [100]TypeWithKT
ktS []KT
vtS []VT
s []Both
s []*TypeWithKT
mp map[*TypeWithKT]interface{}
)

func XXX(vs ...interface{}) interface{} {
Expand Down
Loading

0 comments on commit cf38f87

Please sign in to comment.