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

chore: update fielddeclaration to a class #3492

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected override IReadOnlyList<EnumTypeMember> BuildMembers()
// for initializationValue, if the enum is extensible, we always need it
var initializationValue = Literal(inputValue.Value);
var field = new FieldDeclaration(
Description: FormattableStringHelpers.FromString(inputValue.Description),
Modifiers: modifiers,
Type: ValueType,
Name: name,
InitializationValue: initializationValue);
modifiers,
ValueType,
name,
FormattableStringHelpers.FromString(inputValue.Description),
initializationValue);

values[i] = new EnumTypeMember(valueName, field, inputValue.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@

namespace Microsoft.Generator.CSharp
{
public sealed record FieldDeclaration(FormattableString? Description, FieldModifiers Modifiers, CSharpType Type, string Name, ValueExpression? InitializationValue)
public sealed class FieldDeclaration
{
public string Accessibility => (Modifiers & FieldModifiers.Public) > 0 ? "public" : "internal";
public FormattableString? Description { get; }
public FieldModifiers Modifiers { get; }
public CSharpType Type { get; }
public string Name { get; }
public ValueExpression? InitializationValue { get; }

private CodeWriterDeclaration? _declaration;

public CodeWriterDeclaration Declaration => _declaration ??= new CodeWriterDeclaration(Name);
jorgerangel-msft marked this conversation as resolved.
Show resolved Hide resolved

public FieldDeclaration(FieldModifiers modifiers, CSharpType type, string name)
: this(description: null,
modifiers: modifiers,
type: type,
name: name)
{ }

public FieldDeclaration(FormattableString? description, FieldModifiers modifiers, CSharpType type, string name)
: this(Description: description,
Modifiers: modifiers,
Type: type,
Name: name,
InitializationValue: null)
{ }
public FieldDeclaration(
FieldModifiers modifiers,
CSharpType type,
string name,
FormattableString? description = null,
ValueExpression? initializationValue = null)
{
Modifiers = modifiers;
Type = type;
Name = name;
Description = description;
InitializationValue = initializationValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ protected override IReadOnlyList<EnumTypeMember> BuildMembers()
// for fixed enum, we only need it for int values, for other value typed fixed enum, we use the serialization extension method to give the values (because assigning them to enum members cannot compile)
var initializationValue = IsIntValueType ? Literal(inputValue.Value) : null;
var field = new FieldDeclaration(
Description: FormattableStringHelpers.FromString(inputValue.Description),
Modifiers: modifiers,
Type: ValueType,
Name: name,
InitializationValue: initializationValue);
modifiers,
ValueType,
name,
FormattableStringHelpers.FromString(inputValue.Description),
initializationValue);

values[i] = new EnumTypeMember(name, field, inputValue.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,9 @@ public void TestWriteMethodDeclarationNoScope_ConstructorSignature()
[Test]
public void CodeWriter_WriteField()
{
var field1 = new FieldDeclaration($"To test int", FieldModifiers.Private, typeof(int), "_intConst");
var field2 = new FieldDeclaration($"To test string", FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "_stringValue");
var field3 = new FieldDeclaration($"To test a field with initialization value", FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "withValue")
{
InitializationValue = Literal("abc")
};
var field1 = new FieldDeclaration(FieldModifiers.Private, typeof(int), "_intConst", $"To test int");
var field2 = new FieldDeclaration(FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "_stringValue", $"To test string");
var field3 = new FieldDeclaration(FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "withValue", $"To test a field with initialization value", Literal("abc"));

var codeWriter = new CodeWriter();
codeWriter.WriteField(field1);
Expand Down
Loading