Coder Social home page Coder Social logo

Comments (5)

jackc avatar jackc commented on June 14, 2024

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.

flowchartsman avatar flowchartsman commented on June 14, 2024

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.

nkev avatar nkev commented on June 14, 2024

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.

nkev avatar nkev commented on June 14, 2024

Actually, in that last code snippet, the $value variable is rendering with start and end braces. Any idea why that might happen?

from pgx.

nkev avatar nkev commented on June 14, 2024

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)

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.