Comments (5)
@blindspotbounty No worries
That looks good to me. I think I can try propose PR next days if you don't mind.
Yeah that would be amazing!
do you think we should do it under option or add some new accessors indefinitely?
So I don't want to add extra flags to the code generator so i would say just adding it normally would be alright. However we will keep the original one. So what i think would be useful is something like this:
// Original
public var value: EnumUnknown_A? { ... }
// + new one
public var unsafeValue: EnumValue<EnumUnknown_A>? { ... }
Since in theory we are reading memory that's not planned for with the generated code. and thus making it unsafe although its been verified that its within the buffer.
from flatbuffers.
Hej
Thanks for opening this issue.
1- A solution would be to create an init within the enums that check if that validates a value, and if it's not correct we return an unknown case. which would require a way to make sure that the user is not going to return that value (example: infinitely)
2- Another one would be to basically check if the value is there before _accessor.readBuffer(of: Int8.self, at: o)
and having a bool for that to validate if it's
3- Throw and error?! if the value is there but couldn't be mapped
from flatbuffers.
Hi @mustiikhalil
Thank you for the quick answer!
I believe that ideally would be (1) with some extension (maybe under option) to generate RawRepresentable
structure instead of enum.
E.g. like the following:
public struct EnumUnknown_A: RawRepresentable, Enum, Verifiable {
public typealias T = Int8
public static var byteSize: Int { return MemoryLayout<Int8>.size }
public var value: Int8 { return self.rawValue }
public var rawValue: Int8
public static var a: EnumUnknown_A = .init(rawValue: 0)
public static var b: EnumUnknown_A = .init(rawValue: 1)
public static var max: EnumUnknown_A { return .b }
public static var min: EnumUnknown_A { return .a }
}
That way, any unknown value can be propagated to the client code while structure remain RawRepresentable
.
If it is inline with your envision, or you are okay to have this under option - I think I can add that to flatc.
What do you think about that?
I like both other options - they can always be a part of generated code.
from flatbuffers.
@blindspotbounty The whole idea of having it as an enum is that the options are constrained so I don't think making it a struct would be an option here.
However, after a bit of thinking maybe we can do something like this:
// Flatbuffers
// - Sources/EnumValues.swift
public enum EnumValues<T> where T: Enum {
case found(v: T)
case unknown(v: Any)
}
And then we can add an API that does the following:
public enum EnumUnknown_A: Int8, Enum, Verifiable {
...
public static func wrapped(_ v: Int8) -> EnumValues<EnumUnknown_A> {
guard let e = EnumUnknown_A(rawValue: v) else { return .unknown(v: v) }
return .found(v: e)
}
}
public struct EnumUnknown_foo: FlatBufferObject, Verifiable {
...
/// New API?
public var value: EnumValues<EnumUnknown_A>? {
let o = _accessor.offset(VTOFFSET.value.v)
if o == 0 { return nil }
return EnumUnknown_A.wrapped(_accessor.readBuffer(of: Int8.self, at: o)
}
// maybe keep the original one
public var value: EnumUnknown_A? { let o = _accessor.offset(VTOFFSET.value.v); return o == 0 ? nil : EnumUnknown_A(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? nil }
...
}
Or we can also leverage the Swift.Result<V, Error>
so we don't reinvent the wheel and just return a FlatbufferError.unknownValue(v: Any)
.
What do you think?
From my point of view this is safer than allowing the user to get any value from the buffer, instead of just creating a struct that you can pass to it any value kinda
from flatbuffers.
@mustiikhalil thank you for you input and I apologize for the late answer.
That looks good to me. I think I can try propose PR next days if you don't mind, do you think we should do it under option or add some new accessors indefinitely?
Btw, have one more use case comes to mind (it is out of scope but nice to have in future) is to allow to propagate such field.
E.g. the following table:
namespace EnumUnknown;
enum A: byte {
a,
b,
c,
}
table FooSnapshot {
value:A=null;
}
table FooUpdate {
value:A=null;
}
and the corresponding code:
extension EnumUnknown_Foo {
...
mutating func update(_ update: EnumUnknown_FooUpdate) {
if let value = update.value {
self.mutate(value: value)
}
}
from flatbuffers.
Related Issues (20)
- Is it binary-compatible to delete (required) attribute?
- [Python] Use 'T' suffix object for serialization
- undocumented breaking change GenerateText->GenText
- [Java, FlatBuffers 23.5.26, Mac OS 14.3] getByKey returns null while accessing using vector index works
- `get_root_as_*` functions not generated [Rust, 23.5.26, Linux] HOT 1
- Generating schemas with language adapters (as optional add-ons)?
- Streaming flatbuffers in java HOT 1
- [TS] optional escalar not set in the binary when it holds value 0 HOT 13
- Release a new version, please HOT 1
- Default value of 0 for field is not part of [optional] enum (java)
- Buffer generated in python gets rejected by flatcc [Python / C, Flatbuffer 23.5.26] HOT 1
- Invalid Code generation if Property has same name as Struct [Windows/C#/24.3.7]
- v24.3.7 of Rust crate not released to crates.io HOT 4
- [v24.3.7] Rust: push_u8_u64_alignment test fails on i686 target (Ubuntu 22.04, CentOS 9)
- How to print nested table using FlatBufferToString?
- [Python] Flatc stub compatibility with gRPC asyncio Interface
- https://youtube.com/@Ulfat3215?si=1fAriKOReP56TyxO
- https://youtube.com/@Ulfat3215?si=1fAriKOReP56TyxO
- [Rust] Add better support for "crate-per-schema" 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 flatbuffers.