Comments (7)
@uber-go/service-framework
from fx.
from fx.
It's an interesting discussion, I think there are two separate points here:
- Wether or not
AsString
when called on any data type should be ok - 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.
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.
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.
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.
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)
- Handling optional fields in fx.Decorate HOT 3
- Multiple implementations HOT 3
- Optional dependency becomes required if there is a provider available HOT 1
- Factory like provider? HOT 3
- Refresh dependency HOT 5
- `fx.Decorate` not working w/ value groups HOT 3
- doc error on [register a handler with the server] HOT 2
- Fx exiting program before panic in OnStart hook gets reported HOT 1
- Add ability to override os.Exit for fxtest HOT 1
- fx.Evaluate: Support dynamic graphs by returning fx.Option from constructors
- https://github.com/benbjohnson/clock is EOL/archived. HOT 1
- fx.Decorate does not add a missing object to the module HOT 3
- When combining fx.As to annotate return type as interface with optional parameters nil value check is not working as expected HOT 1
- Startup order isn't preserved when using modules HOT 2
- Example test failing in master branch when port 8080 is already in use (89f4a90) HOT 3
- fxevent.Logger: Silence until there's an error
- Slog adapter for fx HOT 4
- Release method HOT 10
- fxtest.Lifecycle: Enforce timeout
- Impossible to inject struct that is part of a group by itself HOT 2
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 fx.