mirror of
https://github.com/opbnq-q/nto-cli.git
synced 2025-12-06 20:20:34 +07:00
feat: getting struct fields
This commit is contained in:
9
cmd/input.go
Normal file
9
cmd/input.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func Input() (string, string) {
|
||||||
|
var structName, path string
|
||||||
|
fmt.Scan(&structName, &path)
|
||||||
|
return structName, path
|
||||||
|
}
|
||||||
5
example_structs/post.go
Normal file
5
example_structs/post.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package structs
|
||||||
|
|
||||||
|
type Post struct {
|
||||||
|
title string `json sex`
|
||||||
|
}
|
||||||
23
main.go
Normal file
23
main.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
input "nto_cli/cmd"
|
||||||
|
"nto_cli/utils"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Print("struct name, path to file (including struct): ")
|
||||||
|
structName, path := input.Input()
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
structFields := utils.GetStructFields(file, structName)
|
||||||
|
|
||||||
|
fmt.Println(structFields)
|
||||||
|
}
|
||||||
7
types/field.go
Normal file
7
types/field.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
type Field struct {
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
Medatada []string
|
||||||
|
}
|
||||||
15
utils/contains_many.go
Normal file
15
utils/contains_many.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func ContainsMany(str string, substrs... string) bool {
|
||||||
|
count := 0
|
||||||
|
for _, substr := range substrs {
|
||||||
|
if strings.Contains(str, substr) {
|
||||||
|
count++;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count == len(substrs)
|
||||||
|
}
|
||||||
39
utils/get_struct_fields.go
Normal file
39
utils/get_struct_fields.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"nto_cli/types"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetStructFields(file *os.File, structName string) []types.Field {
|
||||||
|
bracketsCount := 1
|
||||||
|
|
||||||
|
structFound := false
|
||||||
|
|
||||||
|
structFields := []types.Field{}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for i := 1; scanner.Scan() && bracketsCount > 0; i++ {
|
||||||
|
line := scanner.Text()
|
||||||
|
if ContainsMany(line, structName, "type", "struct") {
|
||||||
|
structFound = true
|
||||||
|
}
|
||||||
|
if structFound {
|
||||||
|
bracketsCount += strings.Count(line, "{")
|
||||||
|
bracketsCount -= strings.Count(line, "}")
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
newField := SplitStructField(line)
|
||||||
|
if newField != nil {
|
||||||
|
structFields = append(structFields, *newField)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return structFields
|
||||||
|
}
|
||||||
38
utils/split_struct_field.go
Normal file
38
utils/split_struct_field.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"nto_cli/types"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SplitStructField(field string) *types.Field {
|
||||||
|
if strings.Contains(field, "type") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
startBacktip := strings.Index(field, "`")
|
||||||
|
var metadata []string
|
||||||
|
if startBacktip > -1 {
|
||||||
|
metadata = []string{field[startBacktip:]}
|
||||||
|
} else {
|
||||||
|
startBacktip = len(field)
|
||||||
|
}
|
||||||
|
|
||||||
|
field = strings.TrimSpace(field[:startBacktip])
|
||||||
|
|
||||||
|
data := strings.Split(field, " ")
|
||||||
|
|
||||||
|
if len(data) < 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
name := strings.TrimSpace(data[0])
|
||||||
|
|
||||||
|
dataType := strings.TrimSpace(data[1])
|
||||||
|
|
||||||
|
return &types.Field{
|
||||||
|
Medatada: metadata,
|
||||||
|
Type: dataType,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user