Code Monkey home page Code Monkey logo

Comments (6)

vmihailenco avatar vmihailenco commented on July 22, 2024

Hi,

Unfortunately I can't reproduce the issue:

package main

import (
    "fmt"

    "gopkg.in/pg.v3"
)

type Person struct {
    Emails []string
}

func main() {
    db := pg.Connect(&pg.Options{
        User: "postgres",
    })

    var person Person
    _, err := db.QueryOne(&person, `SELECT null::text[] AS emails`)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%#v\n", person)

    var emails []string
    _, err = db.QueryOne(pg.LoadInto(&emails), `SELECT null::text[] AS emails`)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%#v\n", emails)
}
main.Person{Emails:[]string(nil)}
[]string(nil)

And there is also a test for decoding nil slice: https://github.com/go-pg/pg/blob/master/types_test.go#L189

Can it be that you use old version of the package?

from pg.

FcoManueel avatar FcoManueel commented on July 22, 2024

I see. I assumed that what I was seeing was happening while decoding the slice, but now I see its when inserting the value.

Inside the method appendStringSlice there's this part:

if len(v) == 0 {
    return append(dst, `{}`...)
}

and, since both empty and nil slices have length 0, we will append {} to the query for both.

I confirmed this by adding at line 80 the following check:

if reflect.DeepEqual(src, []string(nil)) {
    return appendNull(dst)
}

... of course there must be better ways to do it 😄specially since the same is happening for the other types of slices.

After adding that code, my original and retrieved structs were the same.

from pg.

FcoManueel avatar FcoManueel commented on July 22, 2024

I'll paste here the code I used to reproduce my original use case, in case it's of any use:

First I created a table inside the test database

CREATE TABLE persons (
id text NOT NULL,
emails text[]
);

And this is my test code:

package main

import (
    "fmt"
    "gopkg.in/pg.v3"
)

type Person struct {
    ID     string
    Emails []string
}

type Persons[]*Person
func (contents *Persons) NewRecord() interface{} {
    content := &Person{}
    *contents = append(*contents, content)
    return content
}

func main() {
    db := pg.Connect(&pg.Options{
        User: "postgres",
        Database: "test",
    })
    db.Exec(`TRUNCATE TABLE persons;`)

    // Create and insert a person without emails
    var person1 = Person{ID: "1"}
    fmt.Printf("%#v\n", person1.Emails)
    _, err := db.ExecOne(`INSERT INTO persons(id, emails) VALUES (?id, ?emails);`, person1)

    // Get person from database
    var allPersons Persons
    _, err = db.Query(&allPersons, `SELECT id, emails FROM persons;`)
    if err != nil {
        panic(err)
    }

    if len(allPersons) != 1 {
        fmt.Println("len(allPersons) :", len(allPersons))
        panic("Expected only 1 person")
    }
    personFromDb := allPersons[0]
    fmt.Printf("%#v\n", personFromDb.Emails)
}

The obtained output was the following:

[]string(nil)
[]string{}

from pg.

vmihailenco avatar vmihailenco commented on July 22, 2024

Thanks for the report. Should be fixed in v3.3.14.

from pg.

FcoManueel avatar FcoManueel commented on July 22, 2024

Awesome, it works.
Thanks!

from pg.

vmihailenco avatar vmihailenco commented on July 22, 2024

You are welcome!

from pg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.