mirror of
https://github.com/opbnq-q/nto-cli.git
synced 2025-12-06 16:30:33 +07:00
feat: getting struct fields
This commit is contained in:
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