Skip to content

Commit 92d4144

Browse files
committed
Merge remote-tracking branch 'origin/master' into release/v5
2 parents 74e5072 + b530090 commit 92d4144

File tree

688 files changed

+23010
-24303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

688 files changed

+23010
-24303
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ master ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ master ]
20+
schedule:
21+
- cron: '34 11 * * 2'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'csharp' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v2
42+
43+
# .NET 6 is used in CodeGen project.
44+
- uses: actions/setup-dotnet@v1
45+
with:
46+
dotnet-version: '6.0.x'
47+
48+
# Initializes the CodeQL tools for scanning.
49+
- name: Initialize CodeQL
50+
uses: github/codeql-action/init@v1
51+
with:
52+
languages: ${{ matrix.language }}
53+
# If you wish to specify custom queries, you can do so here or in a config file.
54+
# By default, queries listed here will override any specified in a config file.
55+
# Prefix the list here with "+" to use these queries and those in the config file.
56+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
57+
58+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
59+
# If this step fails, then you should remove it and run the build manually (see below)
60+
- name: Autobuild
61+
uses: github/codeql-action/autobuild@v1
62+
63+
# ℹ️ Command-line programs to run using the OS shell.
64+
# 📚 https://git.io/JvXDl
65+
66+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
67+
# and modify them (or add more) to build your code if your project
68+
# uses a compiled language
69+
70+
#- run: |
71+
# make bootstrap
72+
# make release
73+
74+
- name: Perform CodeQL Analysis
75+
uses: github/codeql-action/analyze@v1

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ For a fairly complete summary of the unit definition JSON schema, see [Meter of
2727
### Conversion functions
2828
Converting from unit A to B is achieved by first converting from unit A to the base unit, then from the base unit to unit B. To achieve this, each unit defines two conversion functions.
2929

30-
* Prefer multiplication for `FromUnitToBaseFunc` (`{x}*2.54e-2` for `Inch` to `Meter`)
31-
* Prefer division for `FromBaseToUnitFunc` (`{x}/2.54e-2` for `Meter` to `Inch`)
30+
* Prefer multiplication for `FromUnitToBaseFunc` (`{x} * 2.54e-2` for `Inch` to `Meter`)
31+
* Prefer division for `FromBaseToUnitFunc` (`{x} / 2.54e-2` for `Meter` to `Inch`)
3232
* Prefer scientific notation `1e3` and `1e-5` instead of `1000` and `0.00001`
33-
* Prefer a constant if the conversion factor is finite (`{x}*2.54e-2` for `Inch`)
34-
* Prefer a calculation if the conversion factor is infinite (`({x}/72.27)*2.54e-2` for `PrinterPoint`)
33+
* Prefer a constant if the conversion factor is finite (`{x} * 2.54e-2` for `Inch`)
34+
* Prefer a calculation if the conversion factor is infinite (`({x} / 72.27)*2.54e-2` for `PrinterPoint`)
3535

3636
### Units
3737
Generally we try to name the units as what is the most widely used.

CodeGen/Generators/NanoFrameworkGen/NuspecGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public string Generate()
4242
<dependencies>
4343
<dependency id=""nanoFramework.CoreLibrary"" version=""{_mscorlibNuGetVersion}"" />");
4444

45-
if(NanoFrameworkGenerator.ProjectsRequiringMath.Contains(_quantity.Name))
45+
if (NanoFrameworkGenerator.ProjectsRequiringMath.Contains(_quantity.Name))
4646
{
4747
Writer.WL($@"
4848
<dependency id=""nanoFramework.System.Math"" version=""{_mathNuGetVersion}"" />");

CodeGen/Generators/NanoFrameworkGen/ProjectGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public string Generate()
4848
<SpecificVersion>True</SpecificVersion>
4949
</Reference>");
5050

51-
if(NanoFrameworkGenerator.ProjectsRequiringMath.Contains(_quantity.Name))
51+
if (NanoFrameworkGenerator.ProjectsRequiringMath.Contains(_quantity.Name))
5252
{
5353
Writer.WL($@"
5454
<Reference Include=""System.Math, Version={_versions.MathVersion}, Culture=neutral, PublicKeyToken=c07d481e9758c731"">

CodeGen/Generators/NanoFrameworkGen/QuantityGenerator.cs

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public struct {_quantity.Name}
5757
public {_quantity.ValueType} Value => _value;
5858
5959
/// <inheritdoc />
60-
public {_unitEnumName} Unit => _unit;");
60+
public {_unitEnumName} Unit => _unit;
61+
");
6162

6263
// Constructor and static properties
6364
Writer.WL($@" /// <summary>
@@ -88,7 +89,8 @@ public struct {_quantity.Name}
8889
/// <summary>
8990
/// Represents the smallest possible value of Duration
9091
/// </summary>
91-
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}({_quantity.ValueType}.MinValue, BaseUnit);");
92+
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}({_quantity.ValueType}.MinValue, BaseUnit);
93+
");
9294

9395
// Decimal MaxValue = 79228162514264337593543950335M
9496
Writer.WLCondition(_quantity.ValueType == "decimal", $@"
@@ -97,7 +99,8 @@ public struct {_quantity.Name}
9799
/// <summary>
98100
/// Represents the smallest possible value of Duration
99101
/// </summary>
100-
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}(-79228162514264337593543950335M, BaseUnit);");
102+
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}(-79228162514264337593543950335M, BaseUnit);
103+
");
101104

102105
Writer.WL($@"
103106
/// <summary>
@@ -124,10 +127,14 @@ private void GenerateConversionProperties()
124127
");
125128
foreach (var unit in _quantity.Units)
126129
{
130+
if (unit.SkipConversionGeneration)
131+
continue;
132+
127133
Writer.WL($@"
128134
/// <summary>
129-
/// Get {_quantity.Name} in {unit.PluralName}.
135+
/// Gets a <see cref=""double""/> value of this quantity converted into <see cref=""{_unitEnumName}.{unit.SingularName}""/>
130136
/// </summary>");
137+
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
131138
Writer.WL($@"
132139
public {_quantity.ValueType} {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
133140
");
@@ -146,18 +153,21 @@ private void GenerateStaticFactoryMethods()
146153
");
147154
foreach (var unit in _quantity.Units)
148155
{
156+
if (unit.SkipConversionGeneration)
157+
continue;
158+
149159
var valueParamName = unit.PluralName.ToLowerInvariant();
150160
Writer.WL($@"
151161
/// <summary>
152-
/// Get {_quantity.Name} from {unit.PluralName}.
162+
/// Creates a <see cref=""{_quantity.Name}""/> from <see cref=""{_unitEnumName}.{unit.SingularName}""/>.
153163
/// </summary>
154164
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>");
165+
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
155166
Writer.WL($@"
156167
public static {_quantity.Name} From{unit.PluralName}({_quantity.ValueType} {valueParamName}) => new {_quantity.Name}({valueParamName}, {_unitEnumName}.{unit.SingularName});
157168
");
158169
}
159170

160-
Writer.WL();
161171
Writer.WL($@"
162172
/// <summary>
163173
/// Dynamically convert from value and unit enum <see cref=""{_unitEnumName}"" /> to <see cref=""{_quantity.Name}"" />.
@@ -177,71 +187,82 @@ private void GenerateStaticFactoryMethods()
177187
private void GenerateConversionMethods()
178188
{
179189
Writer.WL($@"
180-
#region Conversion Methods
181-
182-
/// <summary>
183-
/// Convert to the unit representation <paramref name=""unit"" />.
184-
/// </summary>
185-
/// <returns>Value converted to the specified unit.</returns>
186-
public {_quantity.ValueType} As({_unitEnumName} unit) => GetValueAs(unit);
187-
188-
/// <summary>
189-
/// Converts this Duration to another Duration with the unit representation <paramref name=""unit"" />.
190-
/// </summary>
191-
/// <returns>A Duration with the specified unit.</returns>
192-
public {_quantity.Name} ToUnit({_unitEnumName} unit)
193-
{{
194-
var convertedValue = GetValueAs(unit);
195-
return new {_quantity.Name}(convertedValue, unit);
196-
}}
197-
198-
/// <summary>
199-
/// Converts the current value + unit to the base unit.
200-
/// This is typically the first step in converting from one unit to another.
201-
/// </summary>
202-
/// <returns>The value in the base unit representation.</returns>
203-
private {_quantity.ValueType} GetValueInBaseUnit()
204-
{{
205-
switch(Unit)
206-
{{");
190+
#region Conversion Methods
191+
192+
/// <summary>
193+
/// Convert to the unit representation <paramref name=""unit"" />.
194+
/// </summary>
195+
/// <returns>Value converted to the specified unit.</returns>
196+
public {_quantity.ValueType} As({_unitEnumName} unit) => GetValueAs(unit);
197+
198+
/// <summary>
199+
/// Converts this Duration to another Duration with the unit representation <paramref name=""unit"" />.
200+
/// </summary>
201+
/// <returns>A Duration with the specified unit.</returns>
202+
public {_quantity.Name} ToUnit({_unitEnumName} unit)
203+
{{
204+
var convertedValue = GetValueAs(unit);
205+
return new {_quantity.Name}(convertedValue, unit);
206+
}}
207+
208+
/// <summary>
209+
/// Converts the current value + unit to the base unit.
210+
/// This is typically the first step in converting from one unit to another.
211+
/// </summary>
212+
/// <returns>The value in the base unit representation.</returns>
213+
private {_quantity.ValueType} GetValueInBaseUnit()
214+
{{
215+
return Unit switch
216+
{{");
207217
foreach (var unit in _quantity.Units)
208218
{
209219
var func = unit.FromUnitToBaseFunc.Replace("{x}", "_value");
210220
Writer.WL($@"
211-
case {_unitEnumName}.{unit.SingularName}: return {func};");
221+
{_unitEnumName}.{unit.SingularName} => {func},");
212222
}
213223

214224
Writer.WL($@"
215-
default:
216-
throw new NotImplementedException($""Can not convert {{Unit}} to base units."");
217-
}}
218-
}}
225+
_ => throw new NotImplementedException($""Can not convert {{Unit}} to base units."")
226+
}};
227+
}}
219228
220-
private {_quantity.ValueType} GetValueAs({_unitEnumName} unit)
221-
{{
222-
if(Unit == unit)
223-
return _value;
229+
private {_quantity.ValueType} GetValueAs({_unitEnumName} unit)
230+
{{
231+
if (Unit == unit)
232+
return _value;
224233
225-
var baseUnitValue = GetValueInBaseUnit();
234+
var baseUnitValue = GetValueInBaseUnit();
226235
227-
switch(unit)
228-
{{");
236+
return unit switch
237+
{{");
229238
foreach (var unit in _quantity.Units)
230239
{
231240
var func = unit.FromBaseToUnitFunc.Replace("{x}", "baseUnitValue");
232241
Writer.WL($@"
233-
case {_unitEnumName}.{unit.SingularName}: return {func};");
242+
{_unitEnumName}.{unit.SingularName} => {func},");
234243
}
235244

236245
Writer.WL(@"
237-
default:
238-
throw new NotImplementedException($""Can not convert {Unit} to {unit}."");
239-
}
240-
}
246+
_ => throw new NotImplementedException($""Can not convert {Unit} to {unit}."")
247+
};
248+
}
241249
242-
#endregion
243-
");
250+
#endregion");
244251
}
245252

253+
/// <inheritdoc cref="GetObsoleteAttributeOrNull(string)"/>
254+
internal static string? GetObsoleteAttributeOrNull(Quantity quantity) => GetObsoleteAttributeOrNull(quantity.ObsoleteText);
255+
256+
/// <inheritdoc cref="GetObsoleteAttributeOrNull(string)"/>
257+
internal static string? GetObsoleteAttributeOrNull(Unit unit) => GetObsoleteAttributeOrNull(unit.ObsoleteText);
258+
259+
/// <summary>
260+
/// Returns the Obsolete attribute if ObsoleteText has been defined on the JSON input - otherwise returns empty string
261+
/// It is up to the consumer to wrap any padding/new lines in order to keep to correct indentation formats
262+
/// </summary>
263+
private static string? GetObsoleteAttributeOrNull(string obsoleteText) => string.IsNullOrWhiteSpace(obsoleteText)
264+
? null
265+
: $"[Obsolete(\"{obsoleteText}\")]";
266+
246267
}
247268
}

CodeGen/Generators/NanoFrameworkGen/UnitTypeGenerator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum {_unitEnumName}
4343
/// <remarks>{unit.XmlDocRemarks}</remarks>");
4444
}
4545

46-
Writer.WLIfText(2, UnitsNetGen.QuantityGenerator.GetObsoleteAttributeOrNull(unit));
46+
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
4747
Writer.WL($@"
4848
{unit.SingularName},");
4949
}
@@ -55,5 +55,10 @@ public enum {_unitEnumName}
5555
}}");
5656
return Writer.ToString();
5757
}
58+
59+
private string? GetObsoleteAttributeOrNull(string obsoleteText) =>
60+
string.IsNullOrWhiteSpace(obsoleteText) ?
61+
null :
62+
$"[System.Obsolete(\"{obsoleteText}\")]";
5863
}
5964
}

CodeGen/Generators/QuantityJsonFilesParser.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
@@ -94,8 +94,11 @@ private static void AddPrefixUnits(Quantity quantity)
9494
BaseUnits = null, // Can we determine this somehow?
9595
FromBaseToUnitFunc = $"({unit.FromBaseToUnitFunc}) / {prefixInfo.Factor}",
9696
FromUnitToBaseFunc = $"({unit.FromUnitToBaseFunc}) * {prefixInfo.Factor}",
97-
Localization = GetLocalizationForPrefixUnit(unit.Localization, prefixInfo)
98-
});
97+
Localization = GetLocalizationForPrefixUnit(unit.Localization, prefixInfo),
98+
ObsoleteText = unit.ObsoleteText,
99+
SkipConversionGeneration = unit.SkipConversionGeneration,
100+
AllowAbbreviationLookup = unit.AllowAbbreviationLookup
101+
} );
99102
}
100103
catch (Exception e)
101104
{

CodeGen/Generators/UnitsNetGen/NumberExtensionsGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static class NumberTo{_quantityName}Extensions
3737

3838
foreach (var unit in _units)
3939
{
40+
if (unit.SkipConversionGeneration)
41+
continue;
42+
4043
Writer.WL(2, $@"
4144
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(UnitsNet.QuantityValue)"" />");
4245

@@ -55,6 +58,6 @@ public static class NumberTo{_quantityName}Extensions
5558
private static string? GetObsoleteAttributeOrNull(string obsoleteText) =>
5659
string.IsNullOrWhiteSpace(obsoleteText) ?
5760
null :
58-
$"[System.Obsolete(\"{obsoleteText}\")]";
61+
$"[Obsolete(\"{obsoleteText}\")]";
5962
}
6063
}

CodeGen/Generators/UnitsNetGen/NumberExtensionsTestClassGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class NumberTo{_quantityName}ExtensionsTests
3232

3333
foreach (var unit in _units)
3434
{
35+
if (unit.SkipConversionGeneration)
36+
continue;
37+
3538
Writer.WL(2, $@"
3639
[Fact]");
3740

@@ -50,6 +53,6 @@ public class NumberTo{_quantityName}ExtensionsTests
5053
private static string? GetObsoleteAttributeOrNull(string obsoleteText) =>
5154
string.IsNullOrWhiteSpace(obsoleteText) ?
5255
null :
53-
$"[System.Obsolete({obsoleteText})]";
56+
$"[Obsolete(\"{obsoleteText}\")]";
5457
}
5558
}

0 commit comments

Comments
 (0)