Skip to content

Commit

Permalink
Add custom fields support (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolfik1 authored May 9, 2024
1 parent 28e6e84 commit 0aa2c91
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
38 changes: 38 additions & 0 deletions src/Funogram.Generator/CustomFields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"Name": "VideoChatStarted",
"Kind": {
"Case": "Fields",
"Fields": [
[
{
"OriginalName": "id",
"ConvertedName": "Id",
"Description": "Unique identifier of video chat (not present in Bot API at the moment, for internal use)",
"OriginalFieldType": "Integer",
"ConvertedFieldType": "int64",
"Optional": true
}
]
]
}
},
{
"Name": "VideoChatEnded",
"Kind": {
"Case": "Fields",
"Fields": [
[
{
"OriginalName": "id",
"ConvertedName": "Id",
"Description": "Unique identifier of video chat (not present in Bot API at the moment, for internal use)",
"OriginalFieldType": "Integer",
"ConvertedFieldType": "int64",
"Optional": true
}
]
]
}
}
]
1 change: 1 addition & 0 deletions src/Funogram.Generator/Funogram.Generator.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="Program.fs" />
<Content Include="RemapTypes.json" />
<Content Include="RemapMethods.json" />
<Content Include="CustomFields.json" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Funogram.Generator/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let processAsync (args: CliArguments list) =
|> TypesParser.withResultPath (Path.Combine(Constants.OutputDir, "types.json"))
|> TypesParser.loadRemapData "./RemapTypes.json"
|> TypesParser.parse
|> TypesParser.mergeCustomFields "./CustomFields.json"

|> TypesGenerator.mkGenerator (Path.Combine(Constants.CodeOutputDir, Constants.TypesFileName))
|> TypesGenerator.generate
Expand Down
36 changes: 35 additions & 1 deletion src/Funogram.Generator/Types/TypesParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,38 @@ let parse (config: ParseConfig) =
| None ->
()

types
types

let mergeCustomFields (customFieldsPath: string) (types: ApiType[]) =
if File.Exists customFieldsPath then
let serializerOptions = Helpers.getJsonSerializerOptions ()

use file = File.OpenRead customFieldsPath
let result =
JsonSerializer.Deserialize<ApiType[]>(file, serializerOptions)
|> Seq.map (fun x -> x.Name, x) |> Map.ofSeq

let types = [|
for tp in types do
match result |> Map.tryFind tp.Name with
| Some typeWithCustomFields ->
match tp.Kind, typeWithCustomFields.Kind with
| ApiTypeKind.Fields fields, ApiTypeKind.Fields extraFields ->
let mergedFields = Array.append fields extraFields
let mergedFieldsDistinct = mergedFields |> Array.distinctBy (_.ConvertedName)

if mergedFields.Length <> mergedFieldsDistinct.Length then
failwith $"Custom fields must be unique (type {tp.Name}).\nTelegram API fields:\n%A{fields}\nCustom fields:\n%A{extraFields}"

yield { tp with Kind = ApiTypeKind.Fields mergedFields }
| ApiTypeKind.Stub, kind ->
yield { tp with Kind = kind }
| _ ->
yield tp
| None -> yield tp
|]

types
else
printfn "WARN: Custom fields file not found at path %s" customFieldsPath
types
2 changes: 1 addition & 1 deletion src/Funogram.Telegram/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>7.3.0.0</Version>
<Version>7.3.0.1</Version>
<Authors>Nikolay Matyushin</Authors>
<Product>Funogram.Telegram</Product>
<Title>Funogram.Telegram</Title>
Expand Down
18 changes: 15 additions & 3 deletions src/Funogram.Telegram/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,19 +2058,31 @@ and [<CLIMutable>] VideoChatScheduled =
}

/// This object represents a service message about a video chat started in the chat. Currently holds no information.
and VideoChatStarted =
new() = {}
and [<CLIMutable>] VideoChatStarted =
{
/// Unique identifier of video chat (not present in Bot API at the moment, for internal use)
[<DataMember(Name = "id")>]
Id: int64 option
}
static member Create(?id: int64) =
{
Id = id
}

/// This object represents a service message about a video chat ended in the chat.
and [<CLIMutable>] VideoChatEnded =
{
/// Video chat duration in seconds
[<DataMember(Name = "duration")>]
Duration: int64
/// Unique identifier of video chat (not present in Bot API at the moment, for internal use)
[<DataMember(Name = "id")>]
Id: int64 option
}
static member Create(duration: int64) =
static member Create(duration: int64, ?id: int64) =
{
Duration = duration
Id = id
}

/// This object represents a service message about new members invited to a video chat.
Expand Down

0 comments on commit 0aa2c91

Please sign in to comment.