Browse Source

Added support to retrieve schema $IDs

vpenades 9 months ago
parent
commit
271f14691b

+ 37 - 9
build/SharpGLTF.CodeGen/SchemaReflection/SchemaTypes.cs

@@ -38,13 +38,40 @@ namespace SharpGLTF.SchemaReflection
         /// <summary>
         /// identifier used for serialization and deserialization
         /// </summary>
-        public abstract string PersistentName { get; }        
+        public abstract string PersistentName { get; }
+
+        /// <summary>
+        /// This is the value usually found in the schema's "$id:"
+        /// </summary>
+        /// <example>
+        /// material.occlusionTextureInfo.schema.json
+        /// </example>
+        public string Identifier { get; set; }
+
+        public String Description { get; set; }
 
         #endregion
 
         #region properties
 
-        public String Description { get; set; }
+        /// <summary>
+        /// Short version of <see cref="Identifier"/>
+        /// </summary>
+        /// <example>
+        /// occlusionTextureInfo
+        /// </example>
+        public string ShortIdentifier
+        {
+            get
+            {
+                var id = this.Identifier.Replace(".schema.json","", StringComparison.OrdinalIgnoreCase);
+                var idx = id.LastIndexOf('.');
+                return idx < 0 ? id : id.Substring(idx+1);
+            }
+        }
+            
+
+        
 
         public Context Owner => _Owner;
 
@@ -365,13 +392,7 @@ namespace SharpGLTF.SchemaReflection
 
         private readonly SortedSet<FieldInfo> _Fields = new SortedSet<FieldInfo>(FieldInfo.Comparer);
 
-        private ClassType _BaseClass;
-        
-        public override string PersistentName => _PersistentName;
-
-        public ClassType BaseClass { get => _BaseClass; set => _BaseClass = value; }
-
-        public IEnumerable<FieldInfo> Fields => _Fields;
+        private ClassType _BaseClass;        
 
         /// <summary>
         /// True to prevent to codegen emitter to emit this class
@@ -380,6 +401,13 @@ namespace SharpGLTF.SchemaReflection
 
         #endregion
 
+        #region properties
+        public override string PersistentName => _PersistentName;
+        public IEnumerable<FieldInfo> Fields => _Fields;
+        public ClassType BaseClass { get => _BaseClass; set => _BaseClass = value; }
+
+        #endregion
+
         #region API
 
         public FieldInfo GetField(string name)

+ 24 - 2
build/SharpGLTF.CodeGen/SchemaReflection/SchemaTypesReader.cs

@@ -39,8 +39,8 @@ namespace SharpGLTF.SchemaReflection
 
         private static SchemaType _UseType(this SchemaType.Context ctx, JSONSCHEMA schema, bool isRequired = true)
         {
-            if (ctx == null) throw new ArgumentNullException(nameof(ctx));
-            if (schema == null) throw new ArgumentNullException(nameof(schema));            
+            ArgumentNullException.ThrowIfNull(ctx);
+            ArgumentNullException.ThrowIfNull(schema);
 
             if (schema is NJsonSchema.JsonSchemaProperty prop)
             {
@@ -116,6 +116,7 @@ namespace SharpGLTF.SchemaReflection
 
                     var etype = ctx.UseEnum(name, isNullable);
 
+                    etype.Identifier = _GetSchemaIdentifier(schema);
                     etype.Description = schema.Description;
 
                     foreach (var kvp in dict) etype.SetValue(kvp.Key, (int)kvp.Value);
@@ -140,6 +141,7 @@ namespace SharpGLTF.SchemaReflection
             {
                 var classDecl = ctx.UseClass(schema.Title);
 
+                classDecl.Identifier = _GetSchemaIdentifier(schema);
                 classDecl.Description = schema.Description;
 
                 // process base class                
@@ -192,6 +194,26 @@ namespace SharpGLTF.SchemaReflection
             throw new NotImplementedException();
         }
 
+        private static string _GetSchemaIdentifier(JSONSCHEMA schema)
+        {
+            if (schema.ExtensionData != null)
+            {
+                if (schema.ExtensionData.TryGetValue("$id", out var value) && value is string id)
+                {
+                    return id;
+                }
+            }
+
+            if (!string.IsNullOrWhiteSpace(schema.DocumentPath))
+            {
+                return System.IO.Path.GetFileName(schema.DocumentPath);
+            }
+
+            return null;
+
+            
+        }
+
         private static bool _IsBlittableType(JSONSCHEMA schema)
         {
             if (schema == null) return false;