Auto structures in Go

Apollo 3d85cf58ea Updated README with more simplified examples 1 year ago
example 766ec742b1 Real world JSON example works 1 year ago
LICENSE 9d5d6042e6 Initial commit 1 year ago
README.md 3d85cf58ea Updated README with more simplified examples 1 year ago
astrut.go 3d85cf58ea Updated README with more simplified examples 1 year ago
go.mod dc6b2355ba Added current state of POC Astruct (Auto Struct) 1 year ago
go.sum dc6b2355ba Added current state of POC Astruct (Auto Struct) 1 year ago

README.md

Astruct

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.

Usage

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.

Additional comments

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

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

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)
    }
}