public void TestArrayEnum() { var ae = new ArrayEnumerator <int>(new int[] { 1, 2, 3 }); ae.MoveNext(); Assert.AreEqual(1, ae.Current); ae.MoveNext(); Assert.AreEqual(2, ae.Current); ae.MoveNext(); Assert.AreEqual(3, ae.Current); }
public void Enumerate() { var counter = 0; while (_enumerator.MoveNext()) { _array[counter++].Should().Be(_enumerator.Current); } counter.Should().Be(_array.Length); }
public void TestArrayEnumerator__StartIndex__Length() { string[] arr = { "abc", "def", "ghi", "jkl" }; IEnumerator <string> enumerator = new ArrayEnumerator <string>(arr, 1, 2); Assert.True(enumerator.MoveNext()); Assert.Equal("def", enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal("ghi", enumerator.Current); Assert.False(enumerator.MoveNext()); }
public void TestArrayEnumerator() { string[] arr = { "abc", "def", "ghi" }; IEnumerator <string> enumerator = new ArrayEnumerator <string>(arr); Assert.True(enumerator.MoveNext()); Assert.Equal("abc", enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal("def", enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal("ghi", enumerator.Current); Assert.False(enumerator.MoveNext()); }
public static new AzureResource FromJsonElement(JsonElement element) { KubernetesCluster resource = new KubernetesCluster(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); resource.SkuTier = element.GetProperty("sku").GetProperty("tier").GetString(); JsonElement properties = element.GetProperty("properties"); resource.KubernetesVersion = properties.GetProperty("kubernetesVersion").GetString(); resource.DnsPrefix = properties.GetProperty("dnsPrefix").GetString(); resource.FQDN = properties.GetProperty("fqdn").GetString(); ArrayEnumerator e = properties.GetProperty("agentPoolProfiles").EnumerateArray(); if (e.MoveNext()) { resource.DefaultNodeProfile.FromJsonElement(e.Current); } return(resource); }
public void TestArrayEnumerator__Empty() { string[] arr = new string[0]; IEnumerator <string> enumerator = new ArrayEnumerator <string>(arr); Assert.False(enumerator.MoveNext()); }
public static new AzureResource FromJsonElement(JsonElement element) { NetworkProfile resource = new NetworkProfile(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); JsonElement properties = element.GetProperty("properties"); ArrayEnumerator en = properties.GetProperty("containerNetworkInterfaceConfigurations").EnumerateArray(); if (en.MoveNext()) { resource.ContainerNetworkInterfaceName = en.Current.GetProperty("name").GetString(); properties = en.Current.GetProperty("properties"); ArrayEnumerator e = properties.GetProperty("ipConfigurations").EnumerateArray(); while (e.MoveNext()) { string name = e.Current.GetProperty("name").GetString(); string subnetId = e.Current.GetProperty("properties").GetProperty("subnet").GetProperty("id").GetString(); string subnetRef = resource.GetSubnetReference(subnetId); resource.IpConfigurations.Add(name, subnetRef); } } return(resource); }
public static new AzureResource FromJsonElement(JsonElement element) { PrivateEndpoint resource = new PrivateEndpoint(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); string subnetId = element.GetProperty("properties").GetProperty("subnet").GetProperty("id").GetString(); resource.SubnetId = resource.GetSubnetReference(subnetId); ArrayEnumerator e = element.GetProperty("properties").GetProperty("privateLinkServiceConnections").EnumerateArray(); while (e.MoveNext()) { resource.ServiceConnectionName = e.Current.GetProperty("name").GetString(); resource.PrivateLinkServiceId = e.Current.GetProperty("properties").GetProperty("privateLinkServiceId").GetString(); ArrayEnumerator e2 = e.Current.GetProperty("properties").GetProperty("groupIds").EnumerateArray(); while (e2.MoveNext()) { resource.GroupIds.Add(e2.Current.GetString()); } } return(resource); }
public static new AzureResource FromJsonElement(JsonElement element) { DnsZoneTXT resource = new DnsZoneTXT(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); JsonElement properties = element.GetProperty("properties"); // ZoneName will get set outside this method because it's not included in the record resource resource.TTL = properties.GetProperty("TTL").GetInt32(); ArrayEnumerator TXTenum = properties.GetProperty("TXTRecords").EnumerateArray(); while (TXTenum.MoveNext()) { ArrayEnumerator valueEnum = TXTenum.Current.GetProperty("value").EnumerateArray(); while (valueEnum.MoveNext()) { resource.TXT.Add(valueEnum.Current.GetString()); } } return(resource); }
public static new AzureResource FromJsonElement(JsonElement element) { RouteTable resource = new RouteTable(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); JsonElement properties = element.GetProperty("properties"); resource.DisableBGPRoutePropagation = properties.GetProperty("disableBgpRoutePropagation").GetBoolean(); ArrayEnumerator e = properties.GetProperty("routes").EnumerateArray(); while (e.MoveNext()) { Route route = Route.FromJsonElement(e.Current) as Route; resource.Routes.Add(route); } return(resource); }
public static new AzureResource FromJsonElement(JsonElement element) { KeyVaultAccessPolicy resource = new KeyVaultAccessPolicy(); resource.Description = element; // basic information // resource.ID = element.GetProperty("id").GetString(); // resource.Name = element.GetProperty("name").GetString(); // resource.Type = element.GetProperty("type").GetString(); // resource.Location = element.GetProperty("location").GetString(); resource.TenantID = element.GetProperty("tenantId").GetString(); resource.ObjectID = element.GetProperty("objectId").GetString(); JsonElement permissions = element.GetProperty("permissions"); ArrayEnumerator e = permissions.GetProperty("keys").EnumerateArray(); while (e.MoveNext()) { resource.KeyPermissions.Add(e.Current.GetString()); } e = permissions.GetProperty("secrets").EnumerateArray(); while (e.MoveNext()) { resource.SecretPermissions.Add(e.Current.GetString()); } e = permissions.GetProperty("certificates").EnumerateArray(); while (e.MoveNext()) { resource.CertificatePermissions.Add(e.Current.GetString()); } return(resource); }
public void FromJsonElement(JsonElement element) { Name = element.GetProperty("name").GetString(); VmSize = element.GetProperty("vmSize").GetString(); ArrayEnumerator e = element.GetProperty("availabilityZones").EnumerateArray(); while (e.MoveNext()) { AvailabilityZones.Add(e.Current.GetString()); } MaxPods = element.GetProperty("maxPods").GetInt32(); Type = element.GetProperty("type").GetString(); }
public static void fillMovieList() { string jsonText = File.ReadAllText("movies.json"); using (JsonDocument document = JsonDocument.Parse(jsonText)) { JsonElement root = document.RootElement; JsonElement moviesList = root; foreach (JsonElement movie in moviesList.EnumerateArray()) { if (movie.TryGetProperty("movieID", out JsonElement movieIDElement) && movie.TryGetProperty("MovieName", out JsonElement MovieNameElement) && movie.TryGetProperty("startTime", out JsonElement startTimeElement) && movie.TryGetProperty("runTime", out JsonElement runTimeElement) && movie.TryGetProperty("genre", out JsonElement genreElement) && movie.TryGetProperty("director", out JsonElement directorElement) && movie.TryGetProperty("screenNumber", out JsonElement screenNumberElement) && movie.TryGetProperty("movieType", out JsonElement movieTypeElement) && movie.TryGetProperty("Synopsis", out JsonElement SynopsisElement) && movie.TryGetProperty("ScreenRows", out JsonElement ScreenRowsElement)) { int movieID = movieIDElement.GetInt32(); string MovieName = MovieNameElement.GetString(); DateTime startTime = startTimeElement.GetDateTime(); int runTime = runTimeElement.GetInt32(); string genre = genreElement.GetString(); string director = directorElement.GetString(); int screenNumber = screenNumberElement.GetInt32(); string movieType = movieTypeElement.GetString(); string Synopsis = SynopsisElement.GetString(); List <string> screenRows = new List <string>(); ArrayEnumerator arrayEnum = ScreenRowsElement.EnumerateArray(); while (arrayEnum.MoveNext()) { screenRows.Add(arrayEnum.Current.GetString()); } Movies fillMovies = new Movies(movieID, MovieName, startTime, screenNumber, runTime, genre, director, movieType, Synopsis); fillMovies.ScreenRows = screenRows; Program.movies.Add(fillMovies); } } } }
public static new AzureResource FromJsonElement(JsonElement element) { NetworkInterface resource = new NetworkInterface(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); ArrayEnumerator e = element.GetProperty("properties").GetProperty("ipConfigurations").EnumerateArray(); while (e.MoveNext()) { IpConfiguration config = IpConfiguration.FromJsonElement(e.Current) as IpConfiguration; resource.IpConfigs.Add(config); } return(resource); }
public static new AzureResource FromJsonElement(JsonElement element) { NetworkSecurityGroup resource = new NetworkSecurityGroup(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); JsonElement properties = element.GetProperty("properties"); ArrayEnumerator e = properties.GetProperty("securityRules").EnumerateArray(); while (e.MoveNext()) { NetworkSecurityRule rule = NetworkSecurityRule.FromJsonElement(e.Current) as NetworkSecurityRule; resource.Rules.Add(rule); } JsonElement subnets; if (properties.TryGetProperty("subnets", out subnets)) { ArrayEnumerator subnetEnum = subnets.EnumerateArray(); while (subnetEnum.MoveNext()) { string subnetId = subnetEnum.Current.GetProperty("id").GetString(); // I only want the name, strip the rest string[] parts = subnetId.Split('/'); resource.Subnets.Add(parts[parts.Length - 1]); } } return(resource); }
public static new AzureResource FromJsonElement(JsonElement element) { VirtualNetwork resource = new VirtualNetwork(); resource.Description = element; // basic information resource.ID = element.GetProperty("id").GetString(); resource.Name = element.GetProperty("name").GetString(); resource.Type = element.GetProperty("type").GetString(); resource.Location = element.GetProperty("location").GetString(); // collect resource specific information JsonElement properties = element.GetProperty("properties"); ArrayEnumerator addressPrefixes = properties.GetProperty("addressSpace").GetProperty("addressPrefixes").EnumerateArray(); while (addressPrefixes.MoveNext()) { resource.AddressPrefixes.Add(addressPrefixes.Current.GetString()); } return(resource); }
protected sealed override Object EvaluateCore( EvaluationContext context, out ResultMemory resultMemory) { resultMemory = null; var result = new StringBuilder(); var memory = new MemoryCounter(this, context.Options.MaxMemory); var current = Parameters[0].Evaluate(context); var ancestors = new Stack <ICollectionEnumerator>(); do { // Descend as much as possible while (true) { // Collection if (current.TryGetCollectionInterface(out Object collection)) { // Array if (collection is IReadOnlyArray array) { if (array.Count > 0) { // Write array start WriteArrayStart(result, memory, ancestors); // Move to first item var enumerator = new ArrayEnumerator(context, current, array); enumerator.MoveNext(); ancestors.Push(enumerator); current = enumerator.Current; } else { // Write empty array WriteEmptyArray(result, memory, ancestors); break; } } // Mapping else if (collection is IReadOnlyObject obj) { if (obj.Count > 0) { // Write mapping start WriteMappingStart(result, memory, ancestors); // Move to first pair var enumerator = new ObjectEnumerator(context, current, obj); enumerator.MoveNext(); ancestors.Push(enumerator); // Write mapping key WriteMappingKey(context, result, memory, enumerator.Current.Key, ancestors); // Move to mapping value current = enumerator.Current.Value; } else { // Write empty mapping WriteEmptyMapping(result, memory, ancestors); break; } } else { throw new NotSupportedException($"Unexpected type '{collection?.GetType().FullName}'"); } } // Not a collection else { // Write value WriteValue(context, result, memory, current, ancestors); break; } } // Next sibling or ancestor sibling do { if (ancestors.Count > 0) { var parent = ancestors.Peek(); // Parent array if (parent is ArrayEnumerator arrayEnumerator) { // Move to next item if (arrayEnumerator.MoveNext()) { current = arrayEnumerator.Current; break; } // Move to parent else { ancestors.Pop(); current = arrayEnumerator.Array; // Write array end WriteArrayEnd(result, memory, ancestors); } } // Parent mapping else if (parent is ObjectEnumerator objectEnumerator) { // Move to next pair if (objectEnumerator.MoveNext()) { // Write mapping key WriteMappingKey(context, result, memory, objectEnumerator.Current.Key, ancestors); // Move to mapping value current = objectEnumerator.Current.Value; break; } // Move to parent else { ancestors.Pop(); current = objectEnumerator.Object; // Write mapping end WriteMappingEnd(result, memory, ancestors); } } else { throw new NotSupportedException($"Unexpected type '{parent?.GetType().FullName}'"); } } else { current = null; } } while (current != null); } while (current != null); return(result.ToString()); }
public override void Patch(Dictionary <string, JsonElement> Data) { //if (!Partial) return; Console.WriteLine($"Patching guild data for {this.Id}"); JsonElement temp; long tempL; #region String values Name = Data["name"].GetString(); Icon = Data["icon"].GetString(); Splash = Data["splash"].GetString(); DiscoverySplash = Data["discovery_splash"].ToString(); Region = Data["region"].ToString(); ArrayEnumerator _Features = Data["features"].EnumerateArray(); Features = new string[Data["features"].GetArrayLength()]; for (int i = 0; i < Features.Length; i++) { _Features.MoveNext(); Features[i] = _Features.Current.GetString(); } #endregion String values #region Integer values AfkTimeout = Data["afk_timeout"].GetInt32(); VerificationLevel = Data["verification_level"].GetInt32(); DefaultNotifications = Data["default_message_notifications"].GetInt32(); ExplicitContentFilter = Data["explicit_content_filter"].GetInt32(); MfaLevel = Data["mfa_level"].GetInt32(); if (Data.TryGetValue("member_count", out temp)) { MemberCount = temp.GetInt32(); } if (Data.TryGetValue("max_presences", out temp)) { MaxPresences = temp.GetInt32(); } if (Data.TryGetValue("max_members", out temp)) { MaxMembers = temp.GetInt32(); } #endregion Integer values #region Long values OwnerId = long.Parse(Data["owner_id"].GetString()); if (Data.TryGetValue("afk_channel_id", out temp) && long.TryParse(temp.GetString(), out tempL)) { AfkChannelId = tempL; } if (Data.TryGetValue("application_id", out temp) && long.TryParse(temp.GetString(), out tempL)) { ApplicationId = tempL; } if (long.TryParse(Data["system_channel_id"].GetString(), out tempL)) { SystemChannelId = tempL; } if (long.TryParse(Data["rules_channel_id"].GetString(), out tempL)) { RulesChannelId = tempL; } if (Data.TryGetValue("widget_channel_id", out temp) && long.TryParse(temp.GetString(), out tempL)) { WidgetChannelId = tempL; } #endregion Long values #region Bool values if (Data.TryGetValue("widget_enabled", out temp)) { WidgetEnabled = temp.GetBoolean(); } if (Data.TryGetValue("large", out temp)) { Large = temp.GetBoolean(); } if (Data.TryGetValue("unavailable", out temp)) { Unavailable = temp.GetBoolean(); } #endregion Bool values if (Data.TryGetValue("joined_at", out temp)) { JoinedAt = DateTime.Parse(temp.GetString()); } if (_Patched) { return; } // TODO: Create role objects Roles = new GuildRoles(this.Client, this); ArrayEnumerator _Roles = Data["roles"].EnumerateArray(); foreach (JsonElement Role in _Roles) { Roles.CreateEntry(Role); } // TODO: Create emoji objects Emojis = new GuildEmojis(this.Client, this); ArrayEnumerator _Emojis = Data["emojis"].EnumerateArray(); foreach (JsonElement Role in _Roles) { Emojis.CreateEntry(Role); } // TODO: AFK channel // TODO: System channel // TODO: Rules channel // TODO: Channels Channels = new GuildChannels(this.Client, this); ArrayEnumerator _Channels = Data["channels"].EnumerateArray(); foreach (JsonElement Channel in _Channels) { Channels.CreateEntry(Channel); } // TODO: Members Members = new GuildMembers(Client, this); ArrayEnumerator _Members = Data["members"].EnumerateArray(); foreach (JsonElement Member in _Members) { //Console.WriteLine(Member); Members.CreateEntry(Member); } // TODO: Presences // TODO: Voice states _Patched = true; }
static async Task Main(string[] args) { XmlDocument doc = new XmlDocument(); Console.WriteLine(args[0]); FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read); doc.Load(fs); XmlNodeList nodes = doc.GetElementsByTagName("PackageReference"); for (int i = 0; i < nodes.Count; i++) { string packageName = nodes[i].Attributes.GetNamedItem("Include").Value; Console.WriteLine(packageName); Version packageVersion; if (nodes[i].Attributes.GetNamedItem("Version") != null) { Version.TryParse(nodes[i].Attributes.GetNamedItem("Version").Value, out packageVersion); List <Version> allTheVersions = new List <Version>(); HttpClient client = new HttpClient(); HttpResponseMessage message = await client.GetAsync($"https://api-v2v3search-0.nuget.org/search/query?q=packageid:{packageName.ToLowerInvariant()}&ignoreFilter=true"); string json = await message.Content.ReadAsStringAsync(); JsonDocument packageDoc = await System.Text.Json.JsonDocument.ParseAsync(await message.Content.ReadAsStreamAsync()); JsonElement root = packageDoc.RootElement; ObjectEnumerator oe = root.EnumerateObject(); while (oe.MoveNext()) { if (oe.Current.Name == "data") { ArrayEnumerator ae = oe.Current.Value.EnumerateArray(); while (ae.MoveNext()) { ObjectEnumerator re = ae.Current.EnumerateObject(); while (re.MoveNext()) { if (re.Current.Name == "Version") { Version v; if (Version.TryParse(re.Current.Value.GetString(), out v)) { allTheVersions.Add(v); } } } } var newerVersions = allTheVersions.Where(ver => ver.CompareTo(packageVersion) > 0); foreach (Version newerVersion in newerVersions) { Console.WriteLine($"{packageName} - {newerVersion}"); } } } } } }
public async Task <List <AzureResource> > LoadResource(AzureResource stub) { // using a list because in special cases we might load subresources too List <AzureResource> resources = new List <AzureResource>(); AzureResource resource = null; try { Assembly a = Assembly.GetExecutingAssembly(); List <Type> types = a.GetTypes().Where(t => t.IsClass && t.BaseType == typeof(AzureResource)).ToList(); foreach (Type t in types) { FieldInfo fi = t.GetField("AzureType"); if (fi != null && fi.GetValue(null).ToString() == stub.Type) { // generic method of loading a single resource string apiVersion = t.GetField("ApiVersion").GetValue(null).ToString(); JsonDocument result = await CallARM($"https://management.azure.com{stub.ID}?api-version={apiVersion}"); MethodInfo method = t.GetMethod("FromJsonElement"); if (method != null) { resource = (AzureResource)method.Invoke(null, new object[] { result.RootElement }); resource.ResourceGroupName = stub.ResourceGroupName; resources.Add(resource); } // in special cases we want to load subresources, such as subnets in a virtual network that we want to create as separate objects if (stub.Type == VirtualNetwork.AzureType) { ArrayEnumerator subnetEnum = result.RootElement.GetProperty("properties").GetProperty("subnets").EnumerateArray(); while (subnetEnum.MoveNext()) { Subnet subnet = Subnet.FromJsonElement(subnetEnum.Current) as Subnet; subnet.VirtualNetworkName = stub.Name; resources.Add(subnet); } } if (stub.Type == NetworkSecurityGroup.AzureType) { // if subnet is defined we need to add an association because inline subnet blocks don't support delegation or service endpoints NetworkSecurityGroup nsg = resource as NetworkSecurityGroup; foreach (string subnet in nsg.Subnets) { SubnetNetworkSecurityGroupAssociation assoc = new SubnetNetworkSecurityGroupAssociation(); assoc.SubnetName = subnet; assoc.NetworkSecurityGroupName = nsg.Name; resources.Add(assoc); } } if (stub.Type == DnsZone.AzureType) { // DNS zones don't contain records so we need to add each one as a separate resource JsonDocument records = await CallARM($"https://management.azure.com{stub.ID}/all?api-version={apiVersion}"); ArrayEnumerator recordEnum = records.RootElement.GetProperty("value").EnumerateArray(); while (recordEnum.MoveNext()) { // this could be a recursive call, is it better to load the object twice or to duplicate this code? :S string type = recordEnum.Current.GetProperty("type").GetString(); if (type == "Microsoft.Network/dnszones/CNAME") { var record = DnsZoneCNAME.FromJsonElement(recordEnum.Current) as DnsZoneCNAME; record.Location = stub.Location; record.ZoneName = stub.Name; resources.Add(record); } if (type == "Microsoft.Network/dnszones/TXT") { var record = DnsZoneTXT.FromJsonElement(recordEnum.Current) as DnsZoneTXT; record.Location = stub.Location; record.ZoneName = stub.Name; resources.Add(record); } if (type == "Microsoft.Network/dnszones/A") { var record = DnsZoneA.FromJsonElement(recordEnum.Current) as DnsZoneA; record.Location = stub.Location; record.ZoneName = stub.Name; resources.Add(record); } } } if (stub.Type == KeyVault.AzureType) { // enumerate the access policies and create separately - inline blocks only support 16 access policies ArrayEnumerator policyEnum = result.RootElement.GetProperty("properties").GetProperty("accessPolicies").EnumerateArray(); while (policyEnum.MoveNext()) { var policy = KeyVaultAccessPolicy.FromJsonElement(policyEnum.Current) as KeyVaultAccessPolicy; policy.KeyVaultName = stub.Name; resources.Add(policy); } } string filename = $"../{stub.Type.Replace('/', '_')}_{stub.Name}.json"; if (File.Exists(filename) == false) { string s = result.RootElement.ToString(); File.WriteAllText(filename, s); } } } if (resource == null) { resources.Add(stub); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(resources); }