Esempio n. 1
0
        public string GenerateInterface()
        {
            var writer = new IndentWriter();

            writer.WriteLine($"namespace {_options.Namespace}");
            writer.WriteLine("{");
            using (writer.Indent())
            {
                writer.WriteLine("using System;");
                writer.WriteLine("using Astral;");
                _extensions.Iter(p => p.WriteNamespaces(writer, _schema));
                writer.WriteLine();

                writer.WriteLine(string.Format("[Schema(@\"{0}\")]", _schema.ToString().Replace("\"", "\"\"")));
                writer.WriteLine($"[Service(\"{_schema.Version}\", \"{_schema.Name}\")]");
                WriteTransports(_schema, writer);
                _extensions.Iter(p => p.WriteServiceAttributes(writer, _schema));
                writer.WriteLine($"public interface {_options.InterfaceName ?? _schema.Title}");
                writer.WriteLine("{");
                using (writer.Indent())
                {
                    foreach (var endpoint in _schema.Endpoints)
                    {
                        if (_options.Endpoints.IsSkip(endpoint.Value.Title))
                        {
                            continue;
                        }
                        WriteTransports(endpoint.Value, writer);
                        writer.WriteLine($"[Endpoint(\"{endpoint.Key}\")]");
                        _extensions.Iter(p => p.WriteEndpointAttributes(writer, endpoint.Value));

                        switch (endpoint.Value)
                        {
                        case EventEndpointSchema ees:

                            writer.WriteLine($"IEvent<{GetTypeNameByContract(ees.Event)}> {ees.Title} " + "{ get; }");
                            break;

                        case CommandEndpointSchema ces:
                            writer.WriteLine($"ICommand<{GetTypeNameByContract(ces.Command)}> {ces.Title} " + "{ get; }");
                            break;

                        case CallableEndpointSchema cls:
                            writer.WriteLine($"ICallable<{GetTypeNameByContract(cls.Request)}, {GetTypeNameByContract(cls.Response)}> {cls.Title} " + "{ get; }");
                            break;

                        default:
                            throw new InvalidOperationException($"Unknown endpoint type {endpoint.Value.GetType()}");
                        }
                    }
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");
            return(writer.ToString());
        }
Esempio n. 2
0
 private void WriteTransports(GatePartSchema gate, IndentWriter writer)
 {
     if (_schema.Transports != null)
     {
         foreach (var transport in gate.Transports)
         {
             writer.WriteLine($"[Transport(TransportType.{transport.Key}, \"{transport.Value}\")");
         }
     }
 }