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

refactoring around codegen-test and Drivers project #55

Merged
merged 1 commit into from
Jun 16, 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
7 changes: 3 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ jobs:
run: ./scripts/wasm/update_sha.sh sqlc.ci.yaml

- name: Codegen Test
run: |
./scripts/run_codegen_test.sh sqlc.ci.yaml \
${{ matrix.file-per-query }} ${{ matrix.generate-csproj }} ${{ matrix.target-framework }}
run: ./scripts/run_codegen_test.sh sqlc.ci.yaml \
${{ matrix.file-per-query }} ${{ matrix.generate-csproj }} ${{ matrix.target-framework }}

- uses: actions/setup-dotnet@v4
with:
Expand Down Expand Up @@ -186,7 +185,7 @@ jobs:
- name: Bump version and create new tag
id: bump_version
run: |
set -ex
set -e
echo "Extract the latest tag version"
LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ COPY Drivers/*.csproj ./Drivers/
COPY EndToEndTests/*.csproj ./EndToEndTests/
COPY Extensions/*.csproj ./Extensions/
COPY GeneratedProtobuf/*.csproj ./GeneratedProtobuf/
COPY LocalRunner/*.csproj ./LocalRunner/
COPY MySqlConnectorExample/*.csproj ./MySqlConnectorExample/
COPY NpgsqlExample/*.csproj ./NpgsqlExample/
COPY PluginOptions/*.csproj ./PluginOptions/
COPY ProcessRunner/*.csproj ./ProcessRunner/
COPY SqlcGenCsharp/*.csproj ./SqlcGenCsharp/
COPY WasmRunner/*.csproj ./WasmRunner/

Expand All @@ -21,10 +21,10 @@ COPY Drivers/ ./Drivers/
COPY EndToEndTests/ ./EndToEndTests/
COPY Extensions/ ./Extensions/
COPY GeneratedProtobuf/ ./GeneratedProtobuf/
COPY LocalRunner/ ./LocalRunner/
COPY MySqlConnectorExample/ ./MySqlConnectorExample/
COPY NpgsqlExample/ ./NpgsqlExample/
COPY PluginOptions/ ./PluginOptions/
COPY ProcessRunner/ ./ProcessRunner/
COPY SqlcGenCsharp/ ./SqlcGenCsharp/
COPY WasmRunner/ ./WasmRunner/

Expand Down
8 changes: 7 additions & 1 deletion Drivers/DbDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public virtual UsingDirectiveSyntax[] GetUsingDirectives()
public string AddNullableSuffix(string csharpType, bool notNull)
{
if (notNull) return csharpType;
if (Utils.IsCsharpPrimitive(csharpType)) return $"{csharpType}?";
if (IsCsharpPrimitive(csharpType)) return $"{csharpType}?";
return DotnetFramework.LatestDotnetSupported() ? $"{csharpType}?" : csharpType;
}

Expand Down Expand Up @@ -73,4 +73,10 @@ public abstract MemberDeclarationSyntax ExecDeclare(string funcName, string text

public abstract MemberDeclarationSyntax ExecLastIdDeclare(string funcName, string queryTextConstant,
string argInterface, IList<Parameter> parameters);

public static bool IsCsharpPrimitive(string csharpType)
{
var csharpPrimitives = new HashSet<string> { "long", "double", "int", "float", "bool" };
return csharpPrimitives.Contains(csharpType.Replace("?", ""));
}
}
2 changes: 1 addition & 1 deletion Drivers/Generators/CommonGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ string GetNullExpression(Column column)
var csharpType = dbDriver.GetColumnType(column);
if (csharpType == "string")
return "string.Empty";
return !dbDriver.DotnetFramework.LatestDotnetSupported() && Utils.IsCsharpPrimitive(csharpType)
return !dbDriver.DotnetFramework.LatestDotnetSupported() && DbDriver.IsCsharpPrimitive(csharpType)
? $"({csharpType}) null"
: "null";
}
Expand Down
12 changes: 0 additions & 12 deletions Drivers/Utils.cs

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dotnet-publish-process: dotnet-build-process
dotnet publish ProcessRunner -c release --output dist/

sqlc-generate-process: dotnet-publish-process
sqlc -f sqlc.process.yaml generate
sqlc -f sqlc.local.yaml generate

test-process-plugin: sqlc-generate-process dockerfile-generate run-tests

Expand Down
116 changes: 58 additions & 58 deletions scripts/run_codegen_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,80 @@ set -e

declare -a examples=("MySqlConnectorExample" "NpgsqlExample")

config_file=$1
file_per_query=$2
generate_csproj=$3
target_framework=$4

generated_files_cleanup() {
generate_csproj=$1
for example_dir in "${examples[@]}"
do
echo "Deleting .cs files" && find "${example_dir}/" -type f -name "*.cs" -exec rm -f {} \;
if [ "${generate_csproj}" = "true" ]; then
echo "Deleting .csproj file" && rm "${example_dir}/${example_dir}.csproj"
fi
done
for example_dir in "${examples[@]}"
do
echo "Deleting .cs files in ${example_dir}"
find "${example_dir}/" -type f -name "*.cs" -exec rm -f {} \;
if [ "${generate_csproj}" = "true" ]; then
echo "Deleting .csproj file" && rm "${example_dir}/${example_dir}.csproj"
fi
done
}

change_config() {
for ((i=0; i<${#examples[@]}; i++)); do
echo "Changing configuration for project ${example_dir}"
yq -i "
.sql[${i}].codegen[0].options.filePerQuery = ${file_per_query} |
.sql[${i}].codegen[0].options.generateCsproj = ${generate_csproj} |
.sql[${i}].codegen[0].options.targetFramework = \"${target_framework}\"
" "${config_file}"
echo "${examples[i]} codegen config:" && yq ".sql[${i}].codegen[0]" "${config_file}"
done
}

check_cs_file_count() {
file_per_query=$1
for example_dir in "${examples[@]}"
do
file_count=$(find "${example_dir}/" -maxdepth 1 -name "*.cs" 2>/dev/null | wc -l)
if [[ "${file_per_query}" = "true" && "${file_count}" -le 2 ]]; then
echo "Assertion failed: Not more than 2 .cs files in the directory ${example_dir}."
return 1
elif [[ "${file_per_query}" = "false" && "${file_count}" -ne 2 ]]; then
echo "Assertion failed: Not exactly 2 .cs files in the directory ${example_dir}."
return 1
fi
done
for example_dir in "${examples[@]}"
do
echo "Checking C# file count in ${example_dir}/"
file_count=$(find "${example_dir}/" -maxdepth 1 -name "*.cs" 2>/dev/null | wc -l)
if [[ "${file_per_query}" = "true" && "${file_count}" -le 2 ]]; then
echo "Assertion failed: Not more than 2 .cs files in the directory ${example_dir}."
return 1
elif [[ "${file_per_query}" = "false" && "${file_count}" -ne 2 ]]; then
echo "Assertion failed: Not exactly 2 .cs files in the directory ${example_dir}."
return 1
fi
done
}

check_csproj_file() {
for example_dir in "${examples[@]}"
do
if [ ! -f "${example_dir}/${example_dir}.csproj" ]; then
echo "Assertion failed: A .csproj file is not present in the directory ${example_dir}."
return 1
fi
done
for example_dir in "${examples[@]}"
do
echo "Checking ${example_dir}.csproj file generated"
if [ ! -f "${example_dir}/${example_dir}.csproj" ]; then
echo "Assertion failed: A .csproj file is not present in the directory ${example_dir}."
return 1
fi
done
}

check_project_compiles() {
if [ "${generate_csproj}" = "true" ]; then
for example_dir in "${examples[@]}"
do
echo "Checking ${example_dir} project compiles"
dotnet build "${example_dir}/"
done
fi
}

config_file=$1
file_per_query=$2
generate_csproj=$3
target_framework=$4

yq -i "
.sql[0].codegen[0].options.filePerQuery = ${file_per_query} |
.sql[1].codegen[0].options.filePerQuery = ${file_per_query} |
.sql[0].codegen[0].options.generateCsproj = ${generate_csproj} |
.sql[1].codegen[0].options.generateCsproj = ${generate_csproj} |
.sql[0].codegen[0].options.targetFramework = \"${target_framework}\" |
.sql[1].codegen[0].options.targetFramework = \"${target_framework}\"
" "${config_file}"

generated_files_cleanup "${generate_csproj}"
echo "Using the following codegen config:" && \
yq '.sql[0].codegen[0]' "${config_file}" && \
yq '.sql[1].codegen[0]' "${config_file}"
generated_files_cleanup && change_config
sqlc -f "${config_file}" generate

status_code=$(check_cs_file_count "${file_per_query}")
if [ "${status_code}" -ne 0 ]; then
exit "${status_code}"
fi
status_code=$(check_csproj_file)
if [ "${status_code}" -ne 0 ]; then
test_functions=("check_cs_file_count" "check_csproj_file" "check_project_compiles")
for test_function in "${test_functions[@]}"; do
${test_function}
status_code=$?
if [ ${status_code} -ne 0 ]; then
echo "Function ${test_function} failed with status code ${status_code}"
exit "${status_code}"
fi
if [ "${generate_csproj}" = "true" ]; then
status_code=$(check_project_compiles "${target_framework}")
if [ "${status_code}" -ne 0 ]; then
exit "${status_code}"
fi
fi
fi
echo "Test ${test_function} passed"
done
2 changes: 1 addition & 1 deletion sqlc-gen-csharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessRunner", "ProcessRunner\ProcessRunner.csproj", "{649CFB46-68D1-41F8-BD76-FF4B79B16825}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalRunner", "LocalRunner\LocalRunner.csproj", "{649CFB46-68D1-41F8-BD76-FF4B79B16825}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySqlConnectorExample", "MySqlConnectorExample\MySqlConnectorExample.csproj", "{6406B659-77CF-4978-ABD3-BFBB2CBFFCA3}"
EndProject
Expand Down
File renamed without changes.
Loading