How to access a struct array through a pointer? - go

Following are my 2 structs
type Attempt struct {
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
LastUpdated string `json:"lastUpdated"`
Duration uint32 `json:"duration"`
SparkUser string `json:"sparkUser"`
IsCompleted bool `json:"completed"`
LastUpdatedEpoch int64 `json:"lastUpdatedEpoch"`
StartTimeEpoch int64 `json:"startTimeEpoch"`
EndTimeEpoch int64 `json:"EndTimeEpoch"`
}
type Apps struct {
Id string `json:"id"`
Name string `json:"name"`
Attempts []Attempt `json:"attempts"`
}
The following test parses a json string into this apps := &[]Apps{}. When accessing the members of apps, I am getting the following error
invalid operation: apps[0] (type *[]Apps does not support indexing)
The test
func TestUnmarshalApps(t *testing.T) {
appsJson := `[
{
"id": "app-20161229224238-0001",
"name": "Spark shell",
"attempts": [
{
"startTime": "2016-12-30T03:42:26.828GMT",
"endTime": "2016-12-30T03:50:05.696GMT",
"lastUpdated": "2016-12-30T03:50:05.719GMT",
"duration": 458868,
"sparkUser": "esha",
"completed": true,
"endTimeEpoch": 1483069805696,
"lastUpdatedEpoch": 1483069805719,
"startTimeEpoch": 1483069346828
},
{
"startTime": "2016-12-30T03:42:26.828GMT",
"endTime": "2016-12-30T03:50:05.696GMT",
"lastUpdated": "2016-12-30T03:50:05.719GMT",
"duration": 458868,
"sparkUser": "esha",
"completed": true,
"endTimeEpoch": 1483069805696,
"lastUpdatedEpoch": 1483069805719,
"startTimeEpoch": 1483069346828
}
]
},
{
"id": "app-20161229222707-0000",
"name": "Spark shell",
"attempts": [
{
"startTime": "2016-12-30T03:26:50.679GMT",
"endTime": "2016-12-30T03:38:35.882GMT",
"lastUpdated": "2016-12-30T03:38:36.013GMT",
"duration": 705203,
"sparkUser": "esha",
"completed": true,
"endTimeEpoch": 1483069115882,
"lastUpdatedEpoch": 1483069116013,
"startTimeEpoch": 1483068410679
}
]
}
]`
apps := &[]Apps{}
err := json.Unmarshal([]byte(appsJson), apps)
if err != nil {
t.Fatal(err)
}
if len(*apps) != 2 {
t.Fail()
}
if len(apps[0].Attempts) != 2 {
t.Fail()
}
}
How to access the fields Attempts, Id etc.?

apps := &[]Apps{}
apps has type *[]Apps (pointer to slice of Apps objects).
Are you sure you didn't mean to use the type []*Apps (slice of pointers to Apps objects)?
Assuming *[]Apps really is the type you intended, you'd need to use (*apps)[i] to access every element of apps. That type is also the reason why you also need to use len(*apps) instead of len(apps) (and *apps for pretty much everything actually).

Related

How to parse JSON-RPC table with different type

I want get informations in JSON-RPC file with this structure :
{
"id": "foo1",
"error": null,
"result": [
{
"key": [
"hello 1",
1,
"world 1"
],
"val": {
"type": "static"
}
},
{
"key": [
"hello 2",
1,
"world 2"
],
"val": {
"type": "static"
}
}
]
}
This is my parsing function, Key is string table (can't accept int type) :
type JsonRpcRsp struct {
Id string `json:"id"`
Error *string `json:"error"`
Result json.RawMessage `json:"result"`
}
type JsonRpcEntry_Val struct {
Type string `json:"type"`
}
type JsonRpcEntry struct {
Key [3]string `json:"key"`
Val JsonRpcEntry_Val `json:"val"`
}
jsonResult := JsonRpcRsp{}
json.Unmarshal(data, &jsonResult)
entries := []JsonRpcEntry{}
for _, val := range jsonResult {
json.Unmarshal(val.Result, &entries)
}
How to parse "key" table ?... problem is there are different types
key table structure is :
[ <string>, <int>, <string>]
To unmarshal arrays of different types in Go you'll need to use interfaces and consequently type assertions if you need access to the types.
This will work for you:
type Result struct {
Key [3]interface{} `json:"key"`
Val struct {
Type string `json:"type"`
} `json:"val"`
}
msg := JsonRpcRsp{}
json.Unmarshal(data, &msg)
var result []Result
json.Unmarshal(msg.Result, &result)
for _, v := range result {
key1 := v.Key[0].(string)
key2 := v.Key[1].(float64)
key3 := v.Key[2].(string)
fmt.Println(key1, key2, key3)
}
After asserting the three interfaces to their types, you can then work with them further, depending on your use case.

gorm: populating related field of newly created data

I have the following related tables:
type Person struct {
ID uint64 `json:"id" gorm:"primary_key;auto_increment"`
Name string `json:"name"`
Surname string `json:"surname"`
}
type Book struct {
ID uint64 `json:"id" gorm:"primary_key;auto_increment"`
Title string `json:"title" binding:"required,min=2,max=100" gorm:"type:varchar(100)"`
Author Person `json:"author" binding:"required" gorm:"foreignkey:AuthorID"` // * here
AuthorID uint64 `json:"-"` // * here
WrittenIn string `json:"written_in" gorm:"type:varchar(5)"`
CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP"`
UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP"`
}
I can successfully create data with Create() method of gorm using this function:
func CreateBook(ctx *gin.Context) {
// validating input
var inputData CreateBookInput
if err := ctx.ShouldBindJSON(&inputData); err != nil {
ctx.JSON(401, gin.H{"status": "fail", "error": err.Error()})
}
// create book
book := models.Book{Title: inputData.Title, AuthorID: inputData.AuthorID, WrittenIn: inputData.WrittenIn}
database.DB.Create(&book).Preload("Author")
// database.DB.Preload("Author").Create(&book)
// database.DB.Set("gorm:auto_preload", true).Create(&book)
ctx.JSON(201, gin.H{"status": "success", "book": book})
}
I want to return the newly created book with its author. Expected response:
"book": {
"id": 10,
"title": "Chinor ostidagi duel",
"author": {
"id": 3,
"name": "John",
"surname": "Smith"
},
"written_in": "1983",
"created_at": "2022-01-07T17:07:50.84473824+05:00",
"updated_at": "2022-01-07T17:07:50.84473824+05:00"
}
But I couldn't find a way to populate related 'author'. So what I get is:
"book": {
"id": 10,
"title": "Chinor ostidagi duel",
"author": {
"id": 0, // empty
"name": "", // empty
"surname": "" // empty
},
"written_in": "1983",
"created_at": "2022-01-07T17:07:50.84473824+05:00",
"updated_at": "2022-01-07T17:07:50.84473824+05:00"
}
Itried these methods with no success:
database.DB.Create(&book).Preload("Author")
database.DB.Preload("Author").Create(&book)
database.DB.Set("gorm:auto_preload", true).Create(&book)
database.DB.Create(&book).Set("gorm:auto_preload", true)
How can I populate related field of newly created data?
One possible solution that you could try is to use the AfterCreate hook.
func (b *Book) AfterCreate(tx *gorm.DB) (err error) {
return tx.Model(b).Preload("Author").Error
}
You can find more info about hooks here.
Preload is chain method, Create is finisher method. only finisher method will generate and execute SQL.
So...
1 Find author by id after create book
if err ;= database.DB.Create(&book).Error; err != nil {
return err
}
// will not throw not found error
database.DB.Limit(1).Find(&book.Author, book.AuthorID)
ctx.JSON(201, gin.H{"status": "success", "book": book})
2 Load author data every times, use hook
// create and update
// func (b *Book) AfterSave(tx *gorm.DB) (err error) {
// just create
func (b *Book) AfterCreate(tx *gorm.DB) (err error) {
// handle error if you want
tx.Limit(1).Find(&b.Author, b.AuthorID)
return
}

How to handle nil struct variable in struct type

I need to marshal/unmarshal json to struct in golang. Assume the struct is
type A struct {
Id string `json:"id"`
Version string `json:"version"`
Actors []actor `json:"actors`
Payload struct {
Name string `json:"name"`
Number string `json:"number"`
}
}
type payload struct {
Name string `json:"name"`
Number string `json:"number"`
}
type actor struct {
Id string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
}
The actors or payload maybe empty. The json maybe
{
"id": "78a07cea-be2b-499c-b82b-e4f510260484",
"version": "1.0.0",
"actors": [
{
"id": "1234567",
"type": "XXX",
"role": "111"
},
{
"id": "7654321",
"type": "YYY",
"role": "222"
}
],
"payload": ""
}
or
{
"id": "78a07cea-be2b-499c-b82b-e4f510260484",
"version": "1.0.0",
"actors": [],
"payload": {
"name": "XXXX",
"number": "1234567"
}
}
If i follow the struct A design and try to marshal json with payload empty, i have to init as below
a := A{
Id: "78a07cea-be2b-499c-b82b-e4f510260484",
Version: "1.0.0",
Actors: []actor{
actor{
Id: "1234567",
Type: "XXX",
Role: "111",
},
actor{
Id: "7654321",
Type: "YYY",
Role: "222",
},
},
Payload: payload{},
}
Which will result in below json with one empty payload struct
{
"id": "78a07cea-be2b-499c-b82b-e4f510260484",
"version": "1.0.0",
"actors": [
{
"id": "1234567",
"type": "XXX",
"role": "111"
},
{
"id": "7654321",
"type": "YYY",
"role": "222"
}
],
"payload": {
"name":"",
"number":""
}
}
Is there any way i can generate
"payload": ""
instead of blank payload struct? Or is there any other struct design for this kind of json format? BTW i cannot pass nil to Payload struct.
The json.Marshaler interface can be implemented to customize JSON encoding, and the json.Unmarshaler interface for decoding (left as an exercise for the reader):
package main
import (
"encoding/json"
"fmt"
)
type A struct {
Payload payload
}
type payload struct {
Name string `json:"name"`
Number string `json:"number"`
}
func (p payload) MarshalJSON() ([]byte, error) {
if p.Name == "" && p.Number == "" {
return []byte(`""`), nil
}
type _payload payload // prevent recursion
return json.Marshal(_payload(p))
}
func main() {
var a A
b, _ := json.MarshalIndent(a, "", " ")
fmt.Println(string(b))
a.Payload.Name = "foo"
b, _ = json.MarshalIndent(a, "", " ")
fmt.Println(string(b))
}
// Output:
// {
// "Payload": ""
// }
// {
// "Payload": {
// "name": "foo",
// "number": ""
// }
// }
Try it on the playground: https://play.golang.org/p/9jhSWnKTnTf
The ad-hoc _payload type is required to prevent recursion. If one would write return json.Marshal(p), the json package would call MarshalJSON again, because p is of type payload, and payload implements json.Marshaler.
The _payload type has the same underlying type as payload but does not implement json.Marshaler (see Type definitions for details), so it is encoded using the standard rules of the json package; it produces exactly the same output that encoding a value of type payload would produce if payload didn't implement json.Marshaler.
Check if using omitempty with json struct tag helps. I think it will result in "payload": {} instead of "payload": ""

Parsing JSONSchema to struct type in golang

So, my use case consists of parsing varying JSON schemas into new struct types, which will be further used with an ORM to fetch data from a SQL database. Being compiled in nature, I believe there will not be an out-of-the-box solution in go, but is there any hack available to do this, without creating a separate go process. I tried with reflection, but could not find a satisfactory approach.
Currently, I am using a-h generate library which does generate the structs, but I am stuck at how to load these new struct types in go runtime.
EDIT
Example JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Address",
"id": "Address",
"type": "object",
"description": "address",
"properties": {
"houseName": {
"type": "string",
"description": "House Name",
"maxLength": 30
},
"houseNumber": {
"type": "string",
"description": "House Number",
"maxLength": 4
},
"flatNumber": {
"type": "string",
"description": "Flat",
"maxLength": 15
},
"street": {
"type": "string",
"description": "Address 1",
"maxLength": 40
},
"district": {
"type": "string",
"description": "Address 2",
"maxLength": 30
},
"town": {
"type": "string",
"description": "City",
"maxLength": 20
},
"county": {
"type": "string",
"description": "County",
"maxLength": 20
},
"postcode": {
"type": "string",
"description": "Postcode",
"maxLength": 8
}
}
}
Now, in the above-mentioned library, there is a command line tool, which generates the text for struct type for above json as below:
// Code generated by schema-generate. DO NOT EDIT.
package main
// Address address
type Address struct {
County string `json:"county,omitempty"`
District string `json:"district,omitempty"`
FlatNumber string `json:"flatNumber,omitempty"`
HouseName string `json:"houseName,omitempty"`
HouseNumber string `json:"houseNumber,omitempty"`
Postcode string `json:"postcode,omitempty"`
Street string `json:"street,omitempty"`
Town string `json:"town,omitempty"`
}
Now, the issue is that how to use this struct type without re-compilation in the program. There is a hack, where I can start a new go process, but that doesn't seem a good way to do it. One other way is to write my own parser for unmarshalling JSON schema, something like:
b := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)
var f interface{}
json.Unmarshal(b, &f)
m := f.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string", vv)
case float64:
fmt.Println(k, "is float64", vv)
case int:
fmt.Println(k, "is int", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
default:
fmt.Println(k, "is of a type I don't know how to handle")
}
}
Can someone please suggest some pointers to look for. Thanks.
So it looks like you're trying to implement your own json marshalling. That's no biggie: the standard json package already supports that. Just have your type implement the MarshalJSON and UnmarshalJSON functions (cf first example on the docs). Assuming some fields will be shared (eg schema, id, type), you can create a unified type like this:
// poor naming, but we need this level of wrapping here
type Data struct {
Metadata
}
type Metadata struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Description string `json:"description"`
Id string `json:"id"`
Properties json.RawMessage `json:"properties"`
Address *Address `json:"-"`
// other types go here, too
}
Now all properties will be unmarshalled into a json.RawMessage field (essentially this is a []byte field). What you can do in your custom unmarshall function now is something like this:
func (d *Data) UnmarshalJSON(b []byte) error {
meta := Metadata{}
// unmarshall common fields
if err := json.Unmarshal(b, &meta); err != nil {
return err
}
// Assuming the Type field contains the value that allows you to determine what data you're actually unmarshalling
switch meta.Type {
case "address":
meta.Address = &Address{} // initialise field
if err := json.Unmarshal([]byte(meta.Properties), meta.Address); err != nil {
return err
}
case "name":
meta.Name = &Name{}
if err := json.Unmarshal([]byte(meta.Properties), meta.Name); err != nil {
return err
}
default:
return errors.New("unknown message type")
}
// all done
d.Metadata = meta // assign to embedded
// optionally: clean up the Properties field, as it contains raw JSON, and is exported
d.Metadata.Properties = json.RawMessage{}
return nil
}
You can do pretty much the same thing for marshalling. First work out what type you're actually working with, then marshal that object into the properties field, and then marhsal the entire structure
func (d Data) MarshalJSON() ([]byte, error) {
var (
prop []byte
err error
)
switch {
case d.Metadata.Address != nil:
prop, err = json.Marshal(d.Address)
case d.Metadata.Name != nil:
prop, err = json.Marshal(d.Name) // will only work if field isn't masked, better to be explicit
default:
err = errors.New("No properties to marshal") // handle in whatever way is best
}
if err != nil {
return nil, err
}
d.Metadata.Properties = json.RawMessage(prop)
return json.Marshal(d.Metadata) // marshal the unified type here
}

golang how to concatenate []byte key vaules with other variable

How to concatenate variable value into the byte key values ?
type Result struct {
SummaryID int `json:"summaryid"`
Description string `json:"description"`
}
byt := []byte(`
{
"fields": {
"project":
{
"key": "DC"
},
"summary": "Test" + Result.SummaryID,
"description": Result.Description,
"issuetype": {
"name": "Bug"
}
}
}`)
Note: values of Result.SummaryID and Result.Description return from the db.Query() and rows.Scan().
Go doesn't support string interpolation, so you'll have to use something like fmt.Sprintf or the template package if you want to compose strings out of smaller substrings.
You can do the former like so:
var buf bytes.Buffer
byt := []byte(fmt.Sprintf(`
{
"fields": {
"project":
{
"key": "DC"
},
"summary": "Test%d",
"description": "%s",
"issuetype": {
"name": "Bug"
}
}
}`, result.SummaryID, result.Description))
Though I would really advise against it, since the encoding/json package is designed for safely and sanely outputting JSON strings.
Here's an example that uses struct embedding for the main object, and maps elsewhere to demonstrate both approaches.
type WrappedResult struct {
Project map[string]string `json:"project"`
Result
IssueType map[string]string `json:"issuetype"`
}
byt, err := json.MarshalIndent(map[string]interface{}{
"fields": WrappedResult{
Result: result,
Project: map[string]string{ "key": "DC" },
IssueType: map[string]string{ "name": "Bug" },
},
});
(note that your type declaration contradicts your JSON example in that the former specifies summaryid but the latter has summary)

Resources