-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from widmogrod/feature/name
Version 2.0
- Loading branch information
Showing
27 changed files
with
1,302 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"context" | ||
"github.com/urfave/cli/v2" | ||
"github.com/widmogrod/mkunion" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"path" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
var output = flag.String("output", "-", "Output file for generated code") | ||
var types = flag.String("types", "", "Comma separated list of golang types to generate union for") | ||
var name = flag.String("name", "", "Name of the union type") | ||
var packageName = flag.String("packageName", "main", "go package name") | ||
|
||
func main() { | ||
flag.Parse() | ||
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
|
||
g := mkunion.Generate{ | ||
Types: strings.Split(*types, ","), | ||
Name: *name, | ||
PackageName: *packageName, | ||
} | ||
var app *cli.App | ||
app = &cli.App{ | ||
Name: mkunion.Program, | ||
Description: "VisitorGenerator union type and visitor pattern gor golang", | ||
EnableBashCompletion: true, | ||
DefaultCommand: "golang", | ||
UseShortOptionHandling: true, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Aliases: []string{"n", "variant"}, | ||
Required: true, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "types", | ||
Aliases: []string{"t"}, | ||
Required: true, | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
cwd, _ := syscall.Getwd() | ||
sourceName := path.Base(os.Getenv("GOFILE")) | ||
sourcePath := path.Join(cwd, sourceName) | ||
|
||
result, err := g.Generate() | ||
if err != nil { | ||
panic(err) | ||
baseName := strings.TrimSuffix(sourceName, path.Ext(sourceName)) | ||
|
||
// file name without extension | ||
inferred, err := mkunion.InferFromFile(sourcePath) | ||
if err != nil { | ||
return err | ||
} | ||
visitor := mkunion.VisitorGenerator{ | ||
Name: c.String("name"), | ||
Types: strings.Split(c.String("types"), ","), | ||
PackageName: inferred.PackageName, | ||
} | ||
|
||
reducer := mkunion.ReducerGenerator{ | ||
Name: visitor.Name, | ||
Types: visitor.Types, | ||
PackageName: inferred.PackageName, | ||
Branches: inferred.ForVariantType(visitor.Name, visitor.Types), | ||
} | ||
|
||
defaultVisitor := mkunion.VisitorDefaultGenerator{ | ||
Name: visitor.Name, | ||
Types: visitor.Types, | ||
PackageName: inferred.PackageName, | ||
} | ||
|
||
generators := []struct { | ||
gen mkunion.Generator | ||
name string | ||
}{ | ||
{gen: &visitor, name: "visitor"}, | ||
{gen: &reducer, name: "reducer"}, | ||
{gen: &defaultVisitor, name: "default_visitor"}, | ||
} | ||
for _, g := range generators { | ||
b, err := g.gen.Generate() | ||
if err != nil { | ||
return err | ||
} | ||
err = ioutil.WriteFile(path.Join(cwd, | ||
baseName+"_"+mkunion.Program+"_"+g.name+".go"), b, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
err = ioutil.WriteFile(*output, result, 0644) | ||
err := app.RunContext(ctx, os.Args) | ||
if err != nil { | ||
panic(err) | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package example | ||
|
||
//go:generate go run ../cmd/mkunion/main.go -name=Vehicle -types=Plane,Car,Boat | ||
type ( | ||
Car struct{} | ||
Plane struct{} | ||
Boat struct{} | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.