Skip to content

Commit

Permalink
enhance DTOs
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalatt committed Apr 17, 2019
1 parent 3bc34e1 commit 20ac2df
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
20 changes: 8 additions & 12 deletions Telega.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ static async Task DownloadFirstChannelPictureExample(TelegramClient tg)
.IfNone(() => throw new Exception("A channel is not found"));
var photo = firstChannel.Photo
.AsTag().IfNone(() => throw new Exception("The first channel does not have a photo"));
var bigPhotoFile = photo.PhotoBig
.AsTag().IfNone(() => throw new Exception("The first channel photo is unavailable"));

InputFileLocation ToInput(FileLocation.Tag location) =>
new InputFileLocation.Tag(
volumeId: location.VolumeId,
localId: location.LocalId,
secret: location.Secret,
fileReference: location.FileReference
);

var photoLoc = ToInput(bigPhotoFile);
var bigPhotoFile = photo.PhotoBig;

var photoLoc = new InputFileLocation.PeerPhotoTag(
peer: new InputPeer.ChannelTag(firstChannel.Id, firstChannel.AccessHash.AssertSome()),
volumeId: bigPhotoFile.VolumeId,
localId: bigPhotoFile.LocalId,
big: true
);
var fileType = await tg.Upload.GetFileType(photoLoc);
var fileTypeExt = fileType.Match(
pngTag: _ => ".png",
Expand Down
21 changes: 10 additions & 11 deletions Telega.Rpc.Dto.Generator/Generation/Gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static class Gen
Line("using T = Telega.Rpc.Dto.Types;")
);

static NestedText GenTypeTagBody(string tagName, Signature tag)
static NestedText GenTypeTagBody(string typeName, string tagName, Signature tag)
{
var modifiedArgs = tag.Args
.Map(x => new Arg(
Expand Down Expand Up @@ -68,6 +68,11 @@ static NestedText GenTypeTagBody(string tagName, Signature tag)
Line(""),
WithGen.GenWith(argsWithoutFlags, tagName),
Line(""),
typeName != tagName ? Scope(
Line($"public static implicit operator {typeName}({tagName} tag) => new {typeName}(tag);"),
Line($"public static implicit operator Some<{typeName}>({tagName} tag) => new {typeName}(tag);"),
Line("")
) : EmptyScope(),
RelationsGen.GenRelations(tagName, argsWithoutFlags),
Line(""),
Line(""),
Expand All @@ -77,14 +82,14 @@ static NestedText GenTypeTagBody(string tagName, Signature tag)
);
}

static NestedText GenTypeTag(Signature tag)
static NestedText GenTypeTag(string typeName, Signature tag)
{
var tagName = tag.Name;

return Scope(
Line($"public sealed class {tagName} : ITgTypeTag, IEquatable<{tagName}>, IComparable<{tagName}>, IComparable"),
Line("{"),
Indent(1, GenTypeTagBody(tagName, tag)),
Indent(1, GenTypeTagBody(typeName, tagName, tag)),
Line("}")
);
}
Expand Down Expand Up @@ -159,18 +164,13 @@ static NestedText GenFunc(Signature func, string funcName)

static NestedText GenTypeWithManyTags(string typeName, Arr<Signature> typeTags)
{
var tagsDefs = typeTags.Map(GenTypeTag).Scope(Environment.NewLine + Environment.NewLine);
var tagsDefs = typeTags.Map(x => GenTypeTag(typeName, x)).Scope(Environment.NewLine + Environment.NewLine);

var tagDef = Scope(
Line("readonly ITgTypeTag _tag;"),
Line($"{typeName}(ITgTypeTag tag) => _tag = tag ?? throw new ArgumentNullException(nameof(tag));")
);

var tagCreateDef = typeTags.Map(tag =>
Line($"public static implicit operator {typeName}({tag.Name} tag) => new {typeName}(tag);")
).Scope();


var serializeRef = Scope(
Line("void ITgSerializable.Serialize(BinaryWriter bw)"),
Line("{"),
Expand Down Expand Up @@ -287,7 +287,6 @@ static NestedText GenTypeWithManyTags(string typeName, Arr<Signature> typeTags)
var bodyDef = Scope(Environment.NewLine + Environment.NewLine,
tagsDefs,
tagDef,
tagCreateDef,
serializeRef,
staticTryDeserializeDef,
staticDeserializeDef,
Expand All @@ -314,7 +313,7 @@ static NestedText GenTypeWithManyTags(string typeName, Arr<Signature> typeTags)

static NestedText GenTypeWithOneTag(string typeName, Signature tag)
{
var tagBody = GenTypeTagBody(typeName, tag);
var tagBody = GenTypeTagBody(typeName, typeName, tag);

var serializeRef = Scope(
Line("void ITgSerializable.Serialize(BinaryWriter bw)"),
Expand Down
2 changes: 2 additions & 0 deletions Telega.Rpc.Dto.Generator/TextModel/NestedTextAbbreviations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using LanguageExt;
using static LanguageExt.Prelude;

namespace Telega.Rpc.Dto.Generator.TextModel
Expand All @@ -9,6 +10,7 @@ static class NestedTextAbbreviations
public static NestedText Line(Text text) => NestedText.CreateLine(text);
public static NestedText Line(string s) => Line((Text) s);
public static NestedText Indent(int offset, NestedText text) => NestedText.CreateIndent(offset, text);
public static NestedText EmptyScope() => NestedText.CreateScope(Arr<NestedText>.Empty, (Text) Environment.NewLine);
public static NestedText Scope(this IEnumerable<NestedText> xs, Text separator = null) => NestedText.CreateScope(xs.ToArr(), separator ?? Environment.NewLine);
public static NestedText Scope(Text separator, params NestedText[] xs) => xs.Scope(separator);
public static NestedText Scope(Text separator, params IEnumerable<NestedText>[] xs) => xs.Bind(identity).Scope(separator);
Expand Down
14 changes: 7 additions & 7 deletions Telega/TelegramClientMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ await _tg.Call(new GetDialogs(
));

public async Task<Messages> GetHistory(
InputPeer peer,
Some<InputPeer> peer,
int offsetId = 0,
int offsetDate = 0,
int addOffset = 0,
Expand All @@ -46,7 +46,7 @@ await _tg.Call(new GetHistory(
hash
));

public async Task<UpdatesType> SendMessage(InputPeer peer, Some<string> message) =>
public async Task<UpdatesType> SendMessage(Some<InputPeer> peer, Some<string> message) =>
await _tg.Call(new SendMessage(
peer: peer,
message: message,
Expand All @@ -61,8 +61,8 @@ await _tg.Call(new SendMessage(
));

public async Task<UpdatesType> SendPhoto(
InputPeer peer,
InputFile file,
Some<InputPeer> peer,
Some<InputFile> file,
Some<string> message
) =>
await _tg.Call(new SendMedia(
Expand All @@ -79,8 +79,8 @@ await _tg.Call(new SendMedia(
));

public async Task<UpdatesType> SendDocument(
InputPeer peer,
InputFile file,
Some<InputPeer> peer,
Some<InputFile> file,
Some<string> mimeType,
Arr<DocumentAttribute> attributes,
Some<string> message
Expand All @@ -106,7 +106,7 @@ await _tg.Call(new SendMedia(
message: message
));

public async Task<bool> SendTyping(InputPeer peer) =>
public async Task<bool> SendTyping(Some<InputPeer> peer) =>
await _tg.Call(new SetTyping(
action: new SendMessageAction.TypingTag(),
peer: peer
Expand Down

0 comments on commit 20ac2df

Please sign in to comment.