-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e213e7b
commit 2d849bc
Showing
4 changed files
with
206 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
using Squidex.ClientLibrary; | ||
|
||
namespace Squidex.CLI.Commands.Models | ||
{ | ||
public sealed class SchemaWithRefs<T> where T : class | ||
{ | ||
public T Schema { get; set; } | ||
|
||
public Dictionary<Guid, string> ReferencedSchemas { get; set; } | ||
|
||
public SchemaWithRefs() | ||
{ | ||
} | ||
|
||
public SchemaWithRefs(T schema) | ||
{ | ||
Schema = schema; | ||
|
||
ReferencedSchemas = new Dictionary<Guid, string>(); | ||
} | ||
|
||
public static SchemaWithRefs<T> Parse(string json) | ||
{ | ||
try | ||
{ | ||
var imported = JsonConvert.DeserializeObject<SchemaWithRefs<T>>(json); | ||
|
||
if (imported.Schema != null && imported.ReferencedSchemas != null) | ||
{ | ||
return imported; | ||
} | ||
|
||
return ParseDirectly(json); | ||
} | ||
catch | ||
{ | ||
try | ||
{ | ||
return ParseDirectly(json); | ||
} | ||
catch (IOException ex) | ||
{ | ||
throw new SquidexException($"Cannot deserialize schema: {ex.Message}"); | ||
} | ||
} | ||
} | ||
|
||
private static SchemaWithRefs<T> ParseDirectly(string json) | ||
{ | ||
var schema = JsonConvert.DeserializeObject<T>(json); | ||
|
||
return new SchemaWithRefs<T>(schema); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Squidex.ClientLibrary.Management; | ||
|
||
namespace Squidex.CLI.Commands.Models | ||
{ | ||
public static class SchemaWithRefsExtensions | ||
{ | ||
public static SchemaWithRefs<SchemaDetailsDto> EnrichSchemaNames(this SchemaWithRefs<SchemaDetailsDto> target, ICollection<SchemaDto> allSchemas) | ||
{ | ||
if (target.Schema.Fields != null) | ||
{ | ||
foreach (var field in target.Schema.Fields) | ||
{ | ||
if (field.Properties is ReferencesFieldPropertiesDto reference) | ||
{ | ||
var referenced = allSchemas.FirstOrDefault(x => x.Id == reference.SchemaId); | ||
|
||
if (referenced != null) | ||
{ | ||
target.ReferencedSchemas[referenced.Id] = referenced.Name; | ||
} | ||
} | ||
|
||
if (field.Nested != null) | ||
{ | ||
foreach (var nested in field.Nested) | ||
{ | ||
if (nested.Properties is ReferencesFieldPropertiesDto nestedReference) | ||
{ | ||
var referenced = allSchemas.FirstOrDefault(x => x.Id == nestedReference.SchemaId); | ||
|
||
if (referenced != null) | ||
{ | ||
target.ReferencedSchemas[referenced.Id] = referenced.Name; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return target; | ||
} | ||
|
||
public static SchemaWithRefs<T> AdjustReferences<T>(this SchemaWithRefs<T> target, ICollection<SchemaDto> allSchemas) where T : UpsertDto | ||
{ | ||
if (target.Schema.Fields != null) | ||
{ | ||
foreach (var field in target.Schema.Fields) | ||
{ | ||
if (field.Properties is ReferencesFieldPropertiesDto reference) | ||
{ | ||
if (target.ReferencedSchemas.TryGetValue(reference.SchemaId, out var name)) | ||
{ | ||
var referenced = allSchemas.FirstOrDefault(x => x.Name == name); | ||
|
||
if (referenced != null) | ||
{ | ||
reference.SchemaId = referenced.Id; | ||
} | ||
} | ||
} | ||
|
||
if (field.Nested != null) | ||
{ | ||
foreach (var nested in field.Nested) | ||
{ | ||
if (nested.Properties is ReferencesFieldPropertiesDto nestedReference) | ||
{ | ||
if (target.ReferencedSchemas.TryGetValue(nestedReference.SchemaId, out var name)) | ||
{ | ||
var referenced = allSchemas.FirstOrDefault(x => x.Name == name); | ||
|
||
if (referenced != null) | ||
{ | ||
nestedReference.SchemaId = referenced.Id; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return target; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters