mirror of
https://github.com/opbnq-q/nto-cli.git
synced 2025-12-06 18:40:34 +07:00
feat: getting struct fields
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user