Comments (5)
I believe this would be because pgx.Null* types do not define String(), so template is basically doing a Printf with the %v format argument.
There are several possible solutions to this.
The most obvious would be to update pgx to define String() for the pgx.Null* types. However, that presents a problem as to what the correct return for String() on a null value would be. Probably "" or "NULL". However, this has the downside of making the null value and a non-null string indistinguishable from each other when output normally.
The next solution would be create a new type is simply the pgx.Null* with String defined for what makes sense for your application. See https://github.com/jackc/pgx/blob/master/conn.go#L73 for an example from pgx of making a new type from an existing one and adding new behavior. The Go docs describing the behavior are here: https://golang.org/ref/spec#Type_declarations.
Another solution would be to use different variable for the display value and to conditionally assign the correct string. This has the advantage of letting null be displayed differently in different contexts. For example, null may best be displayed as "", "0", "0.0", "empty", "unknown", or "null" depending on what exactly it is.
from pgx.
One other solution to consider is adding String() , but having it panic when attempting to stringify a NULL. I know avoiding panic is a best practice, but the user really should be checking any potentially-null types anyway, as this is the nature of the beast when dealing with nullable columns from a strongly-typed language.
Sent from my iPhone
On Jan 3, 2015, at 10:07 AM, Jack Christensen [email protected] wrote:
I believe this would be because pgx.Null* types do not define String(), so template is basically doing a Printf with the %v format argument.
There are several possible solutions to this.
The most obvious would be to update pgx to define String() for the pgx.Null* types. However, that presents a problem as to what the correct return for String() on a null value would be. Probably "" or "NULL". However, this has the downside of making the null value and a non-null string indistinguishable from each other when output normally.
The next solution would be create a new type is simply the pgx.Null* with String defined for what makes sense for your application. See https://github.com/jackc/pgx/blob/master/conn.go#L73 for an example from pgx of making a new type from an existing one and adding new behavior. The Go docs describing the behavior are here: https://golang.org/ref/spec#Type_declarations.
Another solution would be to use different variable for the display value and to conditionally assign the correct string. This has the advantage of letting null be displayed differently in different contexts. For example, null may best be displayed as "", "0", "0.0", "empty", "unknown", or "null" depending on what exactly it is.
—
Reply to this email directly or view it on GitHub.
from pgx.
I worked out that the pgx.NullString struct has two parameters, "Valid" and String" so if I change my HTML template from {{ .Title }} to below, it works correctly. My title is only rendered if it is not null in the database:
{{ if .Title.Valid }}
<h3>{{ .Title.String }}</h3>
{{ end }}
Similarly, my pgx.NullHstore "Specs" column also works too:
{{ if .Specs.Valid }}
{{with .Specs.Hstore }}
<p>
{{ range $key, $value := . }}
<strong>{{ $key }}</strong>: {{ $value }} <br/>
{{ end }}
</p>
{{end}}
{{ end }}
from pgx.
Actually, in that last code snippet, the $value variable is rendering with start and end braces. Any idea why that might happen?
from pgx.
Aha! it's the same issue... The $value variable is a pgx.NullString so you need to check for validity. The final working code to render a pgx.NullHstore in a template is:
{{ if .Specs.Valid }}
{{with .Specs.Hstore }}
<p>
{{ range $key, $value := . }}
<strong>{{ $key }}</strong>:
{{ if $value.Valid }}
{{ $value.String }} <br/>
{{ end }}
{{ end }}
</p>
{{end}}
{{ end }}
from pgx.
Related Issues (20)
- Option to wrap goroutines with a custom handler HOT 4
- Add `Close()` method to the connector for a pooled connection HOT 3
- chunkreader Next returns internal/poll.DeadlineExceededError when returning a column with some Kb data. HOT 3
- pgxpool: different MaxConnLifetime for fallback hosts HOT 1
- The type for "MONEY" isn't detected
- Support directly inserting pgtype.Bits into a column of type BIT(n) using named args
- Discussion: useful to return rows early using iter.Seq? HOT 2
- Discussion: Improve pgx.CollectRows to dynamically allocate sufficient slice capacity to avoid memory reallocations HOT 2
- pgtype.ArrayCodec should support non-comma delimiter HOT 2
- `fatal error: concurrent map writes` in `EnumCodec.lookupAndCacheString` HOT 4
- Support for require_auth configuration parameter HOT 1
- Security: Uncaught Exception Violation found by Snyk HOT 3
- RowToStructByName, Lax and Pos not working with `db:"-"` tag HOT 1
- Examples or documentation for "WHERE field IN $1" - should be "WHERE field IN ($1)" HOT 1
- Tracelog truncation can make output longer.
- dangling transactions after 'failed to deallocate cached statement' errors HOT 1
- Clarify ErrIsClosed return type in docs HOT 1
- v4.18.3 Security vulnerability CVE-2024-27289 HOT 2
- Running lib/pq and pgx in the same code base? HOT 1
- Cannot scan/insert user defined UUIDs.
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 pgx.