I have an internal JSON configuration file which I want to translate into pure Go code. For exercise sake, I want to generate a pure Go code with types and prefilled variables. I’ve found numerous online tools e.g. json-to-go, but they generate only types (JSON schemas), but not variables filled with actual data from JSON. Of course, I can unmarshal data from JSON every time, but from a practical point of use, I think it’s easier to have that configuration already in Go code, which can be easily modified in future. Do such tools even exist or are there better alternatives for having configuration directly in Go code?
I’m not sure I understand the question; can you provide a minimal example of what you’re looking for?
sorry, I should give an example initially, here is what I mean, from this JSON
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
after translating with https://mholt.github.io/json-to-go/ I’ve got this Go struct
type AutoGenerated struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
I’m looking for the data in addition to the above. I want another piece of Go code to be auto-generated, e.g.
varAutoGenerated := AutoGenerated{
UserID: 1,
ID: 1,
Title: "delectus aut autem",
Completed: false,
}
It looks like a trivial example to type it just once, but my real JSON config contains hundreds of lines with multilevel nested structs.
To my knowledge, here is the code below to convert the go language from “json” code
package main
import (
"encoding/json"
"fmt"
"log"
)
type AutoGenerated struct {
UserID int
ID int
Title string
Completed bool
}
func main(){
jsoncodes :=
`
{
"UserID": 1,
"ID": 1,
"Title": "delectus aut autem",
"Completed": false
}
`
a := new(AutoGenerated)
err := json.Unmarshal([]byte(jsoncodes),a)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n",a)//&{UserID:1 ID:1 Title:delectus aut autem Completed:false}//&{UserID:1 ID:1 Title:delectus aut autem Completed:false}
}
I’m going to leave out the details because I’m not very good at explaining.
I’m really sorry.
thank you @fea_kafea, unfortunately, fmt.Printf
doesn’t produce valid Go code, even in your example output, you can see it.
The closest solution I’ve found so far is the render.AsCode
function from this library https://github.com/gdexlab/go-render
here is the example https://play.golang.org/p/5mSaVRrnONy
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/gdexlab/go-render/render"
)
type AutoGenerated struct {
UserID int
ID int
Title string
Completed bool
}
func main() {
jsonStr :=
`
{
"UserID": 1,
"ID": 1,
"Title": "delectus aut autem",
"Completed": false
}
`
a := new(AutoGenerated)
err := json.Unmarshal([]byte(jsonStr), a)
if err != nil {
log.Fatal(err)
}
output := render.AsCode(a)
fmt.Println(output)
}
To complete the answer, I suggest (to myself from the past:) using json-to-go for the type generation and then (after unmarshaling) rendering the result as code.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.