An open API service indexing awesome lists of open source software.

https://github.com/goiste/goparser

Parse Go files by annotation in doc comment
https://github.com/goiste/goparser

ast ast-tree go go-parser golang parser

Last synced: 2 months ago
JSON representation

Parse Go files by annotation in doc comment

Awesome Lists containing this project

README

        

# Go Parser

A small package that helps you parse Go files by annotation in doc comment, e.g. get var values or list of struct
methods.


Features:

- get values of variables:
- literal types
- slices of literal types
- maps with literal types as keys and values
- get list of function names:
- by method receiver type
- by parameters types


Literal types:

- bool
- string
- int8-int64
- uint8-uint64
- float32/64

— set directly, w/o type castings or pointers


Usage:

parsed code:

```go
var (
// parser
boolValue = true

// parser:str
stringValue = "3"
```

[full example_code.go](example/example_code.go)

main code:

```go
import (
gp "github.com/goiste/goparser"
)

...

p, err := gp.New("example_code.go")
if err != nil {
panic(err)
}

boolValues := gp.GetBasicValues[bool](p, "parser")
for _, v := range boolValues {
fmt.Printf("name: %s; value: %t\n", v.Name, v.Value) // name: boolValue; value: true
}

stringValues := gp.GetBasicValues[string](p, "parser", "parser:str")
for _, v := range stringValues {
fmt.Printf("name: %s; value: %q\n", v.Name, v.Value) // name: stringValue; value: "3"
}
```

[full example.go](example/example.go)