/* ========================================================================
* 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;
using System.IO;
using System.Runtime.CompilerServices;
using Opc.Ua.SourceGeneration.Dependency;
namespace Opc.Ua.SourceGeneration
{
///
/// Information read from a referenced assembly's
/// . Value-type equatable so the
/// incremental source-generator cache keys behave correctly.
///
public readonly struct ModelDependencyReference : IEquatable
{
///
/// Create a reference.
///
public ModelDependencyReference(
string assemblyName,
string modelUri,
string prefix,
string version,
string publicationDate,
string? name = null,
string? payload = null)
{
AssemblyName = assemblyName ?? string.Empty;
ModelUri = modelUri ?? string.Empty;
Prefix = prefix ?? string.Empty;
Version = version ?? string.Empty;
PublicationDate = publicationDate ?? string.Empty;
Name = name ?? string.Empty;
Payload = payload ?? string.Empty;
}
///
/// Returns true when the entry has both a model URI and a C# prefix.
///
public bool IsValid => ModelUri.Length != 0 && Prefix.Length != 0;
///
/// The assembly the attribute was read from.
///
public string AssemblyName { get; }
///
/// The OPC UA model URI recorded in the attribute.
///
public string ModelUri { get; }
///
/// The C# namespace the assembly used for this model's generated types.
///
public string Prefix { get; }
///
/// The model version string (may be empty).
///
public string Version { get; }
///
/// The model publication date (ISO-8601; may be empty).
///
public string PublicationDate { get; }
/// The C# identifier used for the model's name constant inside
/// the referenced assembly's Namespaces class (may be empty).
public string Name { get; }
///
/// Base64-encoded Deflate-compressed ModelDependencyV1
/// payload. Empty on transitive-dependency entries; non-empty
/// only on the producing assembly's self-declaration entry.
///
public string Payload { get; }
///
/// Decodes and memoises the as a
/// . Returns null when the
/// payload is empty, malformed, or carries an unknown version.
///
///
/// Memoisation is keyed on the payload string to avoid re-decoding
/// the same multi-kilobyte byte block when the same dependency
/// flows through Roslyn's incremental cache multiple times.
///
public ModelDependencyV1? GetDependency()
{
if (string.IsNullOrEmpty(Payload))
{
return null;
}
return s_decoded.GetValue(Payload, DecodePayload).Value;
}
private static DecodedDependency DecodePayload(string payload)
{
try
{
return new DecodedDependency(ModelDependencyV1.FromBase64Payload(payload));
}
catch (Exception ex) when (
ex is InvalidDataException ||
ex is IOException ||
ex is ArgumentException ||
ex is FormatException ||
ex is OverflowException)
{
return new DecodedDependency(null);
}
}
private sealed class DecodedDependency
{
public DecodedDependency(ModelDependencyV1? value)
{
Value = value;
}
public ModelDependencyV1? Value { get; }
}
private static readonly ConditionalWeakTable s_decoded
= new();
///
public bool Equals(ModelDependencyReference other)
{
return
string.Equals(AssemblyName, other.AssemblyName, StringComparison.Ordinal) &&
string.Equals(ModelUri, other.ModelUri, StringComparison.Ordinal) &&
string.Equals(Prefix, other.Prefix, StringComparison.Ordinal) &&
string.Equals(Version, other.Version, StringComparison.Ordinal) &&
string.Equals(PublicationDate, other.PublicationDate, StringComparison.Ordinal) &&
string.Equals(Name, other.Name, StringComparison.Ordinal) &&
string.Equals(Payload, other.Payload, StringComparison.Ordinal);
}
///
public override bool Equals(object? obj)
{
return obj is ModelDependencyReference other && Equals(other);
}
///
public override int GetHashCode()
{
unchecked
{
int hash = StringComparer.Ordinal.GetHashCode(AssemblyName);
hash = (hash * 397) ^ StringComparer.Ordinal.GetHashCode(ModelUri);
hash = (hash * 397) ^ StringComparer.Ordinal.GetHashCode(Prefix);
hash = (hash * 397) ^ StringComparer.Ordinal.GetHashCode(Version);
hash = (hash * 397) ^ StringComparer.Ordinal.GetHashCode(PublicationDate);
hash = (hash * 397) ^ StringComparer.Ordinal.GetHashCode(Name);
hash = (hash * 397) ^ StringComparer.Ordinal.GetHashCode(Payload);
return hash;
}
}
///
public static bool operator ==(ModelDependencyReference left, ModelDependencyReference right)
{
return left.Equals(right);
}
///
public static bool operator !=(ModelDependencyReference left, ModelDependencyReference right)
{
return !left.Equals(right);
}
}
}