Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add 1200 line max #995

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/scripts/max-line-length.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Check if all required arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <folder_path> <max_line_length> <file_globs>"
exit 1
fi

folder_path="$1"
max_length="$2"
file_globs="$3"

# Function to get relative path
get_relative_path() {
local path="$1"
local base="$(pwd)"
echo "${path#$base/}"
}

# Initialize a flag to check if any lines exceed the max length
found_lines=0

# Convert comma-separated globs to an array
IFS=',' read -ra glob_array <<< "$file_globs"

# Use find to get all files matching the glob patterns
for glob in "${glob_array[@]}"; do
while IFS= read -r -d '' file; do
# Get the relative path
relative_path=$(get_relative_path "$file")

# Use awk to process each file
awk -v max="$max_length" -v file="$relative_path" '
length($0) > max {
print file ":" NR
found = 1
exit 1
}
END {
exit found
}' "$file"

# Check awk's exit status
if [ $? -eq 1 ]; then
found_lines=1
fi
done < <(find "$folder_path" -type f -name "$glob" -print0)
done

# Exit with appropriate code
if [ $found_lines -eq 0 ]; then
echo "All lines are within the $max_length character limit."
exit 0
else
echo "Some lines exceedded the $max_length character limit."
exit 1
fi
13 changes: 13 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,16 @@ jobs:

- name: Lint
run: composer lint

max-line-length:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Make script executable
run: chmod +x ./.github/scripts/max-line-length.sh

- name: Check max lines
run: ./.github/scripts/max-line-length.sh . 1200 "*.twig"
41 changes: 35 additions & 6 deletions templates/dotnet/Package/Models/Model.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,43 @@ namespace {{ spec.title | caseUcfirst }}.Models
{%~ endif %}
}

public static {{ definition.name | caseUcfirst | overrideIdentifier}} From(Dictionary<string, object> map) => new {{ definition.name | caseUcfirst | overrideIdentifier}}(
public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary<string, object> map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}(
{%~ for property in definition.properties %}
{{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<List<Dictionary<string, object>>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList(){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject<Dictionary<string, object>>()!){% endif %}{% else %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>(){% else %}{% if property.type == "integer" or property.type == "number" %}{% if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]){% else %}{% if property.type == "boolean" %}({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"]{% else %}map{% if not property.required %}.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null{% else %}["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString(){% endif %}{% endif %}{% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}

{{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:
{%- if property.sub_schema %}
{%~ if property.type == 'array' %}
((JArray)map["{{ property.name }}"])
.ToObject<List<Dictionary<string, object>>>()
.Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it))
.ToList()
{%- else %}
{{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(
map: ((JObject)map["{{ property.name }}"])
.ToObject<Dictionary<string, object>>()!
)
{%- endif %}
{%~ else %}
{%~ if property.type == 'array' %}
((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>()
{%~ else %}
{%~ if property.type == "integer" or property.type == "number" %}
{%~ if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %} Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"])
{%~ else %}
{%~ if property.type == "boolean" %}
({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"]
{%~ else %}
{%~ if not property.required %}map.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null
{%~ else %} map["{{ property.name }}"].ToString(){% endif %}
{%- endif %}
{%~ endif %}
{%~ endif %}
{%~ endif %}
{%- if not loop.last or (loop.last and definition.additionalProperties) %},
{%~ endif %}
{%~ endfor %}
{%~ if definition.additionalProperties %}
data: map
{%~ endif %}
{%- if definition.additionalProperties %}
, data: map
{%- endif ~%}
);

public Dictionary<string, object?> ToMap() => new Dictionary<string, object?>()
Expand Down