Skip to content

Commit

Permalink
Implement sharing Attrib Metada for loaded jsons
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Oct 26, 2023
1 parent b73a4d7 commit 6ad3d53
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public void Initialize(int appId, IEnumerable<IContentType> types, IEntitiesSour

public AppState App
{
get => AppPackageOrNull ?? throw new Exception("cannot use app in serializer without initializing it first, make sure you call Initialize(...)");
set => AppPackageOrNull = value;
get => AppOrNull ?? throw new Exception("cannot use app in serializer without initializing it first, make sure you call Initialize(...)");
set => AppOrNull = value;
}
protected AppState AppPackageOrNull { get; private set; }
protected AppState AppOrNull { get; private set; }

public bool PreferLocalAppTypes = false;

Expand Down
34 changes: 28 additions & 6 deletions ToSic.Eav.ImportExport/Json/JsonDeserializer_ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IContentType ConvertContentType(JsonContentTypeSet json)
var lMain = Log.Fn<IContentType>();
var contentType = DirectEntitiesSource.Using(relationships =>
{
var relationshipsSource = AppPackageOrNull as IEntitiesSource ?? relationships.Source;
var relationshipsSource = AppOrNull as IEntitiesSource ?? relationships.Source;

IEntity ConvertPart(JsonEntity e) =>
Deserialize(e, AssumeUnknownTypesAreDynamic, false, relationshipsSource);
Expand All @@ -59,16 +59,38 @@ IEntity ConvertPart(JsonEntity e) =>
var attribs = jsonType.Attributes
.Select((jsonAttr, pos) =>
{
var mdEntities = jsonAttr.Metadata?.Select(ConvertPart).ToList() ?? new List<IEntity>();
var valType = ValueTypeHelpers.Get(jsonAttr.Type);

// #SharedFieldDefinition
bool hasSourceGuid = jsonAttr.SysSettings?.SourceGuid != null;
var mdEntities = hasSourceGuid
? null
: jsonAttr.Metadata?.Select(ConvertPart).ToList() ?? new List<IEntity>();
// Only provide deferred source if App really exists, must use AppOrNull
// Note that we can't use a ? : syntax, because it would require C# 9
//Func<Metadata.IHasMetadataSource> deferredSource = () => AppOrNull;
//if (AppOrNull == null) deferredSource = null;

var attrMetadata = new ContentTypeAttributeMetadata(key: default, type: valType,
name: jsonAttr.Name, sourceGuid: jsonAttr.SysSettings?.SourceGuid, items: mdEntities, appSource: AppOrNull /*, deferredSource: deferredSource */);

var attDef = Services.DataBuilder.TypeAttributeBuilder
.Create(appId: AppId, name: jsonAttr.Name, type: ValueTypeHelpers.Get(jsonAttr.Type),
isTitle: jsonAttr.IsTitle, sortOrder: pos,
.Create(
appId: AppId,
name: jsonAttr.Name,
type: valType,
isTitle: jsonAttr.IsTitle,
sortOrder: pos,
// #SharedFieldDefinition
guid: jsonAttr.Guid,
sysSettings: jsonAttr.SysSettings?.ToSysSettings(),
// metadataItems: mdEntities,
metadata: attrMetadata
);

metadataItems: mdEntities);
relationships.List?.AddRange(mdEntities);
// #SharedFieldDefinition
if (mdEntities != null)
relationships.List?.AddRange(mdEntities);
return (IContentTypeAttribute)attDef;
})
.ToList();
Expand Down
4 changes: 2 additions & 2 deletions ToSic.Eav.ImportExport/Json/JsonDeserializer_Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public IEntity Deserialize(JsonEntity jEnt,


l.A("build entity");
var partsBuilder = EntityPartsBuilder.ForAppAndOptionalMetadata(source: AppPackageOrNull, metadata: mdItems);
var partsBuilder = EntityPartsBuilder.ForAppAndOptionalMetadata(source: AppOrNull, metadata: mdItems);
var newEntity = Services.DataBuilder.Entity.Create(
appId: AppId,
guid: jEnt.Guid,
Expand All @@ -101,7 +101,7 @@ public IEntity Deserialize(JsonEntity jEnt,
metadataFor: target,
contentType: contentType,
isPublished: true,
//source: AppPackageOrNull,
//source: AppOrNull,
//metadataItems: mdItems,
partsBuilder: partsBuilder,
created: DateTime.MinValue,
Expand Down
2 changes: 1 addition & 1 deletion ToSic.Eav.ImportExport/Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ protected JsonSerializer(MyServices services, string logName): base(services, lo

internal static class StringHelpers
{
public static string EmptyAlternative(this string s, string alternative) => string.IsNullOrEmpty(s) ? alternative : s;
public static string EmptyFallback(this string s, string alternative) => string.IsNullOrEmpty(s) ? alternative : s;
}
}
2 changes: 1 addition & 1 deletion ToSic.Eav.ImportExport/Json/JsonSerializer_Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ private static string LanguageKey(IValue v) =>
.OrderBy(l => l.ReadOnly)
.Select(l => (l.ReadOnly ? ReadOnlyMarker : "") + l.Key)
.ToArray())
.EmptyAlternative(NoLanguage);
.EmptyFallback(NoLanguage);
}
}
3 changes: 3 additions & 0 deletions ToSic.Eav.ImportExport/Persistence.File/FileSystemLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public JsonSerializer Serializer
{
if (_ser != null) return _ser;
_ser = _jsonSerializerGenerator.New();
// #SharedFieldDefinition
// Also provide AppState if possible, for new #SharedFieldDefinition
if (EntitiesSource is AppState withAppState) _ser.Initialize(withAppState);
_ser.Initialize(AppId, new List<IContentType>(), EntitiesSource);
_ser.AssumeUnknownTypesAreDynamic = true;
return _ser;
Expand Down

0 comments on commit 6ad3d53

Please sign in to comment.