-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 666918c
Showing
6 changed files
with
854 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
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,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) |
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,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] | ||
} |
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,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{} | ||
} |
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,3 @@ | ||
module github.com/goiste/goparser | ||
|
||
go 1.18 |
Oops, something went wrong.