Skip to content

Commit

Permalink
Rewrite attribute checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Jul 4, 2023
1 parent bf9a70f commit a973652
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 deletions.
6 changes: 4 additions & 2 deletions .Lib9c.Tests/Action/Factory/ClaimStakeRewardFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public void Create_ByVersion_Success(string expectActionType, int version)
{
var addr = new PrivateKey().ToAddress();
var action = ClaimStakeRewardFactory.CreateByVersion(version, addr);
var actualActionType = (string)(Text)ActionTypeAttribute.ValueOf(action.GetType())!;
Assert.Equal(expectActionType, actualActionType);
var actualActionType = action
.GetType()
.GetCustomAttribute<ActionTypeAttribute>()?.TypeIdentifier;
Assert.Equal(new Text(expectActionType), actualActionType);
}
}
}
19 changes: 6 additions & 13 deletions .Lib9c.Tests/Action/Scenario/MeadScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,16 @@ public void Contract()
public void UseGas()
{
Type baseType = typeof(Nekoyume.Action.ActionBase);
Type attrType = typeof(ActionTypeAttribute);
Type obsoleteType = typeof(ActionObsoleteAttribute);

bool IsTarget(Type type)
{
return baseType.IsAssignableFrom(type) &&
type.IsDefined(attrType) &&
type != typeof(InitializeStates) &&
ActionTypeAttribute.ValueOf(type) is { } &&
(
!type.IsDefined(obsoleteType) ||
type
.GetCustomAttributes()
.OfType<ActionObsoleteAttribute>()
.Select(attr => attr.ObsoleteIndex)
.FirstOrDefault() > ActionObsoleteConfig.V200030ObsoleteIndex
);
type != typeof(InitializeStates) &&
type.GetCustomAttribute<ActionTypeAttribute>() is { } &&
(
!(type.GetCustomAttribute<ActionObsoleteAttribute>()?.ObsoleteIndex is { } obsoleteIndex) ||
obsoleteIndex > ActionObsoleteConfig.V200030ObsoleteIndex
);
}

var assembly = baseType.Assembly;
Expand Down
5 changes: 2 additions & 3 deletions .Lib9c.Tests/Action/Snapshot/ActionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ public static IValue GetActionTypeId<T>()
{
Type attrType = typeof(ActionTypeAttribute);
Type actionType = typeof(T);
return actionType.IsDefined(attrType) &&
ActionTypeAttribute.ValueOf(actionType) is { } tid
? tid
return actionType.GetCustomAttribute<ActionTypeAttribute>() is { } attr
? attr.TypeIdentifier
: throw new ArgumentException(
$"The action type attribute is missing for {typeof(T)}.");
}
Expand Down
18 changes: 5 additions & 13 deletions .Lib9c.Tools/SubCommand/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,23 @@ public void List(
)
{
Type baseType = typeof(Nekoyume.Action.ActionBase);
Type attrType = typeof(ActionTypeAttribute);
Type obsoleteType = typeof(ActionObsoleteAttribute);

bool IsTarget(Type type)
{
return baseType.IsAssignableFrom(type) &&
type.IsDefined(attrType) &&
ActionTypeAttribute.ValueOf(type) is { } &&
type.GetCustomAttribute<ActionTypeAttribute>() is { } &&
(
!excludeObsolete ||
!type.IsDefined(obsoleteType) ||
type
.GetCustomAttributes()
.OfType<ActionObsoleteAttribute>()
.Select(attr => attr.ObsoleteIndex)
.FirstOrDefault() > blockIndex
!(type.GetCustomAttribute<ActionObsoleteAttribute>() is { } aoAttr) ||
aoAttr.ObsoleteIndex > blockIndex
);
}

var assembly = baseType.Assembly;
var typeIds = assembly.GetTypes()
.Where(IsTarget)
.Select(type => ActionTypeAttribute.ValueOf(type))
.Where(type => type is Text)
.Cast<Text>()
.Select(type => type.GetCustomAttribute<ActionTypeAttribute>()?.TypeIdentifier)
.OfType<Text>()
.OrderBy(type => type);

foreach (Text typeId in typeIds) {
Expand Down
1 change: 1 addition & 0 deletions Lib9c/Action/ActionObsoleteAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Nekoyume.Action
/// starting from <see cref="ActionObsoleteAttribute.ObsoleteIndex"/> + 2.
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ActionObsoleteAttribute : Attribute
{
public ActionObsoleteAttribute(long obsoleteIndex)
Expand Down

0 comments on commit a973652

Please sign in to comment.