An example of a CRUD api in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.0 KiB

package randomdata
import (
"encoding/json"
)
// The `bridge` between input values and a record in db
type RandomData struct {
DateCreated int64
LastUpdated int64
ID uint
Title string `json:"title"`
Text string `json:"text"`
}
// Unmarshal `RandomData` from Json encoded bytes
func FromJson(jsonBytes []byte) (*RandomData, error) {
var randomData RandomData
err := json.Unmarshal(jsonBytes, &randomData)
if err != nil {
return nil, err
}
return &randomData, nil
}
// Convert struct to json bytes
func (rd *RandomData) ToJson() ([]byte, error) {
bytes, err := json.Marshal(rd)
if err != nil {
return nil, err
}
return bytes, nil
}
// Convert all rdatas to []byte
func ToJsonAll(rdatas []*RandomData, addNewlines bool) ([]byte, error) {
var rdatasBytes []byte
for _, rdata := range rdatas {
jsonBytes, err := rdata.ToJson()
if err != nil {
return nil, err
}
rdatasBytes = append(rdatasBytes, jsonBytes...)
if addNewlines {
rdatasBytes = append(rdatasBytes, 10)
}
}
return rdatasBytes, nil
}