Comments (3)
Note that Scanner is another relevant interface for SQL integration.
from typeid-go.
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.
Recently committed a PR that adds this. Next release will have this built in.
from typeid-go.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeid-go.