// Copyright 2016-2019, Pulumi Corporation
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Pulumi.Serialization;
namespace Pulumi
{
public partial class Deployment
{
internal static bool _excessiveDebugOutput = false;
///
/// walks the props object passed in,
/// awaiting all interior promises besides those for and , creating a reasonable POCO object that can be remoted over
/// to registerResource.
///
private static Task SerializeResourcePropertiesAsync(
string label, IDictionary args, bool keepResources)
{
return SerializeFilteredPropertiesAsync(
label, args, key => key != Constants.IdPropertyName && key != Constants.UrnPropertyName, keepResources);
}
private static async Task SerializeAllPropertiesAsync(
string label, IDictionary args, bool keepResources)
{
var result = await SerializeFilteredPropertiesAsync(label, args, _ => true, keepResources).ConfigureAwait(false);
return result.Serialized;
}
///
/// walks the props object passed in,
/// awaiting all interior promises for properties with keys that match the provided filter,
/// creating a reasonable POCO object that can be remoted over to registerResource.
///
private static async Task SerializeFilteredPropertiesAsync(
string label, IDictionary args, Predicate acceptKey, bool keepResources)
{
var propertyToDependentResources = ImmutableDictionary.CreateBuilder>();
var result = ImmutableDictionary.CreateBuilder();
foreach (var (key, val) in args)
{
if (acceptKey(key))
{
// We treat properties with null values as if they do not exist.
var serializer = new Serializer(_excessiveDebugOutput);
var v = await serializer.SerializeAsync($"{label}.{key}", val, keepResources).ConfigureAwait(false);
if (v != null)
{
result[key] = v;
propertyToDependentResources[key] = serializer.DependentResources;
}
}
}
return new SerializationResult(
Serializer.CreateStruct(result.ToImmutable()),
propertyToDependentResources.ToImmutable());
}
private struct SerializationResult
{
public readonly Struct Serialized;
public readonly ImmutableDictionary> PropertyToDependentResources;
public SerializationResult(
Struct result,
ImmutableDictionary> propertyToDependentResources)
{
Serialized = result;
PropertyToDependentResources = propertyToDependentResources;
}
public void Deconstruct(
out Struct serialized,
out ImmutableDictionary> propertyToDependentResources)
{
serialized = Serialized;
propertyToDependentResources = PropertyToDependentResources;
}
}
}
}