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

Question: Template Version2/DataTypes/ClassWithOptionalFields.cs generates invalid code with subtyped datatype #192

Open
larws opened this issue Oct 21, 2024 · 4 comments

Comments

@larws
Copy link

larws commented Oct 21, 2024

Hi everyone,

I am currently trying to generate code with a model that includes a structure with an optional field and is derived from another structure. This leads to the generation of code which does not compile, since it generates and IsEqual(....) implementation for the structure with the optional field where base.IsEqual(....) is called. But the template doesn't contain a subtype which provides an IsEqual() method.

In case this is invalid modeling, although I could not find any indication for it in the specification, shouldn't the model compiler to stop with an error?
In case this is a valid model, shouldn't the the ClassWithOptionalFields.cs template be extended to support base types?

Thanks in advance

@opcfoundation-org
Copy link
Contributor

opcfoundation-org commented Oct 21, 2024

Can you attach your model file?

@larws
Copy link
Author

larws commented Oct 22, 2024

Attached you can find a model file (model192.xml) which demonstrates the issue. I also attached the generated code for the data type classes.

model192.zip

@opcfoundation-org
Copy link
Contributor

i looked at the code and the issue seems to be the subclass declares IsEqual as virtual instead of override.
does it compile if you manually make that change?

@larws
Copy link
Author

larws commented Oct 22, 2024

That's not the primary issue here, although that needs to be fixed as well, yes. The issue is caused by the template not supporting a custom base type and thus the resulting class with optional fields has object as its base type.

The Student class should derive from Person which in turn would provide an IsEqual method.

The result should look more like this.

public partial class Student : Person
{
    ...
    public override bool IsEqual(IEncodeable encodeable)
    {
        if (Object.ReferenceEquals(this, encodeable))
        {
            return true;
        }

        Student value = encodeable as Student;

        if (value == null)
        {
             return false;
        }

        if (value.EncodingMask != this.EncodingMask) return false;

        if (!base.IsEqual(encodeable)) return false;
        if (!Utils.IsEqual(m_firstName, value.m_firstName)) return false;
        if ((EncodingMask & StudentFields.University) != 0) if (!Utils.IsEqual(m_university, value.m_university)) return false;

        return true;
    }
    ...
}

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

No branches or pull requests

2 participants