Skip to content

Commit

Permalink
Replace Utf8Json with System.Text.Json (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolfik1 authored Feb 20, 2024
1 parent 5919f2e commit 91fd8bd
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 294 deletions.
6 changes: 3 additions & 3 deletions src/Funogram.Generator/Methods/MethodsGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let generateMakeMethodSignature apiType (fields: ApiTypeField[]) convertFn code
fields
|> Seq.fold (fun code tp ->
let o = if tp.IsOptional then "?" else ""
let c = if fields.[0] <> tp then ", " else ""
let c = if fields[0] <> tp then ", " else ""

let argName = Helpers.toCamelCase tp.OriginalName |> Helpers.fixReservedKeywords
let argType = convertFn tp
Expand All @@ -89,7 +89,7 @@ let generateMakeMethodInvocation apiType (fields: ApiTypeField[]) convertFn code

fields
|> Seq.fold (fun code tp ->
let c = if fields.[0] <> tp then ", " else ""
let c = if fields[0] <> tp then ", " else ""

let argNameOriginal = Helpers.toCamelCase tp.OriginalName |> Helpers.fixReservedKeywords
let argName = convertFn tp argNameOriginal
Expand All @@ -105,7 +105,7 @@ let generateMakeMethodInvocation apiType (fields: ApiTypeField[]) convertFn code
|> Code.print ")"

let generateMakeMethodOverloads apiType (fields: ApiTypeField[]) code =
if fields.Length = 0 || (fields.[0].ConvertedFieldType <> "ChatId") then
if fields.Length = 0 || (fields[0].ConvertedFieldType <> "ChatId") then
code
else
let convertFieldSignatureType replaceType (tp: ApiTypeField) =
Expand Down
4 changes: 2 additions & 2 deletions src/Funogram.Generator/Methods/MethodsParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ let private isMethodSection (node: HtmlNode) =
if node.Name() = "h4" then
let text = Helpers.directInnerText node
let onlyLetters = text |> Seq.forall Char.IsLetter
onlyLetters && text.Length > 0 && Char.IsLower text.[0]
onlyLetters && text.Length > 0 && Char.IsLower text[0]
else
false

let inline private tryFindField (elements: HtmlNode list) (m: Map<string, int>) name =
m |> Map.tryFind name |> Option.map (fun i -> Helpers.innerText elements.[i])
m |> Map.tryFind name |> Option.map (fun i -> Helpers.innerText elements[i])

let inline private defaultFieldValue v =
match v with
Expand Down
10 changes: 5 additions & 5 deletions src/Funogram.Generator/Types/TypesParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let private splitCaseNameAndType (typeName: string) (nameAndType: string) =

let private isValidTypeNode (typeNodeInfo: ApiTypeNodeInfo) =
let name = Helpers.innerText typeNodeInfo.TypeName
Char.IsUpper name.[0] && (name.Replace(" ", "").Length = name.Length)
Char.IsUpper name[0] && (name.Replace(" ", "").Length = name.Length)

let private setConvertedFieldType (field: ApiTypeField) =
{ field with ConvertedFieldType = Helpers.convertTLTypeToFSharpType field.OriginalFieldType field.Description false }
Expand All @@ -66,14 +66,14 @@ let private parseApiTypeFields apiTypeName (node: HtmlNode) =
|> Seq.map (fun n -> n.Elements())
|> Seq.filter (fun e -> e.Length = 3)
|> Seq.map (fun elements ->
let desc = Helpers.innerText elements.[2]
let desc = Helpers.innerText elements[2]
let optionalIndex = desc.IndexOf("Optional. ")
let trimmedDesc = if optionalIndex >= 0 then desc.Substring(10) else desc
{
OriginalName = Helpers.innerText elements.[0]
ConvertedName = elements.[0] |> Helpers.innerText |> Helpers.toPascalCase
OriginalName = Helpers.innerText elements[0]
ConvertedName = elements[0] |> Helpers.innerText |> Helpers.toPascalCase
Description = trimmedDesc
OriginalFieldType = Helpers.innerText elements.[1]
OriginalFieldType = Helpers.innerText elements[1]
ConvertedFieldType = ""
Optional = Some (optionalIndex >= 0)
} |> setConvertedFieldType
Expand Down
8 changes: 4 additions & 4 deletions src/Funogram.Telegram/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ let inline private isAllowedChar (c: char) = Char.IsLetter c || Char.IsDigit c |
// returns -1 if the command is not valid otherwise index of last character
let private validateCommand (text: string) =
let rec iter (text: string) i len =
if i >= len || isAllowedChar text.[i] |> not then
if i >= len || isAllowedChar text[i] |> not then
(i - 1)
else
iter text (i + 1) len

if text.Length <= 1 || text.[0] <> '/' then -1
if text.Length <= 1 || text[0] <> '/' then -1
else iter text 1 text.Length

let getTextForCommand (me: User) (textOriginal: string option) =
match me.Username, textOriginal with
| Some username, Some text when text.Length > 0 && text.[0] = '/' ->
| Some username, Some text when text.Length > 0 && text[0] = '/' ->
match validateCommand text with
| -1 -> textOriginal
| idx when text.Length = idx + 1 -> Some text
| idx when text.[idx + 1] = '@' && text.IndexOf(username, idx + 1) = idx + 2 ->
| idx when text[idx + 1] = '@' && text.IndexOf(username, idx + 1) = idx + 2 ->
text.Remove(idx + 1, username.Length + 1) |> Some
| _ -> textOriginal
| _ -> textOriginal
Expand Down
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.0.0.2</Version>
<Version>7.0.0.4</Version>
<Authors>Nikolay Matyushin</Authors>
<Product>Funogram.Telegram</Product>
<Title>Funogram.Telegram</Title>
Expand Down
10 changes: 5 additions & 5 deletions src/Funogram.Telegram/Sscanf.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ let sscanf (pf:PrintfFormat<_,_,_,_,'t>) s : 't =

let matches =
(groups, formatters)
||> Seq.map2 (fun g f -> g.Value |> parsers.[f])
||> Seq.map2 (fun g f -> g.Value |> parsers[f])
|> Seq.toArray

if matches.Length = 1 then
coerce matches.[0] typeof<'t> :?> 't
coerce matches[0] typeof<'t> :?> 't
else
let tupleTypes = FSharpType.GetTupleElements(typeof<'t>)
let matches =
(matches,tupleTypes)
||> Array.map2 ( fun a b -> coerce a b)
||> Array.map2 coerce
FSharpValue.MakeTuple(matches, typeof<'t>) :?> 't


Expand Down Expand Up @@ -123,11 +123,11 @@ let sscanfci (pf:PrintfFormat<_,_,_,_,'t>) s : 't =

let matches =
(groups, formatters)
||> Seq.map2 (fun g f -> g.Value |> parsers.[f])
||> Seq.map2 (fun g f -> g.Value |> parsers[f])
|> Seq.toArray

if matches.Length = 1 then
coerce matches.[0] typeof<'t> :?> 't
coerce matches[0] typeof<'t> :?> 't
else
let tupleTypes = FSharpType.GetTupleElements(typeof<'t>)
let matches = (matches, tupleTypes) ||> Array.map2 ( fun a b -> coerce a b)
Expand Down
6 changes: 3 additions & 3 deletions src/Funogram.Tests/Constants.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open Funogram.Types
module Constants =
let private ok = sprintf """{"ok":true,"result":%s}"""

let testDate = System.DateTime(2117, 05, 28, 12, 47, 51, DateTimeKind.Utc)
let testDate = DateTime(2117, 05, 28, 12, 47, 51, DateTimeKind.Utc)
let testDateUnix = 4651649271L
let testForwardOrigin = MessageOrigin.HiddenUser(
MessageOriginHiddenUser.Create(
Expand Down Expand Up @@ -39,8 +39,8 @@ module Constants =
let jsonTestEditResult3ApiString = """{"ok":true,"result":{"message_id":123,"from":{"id":321,"is_bot":true,"first_name":"FSharpBot","username":"FSharpBot"},"chat":{"id":123,"first_name":"Test","last_name":"Test","username":"test","type":"private"},"date":4651649271,"edit_date":4651649271,"text":"Updated"}}"""


let testMaskPosition = { MaskPosition.Point = MaskPoint.Eyes; XShift = 0.0; YShift = 0.0; Scale = 0.0 }
let jsonTestMaskPosition = """{"point":"eyes","x_shift":0,"y_shift":0,"scale":0}"""
let testMaskPosition = { MaskPosition.Point = MaskPoint.Eyes; XShift = 1.0; YShift = 2.0; Scale = 3.0 }
let jsonTestMaskPosition = """{"point":"eyes","x_shift":1,"y_shift":2,"scale":3}"""
let jsonTestMaskPositionResult = ok jsonTestMaskPosition


Expand Down
11 changes: 7 additions & 4 deletions src/Funogram.Tests/Funogram.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
<Compile Include="Deserializer.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Funogram/Funogram.fsproj" />
<ProjectReference Include="..\Funogram.Telegram\Funogram.Telegram.fsproj" />
<ProjectReference Include="../Funogram.Telegram\Funogram.Telegram.fsproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 91fd8bd

Please sign in to comment.