|
@@ -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)
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|