Browse Source

Updated README with more simplified examples

Also made VerboseLogging actually use log, and support not logging when
it's set false.
Apollo 1 year ago
parent
commit
3d85cf58ea
2 changed files with 90 additions and 20 deletions
  1. 81 14
      README.md
  2. 9 6
      astrut.go

+ 81 - 14
README.md

@@ -1,24 +1,91 @@
-# astruct
+# Astruct
 
 Auto structures in Go
 
-## BUGS
+`go get git.red-green.com/david/astruct`
 
-* Currently we assume if 1 type is able to be int64 then we use int64, but it might not be able to (it might actually be float64)
-* Need to fix it so if AssumeStruct is true then we make structures (always), else we only make a structure if the types vary
-* Fix it so if we make a structure (because AssumeStruct is true) then anywhere we'd have the same field use that structure instead
-* Fix numeric structure names when there is no prefix
-* Verify this is valid go code
+This project was built to develop the highly complex, nested JSON becoming easy to navigate with structures pre-built for use, with minimal user coding required.
 
-## To Remove
+## Usage
 
-* UseInterface (because we already expect go1.20.3, rather what we built with) use `any` rather than `interface{}` adaptable
+Just feed it a json file and given filename and package name it will generate valid Go structures to access all that data that's in JSON. (Need a short example code snippet? Jump to [Example: Calling](#example-calling))
 
-## Missing Features
+Now all your data won't be 100% perfect, I assume you'll run this to make the initial structs then stop running this and modify the generated Go structures manually.
 
-* Build a better WriteFile where it generates each structure then when we do a final WriteFile we merge all that together (basically optimize the WriteFile process so it's cleaner and less huge)
+> Sometimes Auto struct (Astruct, astruct) get's the type blatantly wrong, I expect the user of this project to look over the generated output, **always look over the output**.
 
-## Gothonic
+## Additional comments
 
-* Work on breaking down the many complex and huge methods/functions and get a series of smaller methods/functions built
-* Use `any` instead of `interface{}`, and drop old versions of Go.
+In the example directory I also include a normal.json, this was early development test JSON, but is more favorable to use the newer test.json as it's more complex and shows how Astruct can determine some types that are required.
+
+## TODO
+
+* Optimize the WriteFile process so it's cleaner and less huge
+
+## Example: Calling
+
+```go
+package main
+
+import (
+    "fmt"
+
+    "git.red-green.com/david/astruct"
+)
+
+func main() {
+    // I'll use the default Astruct settings
+    // Which you can see in astruct.Astruct.Defaults
+    var a *astruct.Astruct = astruct.NewAstruct("Weather")
+    // You'd change any settings here, don't do it before
+    // as NewAstruct calls Init and it calls Defaults
+    var err error = a.ReadFile("test.json") // Ok, let's parse a JSON file
+    if err != nil {
+        fmt.Println("Err:", err)
+        return
+    }
+    // And let's Write out Go code
+    err = a.WriteFile("output.go", "main", 0666)
+    if err != nil {
+        fmt.Println("Err:", err)
+        return
+    }
+    // Technically when you run this again it re-parses the JSON,
+    // You'd only need to parse it again if it's changed (structure
+    // wise, not any of it's data)
+    // Or your ready to take this generated Go code and move it to
+    // your project that's needed to make use of it
+}
+```
+
+## Example: In depth debugging
+
+```go
+package main
+
+import (
+    "fmt"
+    "log"
+
+    "git.red-green.com/david/astruct"
+)
+
+func main() {
+    // Like Example: Calling, I'll use the Defaults
+    var a *astruct.Astruct = astruct.NewAstruct("Weather")
+    // Modified the Verbose Logging setting
+    a.VerboseLogging = true // Enable logging output
+    // The above will print to logs information of what Astruct
+    // saw in the given JSON, without it it will keep this info
+    // quiet.
+    var err error = a.ReadFile("test.json") // Parse
+    if err != nil {
+        log.Panic("Err:", err)
+    }
+    // Generate Output
+    err = a.WriteFile("output.go", "main", 0666)
+    if err != nil {
+        log.Panic("Err:", err)
+    }
+}
+```

+ 9 - 6
astrut.go

@@ -89,7 +89,9 @@ func (A *Astruct) parse(at any, name string) error {
 		var last_type reflect.Kind = 0
 		var same_type bool = true
 		for k, v := range m {
-			fmt.Printf(strings.Repeat("  ", int(A.Depth))+"'%s' = %s\n", k, reflect.TypeOf(v).Kind().String())
+			if A.VerboseLogging {
+				log.Printf(strings.Repeat("  ", int(A.Depth))+"'%s' = %s", k, reflect.TypeOf(v).Kind().String())
+			}
 			if same_type {
 				if last_type == 0 {
 					last_type = reflect.TypeOf(v).Kind()
@@ -148,7 +150,9 @@ func (A *Astruct) parse(at any, name string) error {
 		var last_type reflect.Kind = 0
 		var same_type bool = true
 		for i, v := range a {
-			fmt.Printf(strings.Repeat("  ", int(A.Depth))+"%d = %s\n", i, reflect.TypeOf(v).Kind().String())
+			if A.VerboseLogging {
+				log.Printf(strings.Repeat("  ", int(A.Depth))+"%d = %s", i, reflect.TypeOf(v).Kind().String())
+			}
 			if same_type {
 				if last_type == 0 {
 					last_type = reflect.TypeOf(v).Kind()
@@ -201,7 +205,9 @@ func (A *Astruct) parse(at any, name string) error {
 			}
 		}
 	default:
-		fmt.Printf(strings.Repeat("  ", int(A.Depth))+"%s\n", reflect.TypeOf(at).Kind().String())
+		if A.VerboseLogging {
+			log.Printf(strings.Repeat("  ", int(A.Depth))+"%s", reflect.TypeOf(at).Kind().String())
+		}
 	}
 	return nil
 }
@@ -209,7 +215,6 @@ func (A *Astruct) parse(at any, name string) error {
 // Checks if the given JSON Number (represented as float64) can be a int64 instead
 func IsInt(v float64) bool {
 	str := fmt.Sprintf("%f", v)
-	fmt.Printf("'%s' = ", str)
 	var hit_dot bool = false
 	for _, r := range str {
 		if r == '.' {
@@ -218,12 +223,10 @@ func IsInt(v float64) bool {
 		}
 		if hit_dot {
 			if r != '0' {
-				fmt.Println("false")
 				return false
 			}
 		}
 	}
-	fmt.Println("true")
 	return true
 }