public StuurInformatie Header() { var stuurinformatie = new StuurInformatie(); foreach (var item in ((YamlMappingNode)map.Children[new YamlScalarNode(HeaderAttribute)]).Children) { switch (item.Key.ToString()) { case HeaderSubject: stuurinformatie.Onderwerp = item.Value.ToString(); break; case HeaderOrganization: stuurinformatie.Organisatie = item.Value.ToString(); break; case HeaderType: stuurinformatie.Type = item.Value.ToString(); break; case HeaderDomain: stuurinformatie.Domein = item.Value.ToString(); break; case HeaderVersion: stuurinformatie.Versie = item.Value.ToString(); break; case HeaderStatus: stuurinformatie.Status = item.Value.ToString(); break; case HeaderYear: stuurinformatie.Jaar = item.Value.ToString(); break; case HeaderSource: stuurinformatie.Bron = item.Value.ToString(); break; default: throw new Exception($"unknown header identifider {item.Key.ToString()}"); } } return(stuurinformatie); }
public StuurInformatie Header() { var stuurinformatie = new StuurInformatie(); var node = map.Children.Where(p => p.Key.ToString() == keywords.header); var notSet = new List <string>() { keywords.header_subject, keywords.header_organization, keywords.header_type, keywords.header_domain, keywords.header_version, keywords.header_status, keywords.header_period, keywords.header_source }; if (node.Count() == 0) { throw new FlowFormattingException($"'{keywords.header}:' section is undefined.", new DebugInfo().MapDebugInfo(new Mark(), new Mark())); } var debugInfo = new DebugInfo().MapDebugInfo(node.ElementAt(0).Key.Start, node.ElementAt(0).Key.End); YamlMappingNode seq; try { seq = ((YamlMappingNode)map.Children[new YamlScalarNode(keywords.header)]); } catch (Exception) { throw new FlowFormattingException($"'{keywords.header}:' is empty and expects the following mandatory properties {string.Join(',', notSet)}", debugInfo); } foreach (var item in seq.Children) { var named = typeof(keywords).GetProperties(BindingFlags.NonPublic | BindingFlags.Static) .Where(p => p.Name.StartsWith("header_") && keywords.ResourceManager.GetString(p.Name, keywords.Culture) == item.Key.ToString()) .Select(p => p.Name).SingleOrDefault(); switch (named) { case nameof(keywords.header_subject): stuurinformatie.Onderwerp = item.Value.ToString(); notSet.Remove(keywords.header_subject); break; case nameof(keywords.header_organization): stuurinformatie.Organisatie = item.Value.ToString(); notSet.Remove(keywords.header_organization); break; case nameof(keywords.header_type): stuurinformatie.Type = item.Value.ToString(); notSet.Remove(keywords.header_type); break; case nameof(keywords.header_domain): stuurinformatie.Domein = item.Value.ToString(); notSet.Remove(keywords.header_domain); break; case nameof(keywords.header_version): stuurinformatie.Versie = item.Value.ToString(); notSet.Remove(keywords.header_version); break; case nameof(keywords.header_status): stuurinformatie.Status = item.Value.ToString(); notSet.Remove(keywords.header_status); break; case nameof(keywords.header_period): stuurinformatie.Jaar = item.Value.ToString(); notSet.Remove(keywords.header_period); break; case nameof(keywords.header_source): stuurinformatie.Bron = item.Value.ToString(); notSet.Remove(keywords.header_source); break; default: throw new FlowFormattingException($"unknown property in {keywords.header} definition: '{item.Key.ToString()}:'", new DebugInfo().MapDebugInfo(item.Key.Start, item.Key.End)); } } // check if all items are set. /* * var check = stuurinformatie.GetType().GetProperties() * .Where(pi => pi.PropertyType == typeof(string) && * string.IsNullOrEmpty((string)pi.GetValue(stuurinformatie))) * .Select(p => p.Name) */ if (notSet.Any()) { var s = string.Join(", ", notSet); throw new HeaderFormattingException($"{keywords.header} expects {s} fields to be set.", new DebugInfo().MapDebugInfo(node.ElementAt(0).Key.Start, node.ElementAt(0).Key.End)); } return(stuurinformatie); }