From 5d96a9594d4b072ca35becd48bda367ce4958558 Mon Sep 17 00:00:00 2001 From: Carl de Billy Date: Sat, 16 Feb 2019 17:02:46 -0500 Subject: [PATCH] Added support for readonly immutable fields --- .../Given_ImmutableEntity.Fields.cs | 35 +++++++++++++++++++ src/Uno.CodeGen/ImmutableGenerator.cs | 15 +++++--- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/Uno.CodeGen.Tests/Given_ImmutableEntity.Fields.cs diff --git a/src/Uno.CodeGen.Tests/Given_ImmutableEntity.Fields.cs b/src/Uno.CodeGen.Tests/Given_ImmutableEntity.Fields.cs new file mode 100644 index 0000000..daf9f3d --- /dev/null +++ b/src/Uno.CodeGen.Tests/Given_ImmutableEntity.Fields.cs @@ -0,0 +1,35 @@ +// ****************************************************************** +// Copyright � 2015-2018 nventive inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ****************************************************************** +using System.Collections.Generic; + +namespace Uno.CodeGen.Tests +{ + [GeneratedImmutable] + internal partial class MyEntityWithFields + { + private readonly IReadOnlyDictionary _readonlyField; + + public MyEntityWithFields(IReadOnlyDictionary readonlyField) + { + _readonlyField = readonlyField; + } + } + + partial class Given_ImmutableEntity + { + } +} diff --git a/src/Uno.CodeGen/ImmutableGenerator.cs b/src/Uno.CodeGen/ImmutableGenerator.cs index 86c542c..32d7979 100644 --- a/src/Uno.CodeGen/ImmutableGenerator.cs +++ b/src/Uno.CodeGen/ImmutableGenerator.cs @@ -951,10 +951,17 @@ $@"public sealed class {symbolName}BuilderJsonConverterTo{symbolName}{genericArg if (!field.IsStatic) { - Error( - builder, - $"Immutable type {symbolNames.SymbolNameWithGenerics} cannot " - + $"have the non-static field {field.Name}. You must remove it for immutable generation or make it static."); + if (field.IsReadOnly) + { + CheckTypeImmutable(field.Type, $"Field {field.Name}"); + } + else + { + Error( + builder, + $"Immutable type {symbolNames.SymbolNameWithGenerics} cannot " + + $"have the non-static field {field.Name}. You must either remove it, make it readonly or static to allow for immutable generation."); + } } } }