Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom type struct #87

Closed
gebv opened this issue Nov 22, 2015 · 1 comment
Closed

Custom type struct #87

gebv opened this issue Nov 22, 2015 · 1 comment
Assignees

Comments

@gebv
Copy link

gebv commented Nov 22, 2015

How to add a custom type that would be not causing an error Formater? This is the interface?

ex.

CREATE TABLE objects (
object_id serial NOT NULL PRIMARY KEY,
tags text[],
);

INSERT INTO objects(tags) VALUES
    ('{a, b, c}'),
    ('{a, c}'),
    ('{c}'),
    ('{b}');
var model = struct {
        ObjectId int64
        Tags  StringSlice
    }{}

QueryOne(&model, "SELECT object_id, tags FROM objects WHERE object_id = ?", 1)
// ok

model.Tags.Del("a")
model.Tags.Del("b")
model.Tags.Add("d")

QueryOne(&model, "UPDATE objects SET tags = ?tags WHERE object_id = ?object_id", model)
QueryOne(&model, "UPDATE objects SET tags = ? WHERE object_id = ?", model.Tags, model.ObjectId)
// Error: Format(unsupported StringSlice)

QueryOne(&model, "UPDATE objects SET tags = ? WHERE object_id = ?", []string(model.Tags), model.ObjectId)
// It'ok, but isn't convenient to use
type StringSlice []string

func (c *StringSlice) Del(str string) {
    str = strings.TrimSpace(str)

    if !c.IsExist(str) {
        return
    }

    for index, value := range *c {

        if bytes.Equal([]byte(str), []byte(value)) {
            (*c)[index] = (*c)[len((*c))-1]
            (*c) = (*c)[:len((*c))-1]

            return
        }
    }
}

func (c *StringSlice) IsExist(str string) bool {
    str = strings.TrimSpace(str)

    for _, value := range *c {

        if bytes.Equal([]byte(str), []byte(value)) {

            return true
        }
    }

    return false
}

func (c *StringSlice) Add(str string) {
    str = strings.TrimSpace(str)

    if len(str) == 0 {
        return
    }

    if !c.IsExist(str) {
        *c = append(*c, str)
    }
}
@vmihailenco vmihailenco self-assigned this Nov 22, 2015
vmihailenco added a commit that referenced this issue Nov 22, 2015
Fix slice alis decoding. Fixes #87.
@vmihailenco
Copy link
Member

@gebv should be fixed in master. Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants