parser

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ValidationText = `` /* 3601-byte string literal not displayed */

Functions

func GenerateGORMServices

func GenerateGORMServices(cfg *config.Config, structMetaData []StructMeta) (err error)

generateServices generates the service.go file

func GetModulePath

func GetModulePath(targetDir string) (string, error)

Returns the go import path for a given directory targetDir

func GetPreloadMap

func GetPreloadMap(structs []StructMeta) map[string][]string

GetPreloadMap returns a map of preload statements for each table. Preload statements are generated for all foreignKey fields and many2many fields. Forexample:

[User][]string{"Profile.Avatars"}

func Map

func Map(data []StructMeta) (m map[string]StructMeta)

Create a StructMeta map from slice.

Types

type Arg

type Arg struct {
	Name      string
	Type      string
	IsPointer bool
}

type Field

type Field struct {
	Name     string // Field Exact name
	Type     string // Full data type for the field
	BaseType string // Base type stripped of [] or pointers
	Tag      string // Unmodified struct tag.
	Preload  bool   // Whether this is a foreignKey or many2many field to preload.
	Parent   string // Parent struct Name
}

Field contains meta-data for each struct field.

type Function

type Function struct {
	Name        string
	Pkg         string
	Receiver    string
	PointerRecv bool
	Args        []Arg
	ReturnTypes []Arg
}

func ParseFunctions

func ParseFunctions(src string) (funcs []Function)

ParseFunctions parses Go source code and returns a slice of functions that are declared in the code. It supports functions with pointer receivers, variadic arguments, and return types that are not simple identifiers.

Returns: A slice of Function structs, each representing a function declared in the input code.

Example:

functions := ParseFunctions(`
  package main
  import "fmt"

  type Person struct {
      Name string
      Age  int
  }

  func (p *Person) Greet(other *Person, msg string) (greeting string, err error) {
      return fmt.Sprintf("%s says hello to %s", p.Name, other.Name), nil
  }

  func (p Person) Grow(n int) []int {
      return make([]int, n)
  }
`)

for _, fxn := range functions {
    fmt.Println(fxn.Name)
    fmt.Println(fxn.Pkg)
    fmt.Println(fxn.PointerRecv)
    for _, arg := range fxn.Args {
        fmt.Printf("%s %s %v\n", arg.Name, arg.Type, arg.IsPointer)
    }
    for _, ret := range fxn.ReturnTypes {
        fmt.Printf("%s %s %v\n", ret.Name, ret.Type, ret.IsPointer)
    }
}

type StructMeta

type StructMeta struct {
	Name    string  // Model name e.g User
	PKType  string  // PKType e.g int, int64 etc
	Fields  []Field // Fields for struct fields that are builtin(only)
	Package string  // Package name e.g "github.com/username/module/models"
	Skip    bool    // Skip generating service for this struct
}

StructMeta contains metadata about the struct generated by the go/ast.

func Parse

func Parse(modelPkgs []string) []StructMeta

Parse structs in package pkg and return Struct metadata about them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL