Apollo 3d85cf58ea Updated README with more simplified examples | il y a 1 an | |
---|---|---|
example | il y a 1 an | |
LICENSE | il y a 1 an | |
README.md | il y a 1 an | |
astrut.go | il y a 1 an | |
go.mod | il y a 1 an | |
go.sum | il y a 1 an |
Auto structures in Go
go get git.red-green.com/david/astruct
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.
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)
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.
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.
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.
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
}
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)
}
}