How to change integer to string with go in this case? - go

Using gorm to connect db. Here get all the records:
func GetPeople(c *gin.Context) {
var people []Person
var count int
find_people := db.Find(&people)
find_people.Count(&count)
if err := find_people.Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.Header("X-Total-Count", &count)
c.JSON(200, people)
}
}
About count, the c.Header("X-Total-Count", &count) can't passed since this error:
cannot use &count (type *int) as type string in argument to c.Header
Have tried strconv.Itoa(&count), got another error:
cannot use &count (type *int) as type int in argument to strconv.Itoa
So how to convert integer to string in this case?

In the c.Header() call pass the value of the variable instead of a pointer.
c.Header("X-Total-Count", strconv.Itoa(count))
For reference, the method signature is:
func (c *Context) Header(key, value string) {

Related

Error while trying to fetch queryresult.KV object in JSON.Unmarshal

I am a little bit confused here and although I have searched a lot on this, something is clearly missing from my knowledge and I am asking your help.
I have created a Hyperledger Fabric Network and installed a chaincode in it. And I want to make a function that retrieves all the World State inputs about the Keys. I have done it already with the bytes.Buffer and it worked. But what I want to do is to do it with a struct.
So, I created the following struct that has only the key:
type WSKeys struct {
Key string `json: "key"`
Namespace string `json: "Namespace"`
}
And this is my code function:
func (s *SmartContract) getAllWsDataStruct(APIstub shim.ChaincodeStubInterface , args []string) sc.Response {
var keyArrayStr []WSKeys
resultsIterator, err := APIstub.GetQueryResult("{\"selector\":{\"_id\":{\"$ne\": null }} }")
if err != nil {
return shim.Error("Error occured when trying to fetch data: "+err.Error())
}
for resultsIterator.HasNext() {
// Get the next record
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println(queryResponse)
var qry_key_json WSKeys
json.Unmarshal([]byte(queryResponse), &qry_key_json)
keyArray = append(keyArray, qry_key_json)
}
defer resultsIterator.Close()
all_bytes, _ := json.Marshal(keyArray)
fmt.Println(keyArray)
return shim.Success(all_bytes)
}
When executing the above I get the following error:
cannot convert queryResponse (type *queryresult.KV) to type []byte
I can get the results correctly if I, for example do this:
func (s *SmartContract) getAllWsDataStruct(APIstub shim.ChaincodeStubInterface , args []string) sc.Response {
var keyArray []string
resultsIterator, err := APIstub.GetQueryResult("{\"selector\":{\"_id\":{\"$ne\": null }} }")
if err != nil {
return shim.Error("Error occured when trying to fetch data: "+err.Error())
}
for resultsIterator.HasNext() {
// Get the next record
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
fmt.Println(queryResponse)
keyArray = append(keyArray, queryResponse.Key)
}
defer resultsIterator.Close()
all_bytes, _ := json.Marshal(keyArray)
fmt.Println(keyArray)
return shim.Success(all_bytes)
}
But, why I get the above error when trying to add the queryResponse into a custom struct?
Do I need to add it to a struct that is only its type?
Please someone can explain what I am missing here?
The error statement is verbose enough to indicate, that your []byte conversion failed for the type queryResponse which, with a bit of lookup seems to be a struct type. In Go you cannot natively convert a struct instance to its constituent bytes without encoding using gob or other means.
Perhaps your intention was to use the Key record in the struct for un-marshalling
json.Unmarshal([]byte(queryResponse.Key), &qry_key_json)

How to create an object of interface value type in go using reflection

UPDATED
I want to make helper function for testing reading env vars function. It uses envconfig.
func Test_T2(t *testing.T) {
os.Setenv("APP_PARAM_STR", "string value")
os.Setenv("APP_PARAM_INT", "12")
os.Setenv("APP_PARAM_DURATION", "15s")
os.Setenv("APP_PARAM_INT", "44")
c := ConfigTwo{}
d := ConfigTwo{
ParamDuration: 15*time.Second,
ParamInt: 44,
}
helper(t, &c, &d)
}
func helper(t *testing.T, confObject, expValue interface{}) {
t.Helper()
err := getParams(&confObject)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, expValue, confObject)
}
func getParams(cfg interface{}) error {
return envconfig.Process("APP", cfg)
}
** UPDATE 2 **
It works. Thanks everyone.
It works if I have getPrams function only. But if I add helper (that I need to test different structs) I get an error:
specification must be a struct pointer
envconfig performs two checks here:
Use this code. The argument is a pointer to the expected value.
func helper(t *testing.T, pexpected interface{}) {
t.Helper()
pactual := reflect.New(reflect.TypeOf(pexpected).Elem()).Interface()
err := getParams(pactual)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, pexpected, pactual)
}
The expression reflect.New(reflect.TypeOf(pexeceted).Elem()).Interface() returns a pointer to a new empty value with the same type as what pexpected points to.
Call it like this:
helper(t, &ConfigTwo{A: "expected A Field", B: "expected B field"}

Timestamps in Golang

Trying to get this approach to timestamps working in my application: https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go
Here it is:
package timestamp
import (
"fmt"
"labix.org/v2/mgo/bson"
"strconv"
"time"
)
type Timestamp time.Time
func (t *Timestamp) MarshalJSON() ([]byte, error) {
ts := time.Time(*t).Unix()
stamp := fmt.Sprint(ts)
return []byte(stamp), nil
}
func (t *Timestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.Atoi(string(b))
if err != nil {
return err
}
*t = Timestamp(time.Unix(int64(ts), 0))
return nil
}
func (t Timestamp) GetBSON() (interface{}, error) {
if time.Time(*t).IsZero() {
return nil, nil
}
return time.Time(*t), nil
}
func (t *Timestamp) SetBSON(raw bson.Raw) error {
var tm time.Time
if err := raw.Unmarshal(&tm); err != nil {
return err
}
*t = Timestamp(tm)
return nil
}
func (t *Timestamp) String() string {
return time.Time(*t).String()
}
and the article that goes with it: https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f
However, I'm getting the following error:
core/timestamp/timestamp.go:31: invalid indirect of t (type Timestamp)
core/timestamp/timestamp.go:35: invalid indirect of t (type Timestamp)
My relevant code looks like this:
import (
"github.com/path/to/timestamp"
)
type User struct {
Name string
Created_at *timestamp.Timestamp `bson:"created_at,omitempty" json:"created_at,omitempty"`
}
Can anyone see what I'm doing wrong?
Related question
I can't see how to implement this package either. Do I create a new User model something like this?
u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}
Your code has a typo. You can't dereference a non-pointer, so you need to make GetBSON a pointer receiver (or you could remove the indirects to t, since the value of t isn't changed by the method).
func (t *Timestamp) GetBSON() (interface{}, error) {
To set a *Timestamp value inline, you need to have a *time.Time to convert.
now := time.Now()
u := User{
Name: "Bob",
CreatedAt: (*Timestamp)(&now),
}
Constructor and a helper functions like New() and Now() may come in handy for this as well.
You cannot refer to an indirection of something that is not a pointer variable.
var a int = 3 // a = 3
var A *int = &a // A = 0x10436184
fmt.Println(*A == a) // true, both equals 3
fmt.Println(*&a == a) // true, both equals 3
fmt.Println(*a) // invalid indirect of a (type int)
Thus, you can not reference the address of a with *a.
Looking at where the error happens:
func (t Timestamp) GetBSON() (interface{}, error) {
// t is a variable type Timestamp, not type *Timestamp (pointer)
// so this is not possible at all, unless t is a pointer variable
// and you're trying to dereference it to get the Timestamp value
if time.Time(*t).IsZero() {
return nil, nil
}
// so is this
return time.Time(*t), nil
}

Marshal sql.NullString with omitempty

How is one supposed to not render the null sql.NullString if it is not valid? - http://play.golang.org/p/pzSWS9vE0J
It doesn't seem to be working with omitempty struct tag and I can't quite figure out what to return from the MarshalJSON such that omitempty will be honored
type NS struct {
sql.NullString
}
func (ns *NS) MarshalJSON() ([]byte, error) {
fmt.Println("Marshaling json for NS")
if ns.String == "" && !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.String)
}
type A struct {
RStr string `json:rstr,omitempty"`
Str NS `json:"str,omitempty"`
}
func main() {
a := A{RStr: "rstr"}
s, _ := json.Marshal(&a)
fmt.Println(string(s))
}
As far as I know, there's no way to do that, however you can always use a pointer instead : http://play.golang.org/p/b4Q7YgpUa-
func main() {
a := A{"rstr", &sql.NullString{"Test", true}}
s, err := json.Marshal(&a)
fmt.Println(string(s), err)
a = A{}
fmt.Println(json.Unmarshal(s, &a))
fmt.Printf("%v %v\n", a.RStr, a.Str.String)
a = A{RStr: "rstr"}
s, err = json.Marshal(&a)
fmt.Println(string(s), err)
}
The reason why a pointer makes a difference from http://tip.golang.org/pkg/encoding/json/#Marshal:
-the field is empty and its tag specifies the "omitempty" option.
The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.

Type conversion of string to int

Ok, so I've been having difficulties with the type conversion of a string to byte write. This is the compiler error:
cannot use row[5] (type uint8) as type string in function argument
cannot use &v (type *Field) as type int in function argument
This is an example of row[5]: $15,000.00
Ive declared a struct:
type Field struct {
Eia uint8
}
here is the main implementation:
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
var v Field
for _, row := range record {
eia, err := strconv.ParseInt(row[5], 8, &v) // Estimated Incoming Amount
if err == nil {
fmt.Printf("%+v\n", v)
} else {
fmt.Println(err)
fmt.Printf("%+v\n", v)
}
Can anyone please explain to me how strconv can convert the row to a integer?
If you made a complete example on http://play.golang.org/ it'd be easier to give you a complete solution.
ParseInt() takes the string (you might have to do string(row[5])), the base (you probably meant 10) and the bitsize (that's where you should put 8).
It retuns an int (eia), it doesn't put it into the struct as it looks like you are trying.
Instead do if err == nil { v.Eia = eia }.

Resources