0

I'm stuck on handling Time type data of faunaDB. I have no idea which type of golang is appropriate to map to Time type data of faunaDB.

I've tried the following code for fauna document creation:

type LabelData struct {
    RedirectURL   string `fauna:"redirectURL"`
    Owner         string `fauna:"owner"`
    RedirectCount int    `fauna:"redirectCount"`
    ExternalID    string `fauna:"externalID"`
    Tag           string `fauna:"Tag"`
    Created       int64  `fauna:created`
}

func faunaCreate(externalID string) (err error) {
    var documentRef f.RefV
    labelData := LabelData{
        RedirectURL:   "",
        Owner:         "",
        RedirectCount: 0,
        ExternalID:    externalID,
        Created:       f.ToMillis(f.Time("now")),
    }
    newlabel, err := client.Query(
        f.Create(
            f.Collection("label"),
            f.Obj{"data": labelData},
        ),
    )

But it occurs following error:

cannot use faunadb.ToMillis(faunadb.Time("now")) (type faunadb.Expr) as type int64 in field value

Which data type for golang should I use for faunaDB Time type? Thank you for your suggestion!

Ueda Takeyuki
  • 721
  • 10
  • 26

2 Answers2

1

Well, the error is occurring coz you are trying to initialize faunadb.Expr type to an int64 variable.

So you can change the type of Created var. Either make is faunadb.Expr or an interface{}.

type LabelData struct {
    RedirectURL   string `fauna:"redirectURL"`
    Owner         string `fauna:"owner"`
    RedirectCount int    `fauna:"redirectCount"`
    ExternalID    string `fauna:"externalID"`
    Tag           string `fauna:"Tag"`
    Created       interface{}  `fauna:created`
}
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
0

Use the regular go type time.Time in your struct

Marrony
  • 231
  • 1
  • 3
  • Hi Marrony, Thank you for your suggestion, I'm afraid that using time.Time causes the same type of error. – Ueda Takeyuki Mar 14 '20 at 23:57
  • You should use go types like go types, what you are trying to do is use go type and initialize with a fauna function, it won’t work. – Marrony Apr 05 '20 at 19:32