Git Product home page Git Product logo

Comments (7)

anuptalwalkar avatar anuptalwalkar commented on May 14, 2024

@uber-go/service-framework

from fx.

glibsm avatar glibsm commented on May 14, 2024

@anakryiko

from fx.

glibsm avatar glibsm commented on May 14, 2024

It's an interesting discussion, I think there are two separate points here:

  1. Wether or not AsString when called on any data type should be ok
  2. If As* methods should panic

Based on the quick survey of the codebase it looks like all As* already panic, apart from one very specific case of AsString:

    valueType := reflect.TypeOf(value)

    if valueType.AssignableTo(targetType) {
        return value, nil
    } else if targetType.Name() == "string" { // <--------------
        return fmt.Sprintf("%v", value), nil
    }

It looks to a specially handled case, when someone is asking for a string type. I think we should remove that else if and then the problem would go away. You wouldn't be able to get a map value accidentally out as a string and it would make our API consistent in all cases.

Then, we can talk about removing the As* methods all-together and forcing people to check the ok bool, since panic() should be reserved for extreme cases.

from fx.

anakryiko avatar anakryiko commented on May 14, 2024

I believe having AsStringXXX() (which can panic) and TryAsXXX() (which never panics) is fine. It's like two ways to cast values in Go, one panics if something is not as expected, while another gives programmer flexibility to fall back to something else.

I have related question, though. Should AsInt() try to truncate float64 value to int? Or it should be treated as unexpected type of value and panic? I lean toward strictness (history shows that strictness is preferred over time, as Javascript history shows).

If you still decide to truncate float64 to int, then I'd say it's logical to say that AsFloat() should upconvert int to float64.

BTW, should AsFloat() be called AsFloat64()?

from fx.

madhuravi avatar madhuravi commented on May 14, 2024

I like having two methods too. We initially had a Get and MustGet version in our repo which had a similar idea. It's nice to have the strict version so users don't have to check for things.

I prefer strictness with float64 vs int too. It might be unintentional and cause bugs if we truncate it.

from fx.

shawnburke avatar shawnburke commented on May 14, 2024

I think having two versions is basically a requirement, so we're all set there. I absolutely think we should be doing some set of coercions for people, it makes code so much simpler to write. For example string -> int and int -> string are strightforward.

The relevant question I see is the behavior with string conversion as a generic path. It's very common for frameworks to do "best effort" string (toString) conversions, that's not that unusual.

If the argument here is for the case where a user actually wants a string, then they can call Value() and try the type assertion as well.

But I think we could go either way on this, but I don't know what it means to fail to convert to a string. How do we know what %v does and if that's what the user did or didn't want? In most cases, it'll be right or else the user will do something custom.

from fx.

anuptalwalkar avatar anuptalwalkar commented on May 14, 2024

Thanks for all the great inputs. To summarize, we want to keep best effort type conversion with As* methods. Overall consensus to As* API usage is to make the code simpler to write and not panic on type failures.
If there is a need for stricter Value APIs, we will reevaluate introducing Must* methods. Please feel free to open the issue or relate if it leads to a scenario for introducing strict type check methods.

from fx.

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.