Files
716858aa7d Source generator: option to emit exactly the nodes of a NodeSet2 (#4797)
# Description

For a NodeSet2 input the generator built the predefined nodes from the
merged instance hierarchy. A subtype therefore got a copy of every
Mandatory InstanceDeclaration of its supertypes as a new child node,
with
a NodeId the NodeSet2 never assigned (the supertype's numeric identifier
in the subtype's namespace, or a string identifier derived from the path
when the supertype comes from a referenced assembly). A DeviceType
subtype got eight such children from DI's DeviceType, so the address
space
a server loads from the generated code differs from the NodeSet2 it was
generated from.

ModelSourceGeneratorEmitNodeSetNodesOnly (GeneratorOptions
.EmitNodeSetNodesOnly), off by default, makes the predefined nodes of a
NodeSet2 input exactly the nodes the NodeSet2 declares: a child the
generator derives from a supertype or type definition is not added to a
predefined node. The typed state classes are unchanged and still reach
inherited members through the state classes of their supertypes, and the
CreateInstanceOf* factories still create every Mandatory child of the
type. ModelDesign inputs are not affected. IModelDesign.IsSourceNodeSet
tells the generator whether the target model was read from a NodeSet2.

A test in Opc.Ua.Di.Tests generates a DI-style NodeSet2 with the option
on, exports the generated predefined nodes as a NodeSet2 and compares it
with the input, node for node and reference for reference.

## Related Issues

None.

## Checklist

- [x] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [x] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [x] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
2026-10-16 12:38:19 +02:00

112 lines
3.7 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System.Globalization;
namespace Opc.Ua.SourceGeneration
{
/// <summary>
/// Writes every line to an inner writer indented by four more spaces,
/// for code emitted into a nested block.
/// </summary>
internal sealed class IndentedTemplateWriter : ITemplateWriter
{
/// <summary>
/// Create the writer
/// </summary>
public IndentedTemplateWriter(ITemplateWriter inner)
{
m_inner = inner;
}
/// <inheritdoc/>
public void Write(char text)
{
Write(text.ToString());
}
/// <inheritdoc/>
public void Write(string text)
{
if (m_atLineStart && !string.IsNullOrEmpty(text))
{
m_inner.Write(kIndent);
m_atLineStart = false;
}
m_inner.Write(text);
}
/// <inheritdoc/>
public void Write(string format, object arg1)
{
Write(string.Format(CultureInfo.InvariantCulture, format ?? string.Empty, arg1));
}
/// <inheritdoc/>
public void Write(string format, object arg1, object arg2)
{
Write(string.Format(CultureInfo.InvariantCulture, format ?? string.Empty, arg1, arg2));
}
/// <inheritdoc/>
public void Write(string format, object arg1, object arg2, object arg3)
{
Write(string.Format(
CultureInfo.InvariantCulture, format ?? string.Empty, arg1, arg2, arg3));
}
/// <inheritdoc/>
public void WriteLine()
{
m_inner.WriteLine();
m_atLineStart = true;
}
/// <inheritdoc/>
public void WriteLine(string text)
{
if (m_atLineStart && !string.IsNullOrEmpty(text))
{
m_inner.Write(kIndent);
}
m_inner.WriteLine(text);
m_atLineStart = true;
}
/// <inheritdoc/>
public void WriteLine(string text, params object?[] args)
{
WriteLine(string.Format(CultureInfo.InvariantCulture, text ?? string.Empty, args ?? []));
}
private const string kIndent = " ";
private readonly ITemplateWriter m_inner;
private bool m_atLineStart = true;
}
}