Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent construction of invalid messages #2047

Open
rkm opened this issue Dec 12, 2024 · 2 comments
Open

Prevent construction of invalid messages #2047

rkm opened this issue Dec 12, 2024 · 2 comments
Labels
priority/low type/bug Something isn't working

Comments

@rkm
Copy link
Member

rkm commented Dec 12, 2024

It should not be possible to construct a message class that is not valid; however, all message types currently have public setters. Some classes have a "validate" method, but this is too easy to forget about.

@rkm rkm added type/bug Something isn't working priority/low labels Dec 12, 2024
@rkm
Copy link
Member Author

rkm commented Dec 15, 2024

Using records might also help here as they can generate a lot of the boilerplate code.

@rkm
Copy link
Member Author

rkm commented Dec 17, 2024

Having looked at this in some more detail, I think using records is a good idea but we'll still likely need to add some sort of Validate method as part of the interface that is checked before messages are sent/received. I had hoped that we would be able to implement validation using the type system e.g.,

// https://www.youtube.com/watch?v=KFx9XHpoYV4
public readonly record struct NonEmptyTrimmedString(string Value)
{
    public required string Value { get; init; } =
        !string.IsNullOrWhiteSpace(Value) ? Value.Trim() :
        throw new ArgumentException("Value cannot be null or whitespace", nameof(Value));

    public NonEmptyTrimmedString() : this(string.Empty) { } // Throws

    public static implicit operator string(NonEmptyTrimmedString value) => value.Value;
}

However, it's always possible to zero-initialize a struct so the above doesn't actually provide a guarantee that the validation code is executed. Maybe this would work as a class instead of a struct, but then you lose the value-type semantics.

In any case, these changes should wait until we've implemented the CTP C# wrapper so that we are free to tweak the message classes and not have to worry about mirroring it in Java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority/low type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant