Comments (3)
I'll be sure to leave here an idea. As we probably want to have fine control of what is some
and what is none
and each view will likely differ, why not require a callable to decide it?
$myType = Type\option(
Type\string(),
static fn (string $value) => '' !== $value
);
$myType->coerce('Hello world')->isSome(); // true
$myType->coerce('')->isSome(); // false
WDYT @azjezz @veewee? I will have a couple of spare weeks so I can start working on this idea if you like it
from psl.
Hello @devnix,
I was thinking : wouldn't is make more sense to let the underlying type determine wether it is some or none?
For example:
$type = option(string());
$type->coerce('some'); // Some('some');
$type->coerce(''); // Some('');
// But
$type = option(non_empty_string());
$type->coerce('some'); // Some('some');
$type->coerce(''); // None();
The logic would be as followed:
coerce($data)
- If inner type coerces, then it is some($data)
- If inner type throws exception, then it is none()
assert($data)
- Validates if $data is of type
Option
- In case of some, validates if inner type asserts on unwrapped option value
- Validates if $data is of type
matches($data)
- Validates if $data is of type
Option
- In case of some, validates if inner type matches on unwrapped option value
- Validates if $data is of type
I have not validated every possible type to make sure this logic is 100%, but think it should do the job just fine.
This way, you could use the base types to determine it is a some|none, and a converted()
type (instead of a predicate) for very specific scenarios.
WDYT?
from psl.
Hi @veewee, sorry for the late response!
It is true that converted()
would be a better candidate for the case I presented.
However, the coerce()
behavior looks too forgiving for me. I was surprised by the first time I used shape()
when I saw it failed even when the shape was correct, and then I saw that the given shape I was coercing had more keys than my documented shape, and then I learned about the allow_unknown_fields
parameter.
I think that it's easier to offer stricter behaviors by default with opt-ins for a more lenient logic. For example:
$type = option(string());
$type->coerce('some'); // Some('some');
$type->coerce(''); // Some('');
$type = option(non_empty_string());
$type->coerce('some'); // Some('some');
$type->coerce(null); // None();
$type->coerce(''); // CoercionException
$type = option(
non_empty_string(),
none_if_not_coercible: true
);
$type->coerce('some'); // Some('some');
$type->coerce(''); // None();
However, I also doubt about performing a logic like from_nullable()
with code like this:
$type = option(nullable(string()));
$type->coerce('some'); // Some('some');
$type->coerce(null); // Some(null) or None()?
from psl.
Related Issues (20)
- [Type] add `nonnull` type
- [Type] add `string_range` and `int_range` types.
- Undeprecate `positive_int` HOT 2
- base64 url safe encoding HOT 1
- Return value of `Psl\Str\trim()` must be of the type string, null returned HOT 6
- `CoercionException::withValue()` called but not throwed. Is this dead code? HOT 1
- Improve error messages for shape and vec -like types
- Improve Dict\merge HOT 1
- Make PSL PHPStan compliant
- Split this package is multiple sub-packages HOT 2
- [Bug] Vec\range handles ints which don't roundtrip through floats wrong
- intersection + shapes unexpected behaviour (possible bug?) HOT 4
- Type\implements_interface HOT 3
- Corece an `array` out of an `stdClass`? HOT 3
- [RFC] Change `ResultInterface<T>` to `ResultInterface<T, E>` HOT 5
- chore: rename all enum cases to use PascalCase
- Improvement converted json type HOT 5
- Is nonnull helpful in any way? HOT 1
- PHPStan extension unable to report optional positive-int
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 psl.