Skip to content

Commit

Permalink
'required' flag on typed field is dropped
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-keeper committed Jul 16, 2024
1 parent efae4d7 commit 7d40aea
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Commander/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
Expand Down
4 changes: 2 additions & 2 deletions Commander/Commander.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
Expand Down
2 changes: 1 addition & 1 deletion Commander/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />
</packages>
30 changes: 20 additions & 10 deletions Commander/vault/RecordCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ public static async Task AddRecordCommand(this VaultContext context, AddRecordOp
await context.Vault.CreateRecord(record, node.FolderUid);
}


public static async Task UpdateRecordCommand(this VaultContext context, UpdateRecordOptions options)
{
if (context.Vault.TryGetKeeperRecord(options.RecordId, out var record))
Expand All @@ -434,6 +433,21 @@ record = r;
Console.WriteLine($"Cannot resolve record {options.RecordId}");
return;
}
if (!string.IsNullOrEmpty(options.RecordType))
{
if (record is TypedRecord typed)
{
if (context.Vault.TryGetRecordTypeByName(options.RecordType, out var rt))
{
typed.TypeName = rt.Name;
}
}
else
{
Console.WriteLine($"{options.RecordId} is a legacy record. Record type is not supported.");
return;
}
}

if (!string.IsNullOrEmpty(options.Title))
{
Expand Down Expand Up @@ -573,14 +587,7 @@ record = r;
}

var uploadTask = new FileAttachmentUploadTask(options.FileName);
if (record is PasswordRecord password)
{
await context.Vault.UploadAttachment(password, uploadTask);
}
else if (record is TypedRecord typed)
{
await context.Vault.UploadAttachment(typed, uploadTask);
}
await context.Vault.UploadAttachment(record, uploadTask);
}

public static async Task RemoveRecordCommand(this VaultContext context, RemoveRecordOptions options)
Expand Down Expand Up @@ -740,7 +747,7 @@ class AddRecordOptions
[Option("folder", Required = false, HelpText = "folder")]
public string Folder { get; set; }

[Option('t', "type", Required = true, HelpText = "record type. legacy if omitted.")]
[Option('t', "type", Required = true, HelpText = "record type.")]
public string RecordType { get; set; }

[Option("title", Required = true, HelpText = "record title.")]
Expand All @@ -758,6 +765,9 @@ class UpdateRecordOptions
[Option("title", Required = false, HelpText = "title")]
public string Title { get; set; }

[Option('t', "type", Required = true, HelpText = "record type. typed records only.")]
public string RecordType { get; set; }

[Option('g', "generate", Required = false, Default = false, HelpText = "generate random password")]
public bool Generate { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion KeeperSdk/KeeperSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe">
<Version>4.5.2</Version>
<Version>4.5.3</Version>
</PackageReference>
</ItemGroup>

Expand Down
80 changes: 42 additions & 38 deletions KeeperSdk/utils/RecordTypesUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public static IEnumerable<string> GetTypedFieldValues(this ITypedField field)
{
yield return fts.GetValueAsString();
}
else {
else
{
yield return "<not supported>";
}
}
Expand Down Expand Up @@ -387,60 +388,63 @@ public static void AdjustTypedRecord(this VaultData vault, TypedRecord typed)
{
if (!vault.TryGetRecordTypeByName(typed.TypeName, out var recordType)) return;

var fields = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
for (var i = 0; i < typed.Fields.Count; i++)
var allFields = new Dictionary<string, ITypedField>(StringComparer.InvariantCultureIgnoreCase);
foreach (var rf in typed.Fields.Concat(typed.Custom))
{
var rf = typed.Fields[i];
fields[rf.GetTypedFieldName()] = i;
}

foreach (var field in recordType.Fields)
{
if (!fields.ContainsKey(field.GetTypedFieldName()))
for (var i = rf.Count - 1; i >= 0; i--)
{
typed.Fields.Add(field.CreateTypedField());
var value = rf.GetValueAt(i);
if (value == null)
{
rf.DeleteValueAt(i);
}
}
}

fields.Clear();
for (var i = 0; i < recordType.Fields.Length; i++)
{
var rf = recordType.Fields[i];
fields[rf.GetTypedFieldName()] = i;
}

typed.Fields.Sort((f1, f2) =>
{
var name1 = f1.GetTypedFieldName();
var name2 = f2.GetTypedFieldName();
if (fields.ContainsKey(name1) && fields.ContainsKey(name2))
var fieldKey = rf.GetTypedFieldName();
if (!allFields.ContainsKey(fieldKey))
{
return fields[name1] - fields[name2];
allFields.Add(fieldKey, rf);
}
}

if (fields.ContainsKey(name1))
typed.Fields.Clear();
foreach (var field in recordType.Fields)
{
var fieldKey = field.GetTypedFieldName();
if (allFields.TryGetValue(fieldKey, out var rf))
{
return -1;
allFields.Remove(fieldKey);
}

if (fields.ContainsKey(name2))
else
{
return 1;
rf = field.CreateTypedField();
}
rf.Required = field.Required;
typed.Fields.Add(rf);
}

return 0;
});
foreach (var field in typed.Fields.Concat(typed.Custom))
var customFields = new List<ITypedField>(typed.Custom);
typed.Custom.Clear();
foreach (var rf in customFields)
{
for (var i = field.Count - 1; i >= 0; i--)
if (rf.Count > 0)
{
var value = field.GetValueAt(i);
if (value == null)
var fieldKey = rf.GetTypedFieldName();
if (allFields.ContainsKey(fieldKey))
{
field.DeleteValueAt(0);
typed.Custom.Add(rf);
allFields.Remove(fieldKey);
}
}
}
if (allFields.Count > 0)
{
typed.Custom.AddRange(allFields.Values.Where(x => x.Count > 0));
}
typed.Custom.RemoveAll((rf) => rf.Count == 0);
foreach (var rf in typed.Custom)
{
rf.Required = false;
}
}
}
}
11 changes: 10 additions & 1 deletion KeeperSdk/vault/RecordTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public RecordTypeField(RecordField recordField, string label = null)
/// Gets field label
/// </summary>
public string FieldLabel { get; }

public bool Required { get; internal set; }
}

/// <exclude/>
Expand Down Expand Up @@ -1528,13 +1530,17 @@ internal class RecordTypeDataField<T> : RecordTypeDataFieldBase

public override ITypedField CreateTypedField()
{
return new TypedField<T>(this);
return new TypedField<T>(this)
{
Required = Required
};
}

public RecordTypeDataField(TypedField<T> typedField)
{
Type = typedField.FieldName;
Label = typedField.FieldLabel;
Required = typedField.Required;
Value = typedField.Values.Where(x => x != null).ToArray();
}
}
Expand All @@ -1547,6 +1553,9 @@ internal class RecordTypeDataFieldBase : IExtensibleDataObject
public string Type { get; set; }
[DataMember(Name = "label", Order = 2, EmitDefaultValue = false)]
public string Label { get; set; }
[DataMember(Name = "required", Order = 3, EmitDefaultValue = false)]
public bool Required { get; set; }

public ExtensionDataObject ExtensionData { get; set; }

public virtual ITypedField CreateTypedField()
Expand Down
1 change: 1 addition & 0 deletions KeeperSdk/vault/VaultData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ private void LoadRecordTypes()
{
typeField = new RecordTypeField(rf, x.Label);
}
typeField.Required = x.Required ?? false;
return typeField;
}
else
Expand Down
7 changes: 1 addition & 6 deletions KeeperSdk/vault/VaultDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ public static ITypedField CreateTypedField(string fieldName, string fieldLabel =

public static ITypedField CreateTypedField(this IRecordTypeField fieldInfo)
{
var tf = CreateTypedField(fieldInfo.FieldName, fieldInfo.FieldLabel);
if (fieldInfo is RecordTypeField rtf)
{
}

return tf;
return CreateTypedField(fieldInfo.FieldName, fieldInfo.FieldLabel);
}

public static bool FindTypedField(this IList<ITypedField> fields, IRecordTypeField fieldInfo, out ITypedField field)
Expand Down
11 changes: 11 additions & 0 deletions KeeperSdk/vault/VaultTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ public interface ITypedField : IRecordTypeField
/// Gets the number of values
/// </summary>
int Count { get; }

/// <summary>
/// Gets required flag
/// </summary>
bool Required { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -855,6 +860,7 @@ public class UnsupportedField : ITypedField, IToRecordTypeDataField
internal UnsupportedField(RecordTypeDataFieldBase dataField)
{
_dataField = dataField;
Required = dataField.Required;
}

RecordTypeDataFieldBase IToRecordTypeDataField.ToRecordTypeDataField()
Expand Down Expand Up @@ -888,6 +894,8 @@ void ITypedField.DeleteValueAt(int index)

string IRecordTypeField.FieldName => _dataField.Type;
string IRecordTypeField.FieldLabel => _dataField.Label;

public bool Required { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -952,6 +960,9 @@ public TypedField(string fieldType, string fieldLabel = null)
/// </summary>
public List<T> Values { get; } = new List<T>();

/// <inheritdoc />
public bool Required { get; set; }

public T AppendTypedValue()
{
switch (Values)
Expand Down

0 comments on commit 7d40aea

Please sign in to comment.