Skip to main content

go-codegen

Everywhere, under @mathbalduino

   
package main
import (  "fmt"  "github.com/mathbalduino/go-codegen"  "go/types")
func main() {  parser, e := parser.NewGoParser("#your_pattern_string#", parser.Config{})  if e != nil {    panic(e)  }    // Will print the name of every struct inside the parsed code  parser.IterateStructs(func(struct_ *types.TypeName, logger parser.LoggerCLI) error {    fmt.Println(struct_.Name())    return nil  })}

Parse GO Structs

Create a new instance of the GO parser and you will be able to parse and gather information about the structs inside your code, using the IterateStructs

package main
import (  "fmt"  "github.com/mathbalduino/go-codegen"  "go/types")
func main() {  parser, e := parser.NewGoParser("#your_pattern_string#", parser.Config{})  if e != nil {    panic(e)  }    // Will print the name of every interface inside the parsed code  parser.IterateInterfaces(func(interface_ *types.TypeName, logger parser.LoggerCLI) error {    fmt.Println(interface_.Name())    return nil  })}

Parse GO Interfaces

You can gather information about the interfaces too, just use the IterateInterfaces

package main
import (  "fmt"  "github.com/mathbalduino/go-codegen")
func main() {  // Will skip any interface that doesn't have its name  // equal to "SomeInterface"  cfg := parser.Config{    Focus: &parser.FocusTypeName("SomeInterface")  }    parser, e := parser.NewGoParser("#your_pattern_string#", cfg)  if e != nil {    panic(e)  }    parser.IterateInterfaces(func(interface_ *types.TypeName, logger parser.LoggerCLI) error {    // Callback will be executed only if the interface name    // is equal to "SomeInterface"    fmt.Println(interface_.Name())        return nil  })}

Focus

If you don't want to iterate over all the parsed code, you can use the Focus feature to skip files, packages or typenames

package main
import (  "fmt"  "github.com/mathbalduino/go-codegen"  "github.com/mathbalduino/go-codegen/goFile")
func main() {  parser, e := parser.NewGoParser("#your_pattern_string#", parser.Config{})  if e != nil {    panic(e)  }  // Will create the "exampleFile" inside the "#some_pkg_name#" package, using the "#the_some_pkg_path#" package import path  file := goFile.New("exampleFile", "#some_pkg_name#", "#the_some_pkg_path#")  parser.IterateInterfaces(func(interface_ *types.TypeName, logger parser.LoggerCLI) error {    // Just an example of an extremely simplified code generation. The generated    // file will have a constant string with the name of every parsed interface    file.AddCode(fmt.Sprintf("const InterfaceName_%s = \"%s\"\n", interface_.Name(), interface_.Name()))    return nil  })  // The file will be saved to the "#some_folder_path" folder path  // The "#some_title#" will be written to the file header (comment section)  e = file.Save("#some_title#", "#some_folder_path")  if e != nil {    panic(e)  }}

Files abstraction

The library comes with builtin support for GO and TS files, including formatting, import list handling (trust me, you don't want to handle it) and persistence