public static ICollection <AnnotatedSchema> CleanAndDedupeSchemas(ICollection <JsonSchema4> schemas) { var result = new Dictionary <JsonSchema4, AnnotatedSchema>(); foreach (var currentSchema in schemas) { if (currentSchema.ActualSchema.AnyOf.Count > 0) { foreach (var alternativeResult in CleanAndDedupeSchemas(currentSchema.ActualSchema.AnyOf)) { result[alternativeResult.Schema] = alternativeResult; } } else { AnnotatedSchema entry = new AnnotatedSchema(); entry.Schema = currentSchema.ActualSchema; entry.Title = currentSchema.Title ?? entry.Schema.Title; entry.Description = currentSchema.Description ?? entry.Schema.Description; if (currentSchema is JsonProperty) { entry.Name = (currentSchema as JsonProperty).Name; entry.IsRequired = (currentSchema as JsonProperty).IsRequired; } result[entry.Schema] = entry; } } return(result.Values); }
public static string DescribeSchema(JsonSchema4 schema) { AnnotatedSchema annotatedSchema = new AnnotatedSchema(); annotatedSchema.Schema = schema; annotatedSchema.Title = schema.Title; annotatedSchema.Description = schema.Description; annotatedSchema.IsRequired = (schema as JsonProperty)?.IsRequired ?? false; return(DescribeSchema(annotatedSchema)); }
public static string DescribeSchema(AnnotatedSchema annotatedSchema) { var prefix = annotatedSchema.IsRequired ? "[required] " : ""; if (annotatedSchema.Title != null) { return(prefix + annotatedSchema.Title); } var schema = annotatedSchema.Schema; if (schema.AnyOf.Count > 0) { return(prefix + schema.AnyOf.Count + " possible formats"); } if (schema.Enumeration.Count == 1) { return(prefix + "Must be " + FormatEnumValue(schema.Enumeration.First())); } if (schema.Enumeration.Count > 0) { return(prefix + "One of: " + string.Join(", ", schema.Enumeration.Select(o => FormatEnumValue(o)))); } switch (schema.Type) { case NJsonSchema.JsonObjectType.Boolean: return(prefix + "Boolean"); case NJsonSchema.JsonObjectType.Integer: case NJsonSchema.JsonObjectType.Number: return(DescribeNumberType(schema)); case NJsonSchema.JsonObjectType.Array: return(prefix + "Array [...]"); case NJsonSchema.JsonObjectType.Object: return(prefix + "Object {...}"); case NJsonSchema.JsonObjectType.String: return(prefix + "String"); case NJsonSchema.JsonObjectType.Null: return(prefix + "Must be null"); case NJsonSchema.JsonObjectType.File: case NJsonSchema.JsonObjectType.None: default: return(prefix + "Unspecified format"); } }
private IEnumerable <AutocompleteItem> SuggestProperties(JObject contextObject, AnnotatedSchema annotatedSchema) { var yieldedAny = false; var curSchema = annotatedSchema.Schema; do { foreach (var propertyDef in curSchema.ActualProperties) { yield return(new PropertySuggestItem(propertyDef.Key, propertyDef.Value.ActualPropertySchema, contextObject.Property(propertyDef.Key) != null)); yieldedAny = true; } } while ((curSchema = curSchema.InheritedSchema) != null); if (!yieldedAny && annotatedSchema.Schema.PatternProperties.Count > 0) { yield return(new PropertySuggestItem("", annotatedSchema.Schema.PatternProperties.First().Value, false)); } }
private IEnumerable <AutocompleteItem> SuggestValues(AnnotatedSchema annotatedSchema) { // Help text. var title = annotatedSchema.Title ?? annotatedSchema.Name; var description = annotatedSchema.Description ?? JsonSchemaTools.DescribeSchema(annotatedSchema); var targetSchema = annotatedSchema.Schema; if (targetSchema.Id != null && customSourcesBySchemeId.ContainsKey(targetSchema.Id)) { // Special handling for sources set by the environment (edge names, node names, files, etc.). foreach (var value in customSourcesBySchemeId[targetSchema.Id]()) { yield return(new ValueSuggestItem(value, title != null ? title + ": " + value : value, description)); } } else if (targetSchema.Id == "http://stonehearth.net/schemas/encounters/elements/file.json") { // Special handling for files (alias or file insertion prompts). yield return(new AliasSuggestItem(title, description)); yield return(new FileSuggestItem(title, description, filePath)); } else if (targetSchema.Enumeration.Count > 0) { // Suggest enumeration alternatives. foreach (var alternative in targetSchema.Enumeration) { yield return(new ValueSuggestItem(JsonSchemaTools.FormatEnumValue(alternative), title, description)); } } else if (targetSchema.Type == JsonObjectType.Boolean) { // Suggest the two alternatives of bool. yield return(new ValueSuggestItem("true", title, description)); yield return(new ValueSuggestItem("false", title, description)); } else if (targetSchema.Default != null) { // If there's a default value, suggest that. yield return(new ValueSuggestItem(JsonSchemaTools.FormatEnumValue(targetSchema.Default), title, description)); } else if (targetSchema.Type == JsonObjectType.Object || targetSchema.ActualProperties.Count > 0) { // Construct as much of an object as required. var left = "{\n "; var right = "\n}"; var addedAnyToLeft = false; foreach (var property in targetSchema.ActualProperties) { if (property.Value.IsRequired) { if (addedAnyToLeft) { left += ",\n "; } left += "\"" + property.Key + "\": "; if (property.Value.Default != null) { left += JsonSchemaTools.FormatEnumValue(property.Value.Default); } else if (property.Value.Enumeration.Count == 1) { left += JsonSchemaTools.FormatEnumValue(property.Value.Enumeration.First()); } else if (property.Value.Type == JsonObjectType.Number || property.Value.Type == JsonObjectType.Integer) { left += "0"; } else if (property.Value.Type == JsonObjectType.Boolean) { left += "false"; } else if (property.Value.Type == JsonObjectType.Null) { left += "null"; } else if (property.Value.Type == JsonObjectType.Object) { left += "{}"; } else if (property.Value.Type == JsonObjectType.Array) { left += "[]"; } else if (property.Value.Type == JsonObjectType.String) { left += "\"\""; } addedAnyToLeft = true; } } if (addedAnyToLeft) { right = "," + right; } yield return(new ValueSuggestItem(left, right, title, description)); } else if (targetSchema.Type == JsonObjectType.Array) { // Trivial array form. yield return(new ValueSuggestItem("[\n ", "\n]", title, description)); } else if (targetSchema.Type == JsonObjectType.String) { // Trivial string form. yield return(new ValueSuggestItem("\"", "\"", title ?? "\"...\"", description)); } else if (targetSchema.Type == JsonObjectType.Number || targetSchema.Type == JsonObjectType.Integer) { // Trivial number. yield return(new ValueSuggestItem((targetSchema.Minimum ?? 0).ToString(), title, description)); } else if (targetSchema.Type == JsonObjectType.Null) { // Null has only one option. yield return(new ValueSuggestItem("null", title, description)); } }