Skip to content

Conversation

@bogdandrutu
Copy link
Member

@bogdandrutu bogdandrutu commented Jan 13, 2026

Updates #45365

This PR adds all supported types and the concept of a "Value". A value can be a concrete type value or a nil value of that type. There is a special "NoneValue" that will be used for functions that don't return anything.

Skipping changelog since this code is not yet plugged to be used in the binary.

return newValue(false, true)
}

func ToBoolValue(v Value) (BoolValue, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Question: Do we want to support automatic type conversion when possible, or ask users to explicitly call a conversion function like "String(attributes["foo"])"

@bogdandrutu bogdandrutu force-pushed the add-ottlrun branch 2 times, most recently from f602950 to db552bc Compare January 14, 2026 00:11
Copy link
Contributor

@florianl florianl left a comment

Choose a reason for hiding this comment

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

Maybe just a personal preference:
Instead of implementing NewXValue and NewNilXValue for every type, just have NewXValue and figure out the nil internally before calling newValue().


func (v value[V]) Val() V {
if v.isNil {
panic("null pointer exception")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to do a full crash here? Also, I feel we should, at least, provide some information about why this happened or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

What else should be the behavior? Similar to when you de-reference a pointer, I am not sure what can I do, because if I return a "zero-initialized" value will corrupt/produce wrong results.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about returning an error and drop, somehow, that data?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, why is that better? Isn't the same one check caller needs to do?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, with panic, the collector crashes, leading to all data lost. With the error... maybe one bad record, collector keeps running.
I agree caller should to the check but there can be mistakes. We can fail from the caller but providing some extra context.

At least, something like fmt.Sprintf("ottlruntime: Val() called on nil %T value", v.v)

@bogdandrutu
Copy link
Member Author

Maybe just a personal preference:
Instead of implementing NewXValue and NewNilXValue for every type, just have NewXValue and figure out the nil internally before calling newValue().

@florianl can you help me with a code snippet to understand what is the proposal on how do you know if the value is nil or not?

@bogdandrutu bogdandrutu added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Jan 14, 2026
@florianl
Copy link
Contributor

Maybe just a personal preference:
Instead of implementing NewXValue and NewNilXValue for every type, just have NewXValue and figure out the nil internally before calling newValue().

@florianl can you help me with a code snippet to understand what is the proposal on how do you know if the value is nil or not?

My idea is going in the direction as discussed in #45410 (comment).

The options I see are like this:

func NewBoolValue(val *bool) BoolValue {
	if val == nil {
		return BoolValue{value: newValue(false, true)}
	}
	return BoolValue{value: newValue(*val, false)}
}

But reading your comment, it sounds like that is not a preferred option due to the allocation.

An alternative could look like this:

func NewBoolValue(arg any) (BoolValue, error) {
    switch v := arg.(type) {
    case bool:
        return BoolValue{value: newValue(val, false)}, nil
    case nil:
        return BoolValue{value: newValue(false, true)}, nil
    default:
        return value{}, fmt.Errorf("invalid type")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg/ottl Skip Changelog PRs that do not require a CHANGELOG.md entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants