Beispiel #1
0
        private static string GenerateType(TypeDescription type, string content, Bundle bundle)
        {
            return($@"// Generated from {bundle.TypeToFile[type.QualifiedName].CanonicalPath}({type.SourceReference.Line},{type.SourceReference.Column})
public readonly struct {type.TypeName()} : global::System.IEquatable<{type.TypeName()}>
{{
{Indent(1, content)}
}}");
        }
        public string Generate(TypeDescription type)
        {
            var typeName = type.TypeName();

            var content      = new StringBuilder();
            var commandTypes = bundle.CommandTypes;

            content.AppendLine(GenerateSchemaConstructor(type, type.Fields).TrimEnd());
            content.AppendLine(GenerateApplyToSchemaObject(type.Fields).TrimEnd());
            content.AppendLine(GenerateUpdaters(type.Fields).TrimEnd());
            content.AppendLine(GenerateConstructor(type, type.Fields).TrimEnd());

            if (type.ComponentId.HasValue)
            {
                content.AppendLine(GenerateFromUpdate(type).TrimEnd());
                content.AppendLine(GenerateCreateGetEvents(type.Events).TrimEnd());

                if (!type.IsRestricted)
                {
                    // Workers can't construct or send updates for restricted components.
                    content.AppendLine(GenerateUpdateStruct(type, type.Fields).TrimEnd());
                }
            }

            if (commandTypes.Contains(type.QualifiedName))
            {
                content.AppendLine($@"public static {typeName} Create(global::Improbable.Worker.CInterop.SchemaCommandRequest? fields)
{{
    return fields.HasValue ? new {typeName}(fields.Value.GetObject()) : new {typeName}();
}}");
            }

            return(content.ToString());
        }
        private static string GenerateSchemaConstructor(TypeDescription type, IEnumerable <FieldDefinition> fields)
        {
            var typeName = type.TypeName();

            var text = new StringBuilder();
            var sb   = new StringBuilder();

            foreach (var field in fields)
            {
                var output = GetAssignmentForField(type, field, field.PascalCase());
                sb.AppendLine(output);
            }

            text.AppendLine($@"
internal {typeName}(global::Improbable.Worker.CInterop.SchemaObject fields)
{{
{Indent(1, sb.ToString().TrimEnd())}
}}");

            return(text.ToString());
        }