Skip to content

Conversation

akhilles
Copy link
Contributor

@akhilles akhilles commented Aug 27, 2025

Example code:

load("@github/diodeinc/stdlib:v0.2.13/units.zen", "Voltage", "Current", "Resistance")

Powa = interface(
    NET = using(Net),
    voltage_meta = metadata(Voltage),
)

powa = io("POWA", Powa)

# Test 1: Basic metadata container creation and type storage
voltage_meta = powa.voltage_meta
current_meta = metadata(Current) 
resistance_meta = metadata(Resistance)

# Test 2: Storing values with complex record types (including enums)
v1 = Voltage(value=5.0, tolerance=0.05)     # 5V ±5%

c1 = Current(value=0.1, tolerance=0.05)     # 100mA ±5%
c2 = Current(value=0.02, tolerance=0.01)    # 20mA ±1%

r1 = Resistance(value=1000.0, tolerance=0.05)   # 1kΩ ±5%
r2 = Resistance(value=10000.0, tolerance=0.01)  # 10kΩ ±1%

voltage_meta.push(v1)
current_meta.push(c1)
current_meta.push(c2)
resistance_meta.push(r1)
resistance_meta.push(r2)

# Test 3: Automatic deserialization with get()
latest_voltage = voltage_meta.get()
latest_current = current_meta.get()
latest_resistance = resistance_meta.get()

print("Voltages:", voltage_meta.list())
print("Latest current:", latest_current)
print("Latest resistance:", latest_resistance)

# Test 4: Automatic deserialization with list()
all_voltages = voltage_meta.list()
print("Found", len(all_voltages), "voltage entries")

@akhilles akhilles requested a review from LK August 27, 2025 08:15
@akhilles akhilles marked this pull request as draft August 31, 2025 17:30
@dioderobot dioderobot force-pushed the metadata branch 3 times, most recently from 4b3aead to a861dc4 Compare September 23, 2025 18:11
@akhilles akhilles changed the title Add metadata(), record ser/de support Add metadata() Sep 23, 2025
@akhilles akhilles changed the title Add metadata() [ENG-8] Add metadata() Sep 23, 2025
@akhilles akhilles marked this pull request as ready for review September 23, 2025 18:13

**String Representation**:
Physical values maintain lossless string conversion, preserving original units and tolerances.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add spec for metadata here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


let list_values = input_values
.iter()
.map(|input_value| input_value.to_value(eval, None))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we have types where expected_typ is needed (e.g. enums, records, interfaces)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to convert those types to values without explicitly requiring the type to be provided at the call site. There's no easy way to extract + carry the necessary type information in Rust.

Maybe we can have an optional type hint argument? e.g. metadata.get(EnumTy). It's a bit awkward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get simple records and enums sorta working by hacking around in starlark-rust, but it was very brittle, and still failed on stuff like records containing enums.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok to say they are not supported right now especially now with native units — what's the behavior if you try to construct a metadata container of type enum(...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can construct and push values into a metadata container with enum(). On get(), list(), it'll fail with "enum input requires expected_typ".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just disallow building a container with unsupported types for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


fn get_attr(&self, attribute: &str, heap: &'v Heap) -> Option<Value<'v>> {
match attribute {
"push" => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very nice, I like this more than a bunch of global functions too

Signed-off-by: akhilles <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants