commit 653c667e5777781589c61bd36a0ecbec6449d927 Author: opbnq-q Date: Tue Feb 11 18:51:25 2025 +0700 feat: getting struct fields diff --git a/cmd/input.go b/cmd/input.go new file mode 100644 index 0000000..cd122cd --- /dev/null +++ b/cmd/input.go @@ -0,0 +1,9 @@ +package input + +import "fmt" + +func Input() (string, string) { + var structName, path string + fmt.Scan(&structName, &path) + return structName, path +} \ No newline at end of file diff --git a/example_structs/post.go b/example_structs/post.go new file mode 100644 index 0000000..06104ca --- /dev/null +++ b/example_structs/post.go @@ -0,0 +1,5 @@ +package structs + +type Post struct { + title string `json sex` +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0332167 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module nto_cli + +go 1.23.4 diff --git a/main b/main new file mode 100755 index 0000000..8611b66 Binary files /dev/null and b/main differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..7c753ce --- /dev/null +++ b/main.go @@ -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) +} \ No newline at end of file diff --git a/types/field.go b/types/field.go new file mode 100644 index 0000000..1385791 --- /dev/null +++ b/types/field.go @@ -0,0 +1,7 @@ +package types + +type Field struct { + Name string + Type string + Medatada []string +} diff --git a/utils/contains_many.go b/utils/contains_many.go new file mode 100644 index 0000000..ba7dfaf --- /dev/null +++ b/utils/contains_many.go @@ -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) +} \ No newline at end of file diff --git a/utils/get_struct_fields.go b/utils/get_struct_fields.go new file mode 100644 index 0000000..dd938a1 --- /dev/null +++ b/utils/get_struct_fields.go @@ -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 +} \ No newline at end of file diff --git a/utils/split_struct_field.go b/utils/split_struct_field.go new file mode 100644 index 0000000..fdb9fd9 --- /dev/null +++ b/utils/split_struct_field.go @@ -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, + } +} \ No newline at end of file