Giter Site home page Giter Site logo

Comments (3)

loreto avatar loreto commented on May 14, 2024 1

Note that Scanner is another relevant interface for SQL integration.

from typeid-go.

stevelizcano avatar stevelizcano commented on May 14, 2024

I recommend that this be done, because it makes typeIDs more difficult to wrangle if you are using a library like SQLC.

The issue is that if you do something like select * from my_table where id = 'some_id'; , you will get the typeID back. If you specify the sqlc type id like, for example:

      - db_type: "doc_id"
        go_type: "go.jetpack.io/typeid.TypeID"

It will try and coalesce the return string tuple into the TypeID. It fails, and panics.

Adding this ability—which I think is the Scan feature—would be very helpful.

I wrote this custom one myself to implement it:

type PGTypeID string

// ConvertToTypeID converts the PGTypeID to the typeid.TypeID.
func (p PGTypeID) ConvertToTypeID() (typeid.TypeID, error) {
	str := string(p)
	if !strings.HasPrefix(str, "(") || !strings.HasSuffix(str, ")") {
		return typeid.Nil, errors.New("invalid type id format")
	}

	// Remove the surrounding parentheses and replace the comma with an underscore
	// to match the format expected by FromString
	trimmed := strings.TrimSuffix(strings.TrimPrefix(str, "("), ")")
	trimmed = strings.Replace(trimmed, ",", "_", 1)

	// Use FromString to convert the trimmed string to TypeID
	return typeid.FromString(trimmed)
}

// Scan implements the sql.Scanner interface for PGTypeID.
func (p *PGTypeID) Scan(value interface{}) error {
	strVal, ok := value.(string)
	if !ok {
		return errors.New("PGTypeID must be a string")
	}
	*p = PGTypeID(strVal)
	return nil
}

// Value implements the driver.Valuer interface for PGTypeID.
func (p PGTypeID) Value() (driver.Value, error) {
	return string(p), nil
}

from typeid-go.

loreto avatar loreto commented on May 14, 2024

Recently committed a PR that adds this. Next release will have this built in.

from typeid-go.

Related Issues (7)

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.