Giter Site home page Giter Site logo

Support sql.ErrNoRows about xo HOT 8 CLOSED

xo avatar xo commented on May 3, 2024
Support sql.ErrNoRows

from xo.

Comments (8)

kenshaw avatar kenshaw commented on May 3, 2024

I understand the rationale, but I'm not sure if I agree if this is the logical behavior, as I don't think this would fit for all use cases. Part of what makes xo useful is that you're able to bring your own templates into the mix, and I think that this would be a perfect example of why you'd want to use custom templates.

As a side note, please note that you're basically dropping an error condition (or not reacting to it) and converting a specific error to nil. I don't think one could consider this idiomatic Go.

from xo.

xeoncross avatar xeoncross commented on May 3, 2024

Excuse my laziness, but I'm just getting over a fever.

Edit https://github.com/knq/xo/blob/master/internal/argtype.go

// HandleNoRow and and return nil instead of an error for it
HandleNoRow bool `arg:"--handle-norow,help:return nil instead of an error if no record is found"`

And then in https://github.com/knq/xo/blob/master/templates/postgres.query.go.tpl#L19

if err != nil {
	{{- if .HandleNoRow }}
	if err == sql.ErrNoRows {
		return nil, nil
	}
	{{ end }}
	return nil, err
}

from xo.

xeoncross avatar xeoncross commented on May 3, 2024

Thanks for the quick reply. Let me address your points:

I understand the rationale, but I'm not sure if I agree if this is the logical behavior, as I don't think this would fit for all use cases.

This wouldn't be the default behavior - but a highly useful small feature.

Part of what makes xo useful is that you're able to bring your own templates into the mix, and I think that this would be a perfect example of why you'd want to use custom templates.

It seems overkill to write your own templates just for this.

As a side note, please note that you're basically dropping an error condition (or not reacting to it) and converting a specific error to nil. I don't think one could consider this idiomatic Go.

It's not really an error condition. It's a marker you are supposed to check. No results is not treated as an error in any language but Go... which is why the recommend you check that everything is ok by using sql.ErrNoRows.

No information is being lost but the marker for "empty" is simply a nil result instead of an nil result wrapped up as an Error.

By allowing the check to be performed in the Model we can keep the code more DRY.

from xo.

kenshaw avatar kenshaw commented on May 3, 2024

Let me think about it for a bit, and I'll make a decision. Already xo has "too many" options, and also this would need to be applied to more than one template. Again, I'm still thinking the appropriate / correct approach is to simply use your own templates. "xo is not a silver bullet"

from xo.

xeoncross avatar xeoncross commented on May 3, 2024

I understand, make sure your convinced this is a good feature (After several Go apps I am pretty convinced it is). Here is one more very common example of dealing with a user struct in different parts of the application.

user, err = models.GetUserByID/Email/Username(db, param)
if err != nil {
	if err == sql.ErrNoRows {
		user = nil
	} else {
		log.Fatal(err)
	}
}

vs

user, err = models.GetUserByID/Email/Username(db, param)
if err != nil {
	log.Fatal(err)
}

They both continue with the following user handling

if user == nil {
  // deny, login, redirect, 404
} else {
  
}

To me, keeping database-specific knowledge out of the controller also seem cleaner. Every file I use a user object on shouldn't have to know (or import) the special sql constants to use the results.

from xo.

turnkey-commerce avatar turnkey-commerce commented on May 3, 2024

I tend to agree that no rows returned from a specific query is not so much an exceptional error case and may occur often in cases where custom user criteria are allowed. The case of returning nil allows the application to react to that case more naturally and separately handle a hard error such as the database not being available.

At the very least we should return a specific error type for the NoRows case if it is still going to return an error.

from xo.

xeoncross avatar xeoncross commented on May 3, 2024

At the very least we should return a specific error type for the NoRows case if it is still going to return an error.

Returning an error marking an error marking a nil isn't exactly a step up. Then again, if your PHP/Java it's one more abstraction, so I guess that's a win.

from xo.

turnkey-commerce avatar turnkey-commerce commented on May 3, 2024

I see your point and am definitely not one of those :)

from xo.

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.