-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Codegen] Allow any nullable double fields to be strings or doubles a…
…nd improved error reporting (#984) * Allow any nullable double fields to be strings or doubles and improved error reporting * small fix
- Loading branch information
1 parent
610ca13
commit f8080a9
Showing
7 changed files
with
199 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 37 additions & 27 deletions
64
...odel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/ServiceMetaDataParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...el/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace NetDaemon.HassModel.CodeGenerator.Model; | ||
|
||
class StringAsDoubleConverter : JsonConverter<double?> | ||
{ | ||
public override double? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// Some fields (step) can have a string or a numeric value. If it is a string we will try to parse it to a decimal | ||
return reader.TokenType switch | ||
{ | ||
JsonTokenType.Number => reader.GetDouble(), | ||
JsonTokenType.String => double.TryParse(reader.GetString(), out var d) ? d : null, | ||
_ => Skip(ref reader) | ||
}; | ||
} | ||
|
||
double? Skip(ref Utf8JsonReader reader) | ||
{ | ||
reader.Skip(); | ||
return null; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, double? value, JsonSerializerOptions options) => throw new NotSupportedException(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters