public override void Build() { if (this.memberNode.Slices.Count <= 1) { return; } // Block is marked with !, so the top level lines are not merged, so we // dont have duplicate Read/Write methods. // block may have been defined in a class merged into this one, // so check to see if it exists. String readBlockName = $"ReadHasMembers"; String writeBlockName = $"WriteHasMembers"; String referenceBlockName = $"!HasMembers"; CodeBlockNested memberBlock = this.codeBlocks.ClassMethods.Find(referenceBlockName, false); if (memberBlock == null) { this.codeBlocks.ClassMethods .BlankLine() .StartBlock(referenceBlockName) .BlankLine() .SummaryOpen() .Summary($"Read all values from resource into this instance") .SummaryClose() .AppendCode($"private void ReadHasMembers(BreastRadiologyDocument doc)") .OpenBrace() .AppendCode($"List<{this.FhirClassName}> items = this.Resource.GetValue<{this.FhirClassName}>(\"hasMember\").ToList();") .DefineBlock(out this.readBlock, readBlockName) .CloseBrace() .BlankLine() .SummaryOpen() .Summary($"Write all values from this instance into resource") .SummaryClose() .AppendCode($"private void WriteHasMembers(BreastRadiologyDocument doc)") .OpenBrace() .AppendCode($"List<{this.FhirClassName}> items = new List<{this.FhirClassName}>();") .DefineBlock(out this.writeBlock, writeBlockName) .AppendCode($"this.Resource.SetValue(\"hasMember\", items);") .CloseBrace() .EndBlock() ; } else { this.readBlock = memberBlock.Find(readBlockName); this.writeBlock = memberBlock.Find(writeBlockName); } if (this.codeBlocks.ClassWriteCode.Find("!WriteHasMembers") == null) { this.codeBlocks.ClassWriteCode .StartBlock("!WriteHasMembers") .AppendCode($"this.WriteHasMembers(this.Doc);") .EndBlock() ; } if (this.codeBlocks.ClassReadCode.Find("!ReadHasMembers") == null) { this.codeBlocks.ClassReadCode .StartBlock("!ReadHasMembers") .AppendCode($"this.ReadHasMembers(this.Doc);") .EndBlock() ; } foreach (ElementTreeSlice memberSlice in this.memberNode.Slices.Skip(1)) { ElementDefinition sliceDef = memberSlice.ElementDefinition; this.sliceName = sliceDef.SliceName; Int32 max = CSMisc.ToMax(sliceDef.Max); Int32 min = sliceDef.Min.Value; this.sliceName = this.sliceName.ToMachineName(); if ((sliceDef.Type.Count != 1) || (sliceDef.Type[0].Code != "Reference")) { throw new Exception($"Invalid reference types"); } this.itemElementSetName.Clear(); this.targetProfiles.Clear(); foreach (String profile in sliceDef.Type[0].TargetProfile) { this.targetProfiles.Add(profile); String propertyClassName = CSMisc.ResourceClassName(profile); this.itemElementSetName.Add(propertyClassName); } this.containerClassName = $"M{this.sliceName.ToMachineName()}"; this.itemElementGetName = (this.itemElementSetName.Count == 1) ? this.itemElementSetName [0] : "ResourceBase"; base.BuildOne(memberSlice.ElementDefinition.ElementId, min, max); } }
public override void Build() { if (this.memberNode.Slices.Count <= 1) { return; } // Block is marked with !, so the top level lines are not merged, so we // dont have duplicate Read/Write methods. // block may have been defined in a class merged into this one, // so check to see if it exists. String readBlockName = $"ReadSections"; String writeBlockName = $"WriteSections"; String sectionBlockName = $"!Sections"; CodeBlockNested sectionBlock = this.codeBlocks.ClassMethods.Find(sectionBlockName, false); if (sectionBlock == null) { this.codeBlocks.ClassMethods .BlankLine() .StartBlock(sectionBlockName) .BlankLine() .SummaryOpen() .Summary($"Read all values from resource into this instance") .SummaryClose() .AppendCode($"private void ReadSections(BreastRadiologyDocument doc)") .OpenBrace() .AppendCode($"List<{this.FhirClassName}> items = this.Resource.GetValue<{this.FhirClassName}>(\"{this.fhirName}\").ToList();") .DefineBlock(out this.readBlock, readBlockName) .CloseBrace() .BlankLine() .SummaryOpen() .Summary($"Write all values from this instance into resource") .SummaryClose() .AppendCode($"private void WriteSections(BreastRadiologyDocument doc)") .OpenBrace() .AppendCode($"List<{this.FhirClassName}> items = new List<{this.FhirClassName}>();") .DefineBlock(out this.writeBlock, writeBlockName) .AppendCode($"this.Resource.SetValue(\"{this.fhirName}\", items);") .CloseBrace() .EndBlock() ; } else { this.readBlock = sectionBlock.Find(readBlockName); this.writeBlock = sectionBlock.Find(writeBlockName); } if (this.codeBlocks.ClassWriteCode.Find("!WriteSections") == null) { this.codeBlocks.ClassWriteCode .StartBlock("!WriteSections") .AppendCode($"this.WriteSections(this.Doc);") .EndBlock() ; } if (this.codeBlocks.ClassReadCode.Find("!ReadSections") == null) { this.codeBlocks.ClassReadCode .StartBlock("!ReadSections") .AppendCode($"this.ReadSections(this.Doc);") .EndBlock() ; } foreach (ElementTreeSlice memberSlice in this.memberNode.Slices.Skip(1)) { ElementDefinition memberDef = memberSlice.ElementDefinition; this.sliceName = memberDef.SliceName; ElementTreeNode entryNode = memberSlice.Nodes.GetItem("entry"); ElementTreeNode titleNode = memberSlice.Nodes.GetItem("title"); ElementTreeNode codeNode = memberSlice.Nodes.GetItem("code"); this.sectionCode = (CodeableConcept)codeNode.ElementDefinition.Fixed; if (this.sectionCode == null) { this.sectionCode = (CodeableConcept)codeNode.ElementDefinition.Pattern; } if (this.sectionCode == null) { throw new Exception("Missing section.code fixed value"); } this.title = ((FhirString)titleNode.ElementDefinition?.Fixed)?.Value; if (String.IsNullOrEmpty(this.title) == true) { throw new Exception("Section missing title value"); } ElementDefinition entryDef = entryNode.ElementDefinition; Int32 max = CSMisc.ToMax(entryDef.Max); Int32 min = entryDef.Min.Value; String propertyName = this.sliceName.ToMachineName(); if (entryDef.Type.Count != 1) { throw new Exception($"Error processing slice {this.sliceName}. Expected single type. Got {entryDef.Type.Count}"); } if (entryDef.Type[0].Code != "Reference") { throw new Exception($"Error processing slice {this.sliceName}. Expected type Section. Got {entryDef.Type[0].Code}"); } this.itemElementSetName.Clear(); foreach (String profile in entryDef.Type[0].TargetProfile) { String propertyClassName = CSMisc.ResourceClassName(profile); this.itemElementSetName.Add(propertyClassName); } this.containerClassName = $"M{this.sliceName.ToMachineName()}"; this.itemElementGetName = (this.itemElementSetName.Count == 1) ? this.itemElementSetName[0] : "ResourceBase"; base.BuildOne(memberDef.ElementId, min, max); } }