Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
goiste committed Aug 16, 2022
0 parents commit 666918c
Show file tree
Hide file tree
Showing 6 changed files with 854 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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.

<br>

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

<br>

Literal types:

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

— set directly, w/o type castings or pointers

<br>

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)
58 changes: 58 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"

gp "github.com/goiste/goparser"
)

func main() {
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"
}

floatValues := gp.GetBasicValues[float64](p, "parser")
for _, v := range floatValues {
fmt.Printf("name: %s; value: %.02f\n", v.Name, v.Value) // name: float64Value; value: 3.14
}

floatSliceValues := gp.GetSliceValues[float64](p, "parser")
for _, v := range floatSliceValues {
fmt.Printf("name: %s; values: %v\n", v.Name, v.Value) // name: float64SliceValue; values: [3.14 0.42]
}

stringSliceValues := gp.GetSliceValues[string](p, "parser", "parser:str")
for _, v := range stringSliceValues {
fmt.Printf("name: %s; values: %v\n", v.Name, v.Value) // name: stringSliceValue; values: [a b c]
}

stringMapValues := gp.GetMapValues[string, string](p, "parser", "parser:str")
for _, v := range stringMapValues {
fmt.Printf("name: %s; values: %+v\n", v.Name, v.Value) // name: stringToStringMapValue; values: map[a:1 b:2]
}

floatMapValues := gp.GetMapValues[int64, float64](p, "parser")
for _, v := range floatMapValues {
fmt.Printf("name: %s; values: %+v\n", v.Name, v.Value) // name: intToFloat64MapValue; values: map[3:3.14 17:42]
}

nms := gp.GetFuncNames(p, "LocalStruct", "Context", "string")
fmt.Printf("%v\n", nms) // [usefulFunc1 usefulFunc2 usefulFunc3]

nms = gp.GetFuncNames(p, "LocalStruct", "Context")
fmt.Printf("%v\n", nms) // [usefulFunc1 usefulFunc2 usefulFunc3]

nms = gp.GetFuncNames(p, "", "Context")
fmt.Printf("%v\n", nms) // [usefulFunc4]
}
71 changes: 71 additions & 0 deletions example/example_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"context"
)

const floatConst = 0.0

var (
// parser
boolValue = true

// parser:str
stringValue = "3"

// parser
intValue = 3

notParsedInValue = 17

// parser
float64Value = 3.14

// parser
float32Value = float32(3.14) // not implemented yet

// parser
notParsedFloatValue = floatConst // not implemented yet

// parser
float64SliceValue = []float64{3.14, 0.42}

// parser:str
stringSliceValue = []string{"a", "b", "c"}

// parser:str
stringToStringMapValue = map[string]string{"a": "1", "b": "2"}

// parser
pointerMapValue = &map[string]string{} // not implemented yet

// parser
intToFloat64MapValue = map[int]float64{3: 3.14, 17: 42.0}

_, _, _, _, _, _, _, _, _, _, _ = stringValue, intValue, notParsedInValue, float64Value, float32Value,
notParsedFloatValue, float64SliceValue, stringSliceValue, stringToStringMapValue, pointerMapValue,
intToFloat64MapValue
)

type LocalStruct struct{}

func (s *LocalStruct) usefulFunc1(ctx *context.Context, str string) bool {
_, _ = ctx, str
if boolValue {
boolValue = !boolValue
}
return boolValue
}

func (s LocalStruct) usefulFunc2(ctx *context.Context, str string) {
_, _ = ctx, str
}

func (s LocalStruct) usefulFunc3(ctx context.Context, str string) {
usefulFunc4(ctx, &str)
}

func usefulFunc4(ctx context.Context, str *string) {
_, _ = ctx, str
_ = LocalStruct{}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/goiste/goparser

go 1.18
Loading

0 comments on commit 666918c

Please sign in to comment.