public void Add(CdmParameterDefinition element) { string name = element.Name; if (!string.IsNullOrEmpty(name)) { if (this.Lookup.ContainsKey(name)) { throw new Exception($"Duplicate parameter name {name}"); } else { this.Lookup.Add(name, element); } } this.Ordinals.Add(element, this.Sequence.Count); this.Sequence.Add(element); }
public void Add(CdmParameterDefinition element) { string name = element.Name; if (!string.IsNullOrEmpty(name)) { if (this.Lookup.ContainsKey(name)) { // why not just replace the old one? this.Lookup[name] = element; this.Sequence[this.Sequence.IndexOf(this.Sequence.Find((e) => e.GetName() == name))] = element; this.Ordinals[element] = this.Sequence.Count; return; } else { this.Lookup.Add(name, element); } } this.Ordinals.Add(element, this.Sequence.Count); this.Sequence.Add(element); }
public static Parameter ToData(CdmParameterDefinition instance, ResolveOptions resOpt, CopyOptions options) { dynamic defVal = null; if (instance.DefaultValue != null) { if (instance.DefaultValue is string || instance.DefaultValue is JValue) { defVal = instance.DefaultValue; } else { defVal = ((CdmObject)instance.DefaultValue).CopyData(resOpt, options); } } return(new Parameter { Explanation = instance.Explanation, Name = instance.Name, DefaultValue = defVal, Required = instance.Required == true ? (bool?)true : null, DataType = Utils.JsonForm(instance.DataTypeRef, resOpt, options) }); }
/// <summary> /// Processes extensions from an object which was obtained from a "model.json" file. /// From every extension found, it's value (name, value) is added to traitRefSet, /// and it's definition (name, type) is added to extensionTraitDefList. /// </summary> /// <param name="ctx"> The context </param> /// <param name="sourceObject"> The object obtained from "model.json" file.</param> /// <param name="traitRefSet"> The list of extensions found, in the form of (name & value).</param> /// <param name="extensionTraitDefList"> The list of definitions. For each extension, it's definition is added to this list (name & type).</param> /// <param name="localExtensionTraitDefList"> Same as extensionTraitDefList but limited to extensions inside one document.</param> public static void ProcessExtensionFromJson( CdmCorpusContext ctx, MetadataObject sourceObject, CdmTraitCollection traitRefSet, List <CdmTraitDefinition> extensionTraitDefList, List <CdmTraitDefinition> localExtensionTraitDefList = null) { var extensions = sourceObject.ExtensionFields; foreach (JProperty extensionAsJProperty in extensions.Children()) { string traitName = AddExtensionTraitNamePrefix(extensionAsJProperty.Name); CdmTraitDefinition extensionTraitDef = extensionTraitDefList.Find((CdmTraitDefinition trait) => trait.TraitName == traitName); bool traitExists = extensionTraitDef != null; if (!traitExists) { extensionTraitDef = ctx.Corpus.MakeObject <CdmTraitDefinition>(CdmObjectType.TraitDef, traitName); extensionTraitDef.ExtendsTrait = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, "is.extension", true); } CdmTraitReference extensionTraitRef = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, traitName); var extensionValue = extensionAsJProperty.Value; var extensionType = extensionValue.Type; var shouldLookIntoProperties = extensionType == JTokenType.Object; if (shouldLookIntoProperties) { var extensionValueAsJObject = extensionValue as JObject; // extensionProperty.Name is here the equivalent of extensionProperty from TS project foreach (JProperty extensionProperty in extensionValueAsJObject.Children()) { // for every extensionProperty will have to save the (name, value) pair into extensionArgument, // which will be saved in extensionTraitRef (the entity that will contain the data) // (name, type) will be saved in extensionParameter, // which will be saved in extensionTraitDef (the definition of the data, that can be saved in a schema file) JToken extensionPropertyValue = extensionProperty.Value; if (extensionPropertyValue == null) { continue; } CdmArgumentDefinition extensionArgument = ctx.Corpus.MakeObject <CdmArgumentDefinition>( CdmObjectType.ArgumentDef, extensionProperty.Name ); CdmParameterDefinition extensionParameter = extensionTraitDef.Parameters.AllItems.Find( (CdmParameterDefinition parameter) => parameter.Name == extensionProperty.Name); bool parameterExists = extensionParameter != null; if (!parameterExists) { extensionParameter = ctx.Corpus.MakeObject <CdmParameterDefinition>(CdmObjectType.ParameterDef, extensionProperty.Name); extensionParameter.DataTypeRef = ctx.Corpus.MakeObject <CdmDataTypeReference>(CdmObjectType.DataTypeRef, ConvertJTokenTypeToExpectedString(extensionPropertyValue.Type), true); } if (extensionPropertyValue is JValue extensionPropertyValueAsJValue) { extensionArgument.Value = extensionPropertyValueAsJValue.Value; } else { extensionArgument.Value = extensionPropertyValue; } extensionTraitRef.Arguments.Add(extensionArgument); if (!parameterExists) { extensionTraitDef.Parameters.Add(extensionParameter); } } } else { CdmArgumentDefinition extensionArgument = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, traitName); CdmParameterDefinition extensionParameter = extensionTraitDef.Parameters.AllItems.Find((CdmParameterDefinition parameter) => parameter.Name == traitName); bool parameterExists = extensionParameter != null; if (!parameterExists) { extensionParameter = ctx.Corpus.MakeObject <CdmParameterDefinition>(CdmObjectType.ParameterDef, traitName); extensionParameter.DataTypeRef = ctx.Corpus.MakeObject <CdmDataTypeReference>( CdmObjectType.DataTypeRef, ConvertJTokenTypeToExpectedString(extensionType), true); } if (extensionValue is JValue extensionValuePrimite) { extensionArgument.Value = extensionValuePrimite.Value; } else { extensionArgument.Value = extensionValue; } extensionTraitRef.Arguments.Add(extensionArgument); if (!parameterExists) { extensionTraitDef.Parameters.Add(extensionParameter); } } if (!traitExists) { extensionTraitDefList.Add(extensionTraitDef); } if (localExtensionTraitDefList != null) { localExtensionTraitDefList.Add(extensionTraitDef); } traitRefSet.Add(extensionTraitRef); } }
public ParameterValue(CdmCorpusContext ctx, CdmParameterDefinition parameter, dynamic value) { this.Parameter = parameter; this.Value = value; this.Ctx = ctx; }
public int IndexOf(CdmParameterDefinition paramDef) { return(this.PC.Ordinals[paramDef]); }