In some cases, you need to create a bunch of repeating code based on some data you intended on processing. In the following examples, I had all the field names listed in a powershell
script but I just needed to translate it to C#.
I pasted the data in needed onto a linux box into a file called t.txt
. This is the general format of the file where this is just a small piece of the 50 or so fields this script was displaying.
~/tmp % head t.txt
Write-Host "-- City.............................: $($online_user_details.City)" -ForegroundColor Cyan
Write-Host "-- DisplayName......................: $($online_user_details.DisplayName)" -ForegroundColor Cyan
Write-Host "-- Enabled..........................: $($online_user_details.Enabled)" -ForegroundColor Cyan
Write-Host "-- FirstName........................: $($online_user_details.FirstName)" -ForegroundColor Cyan
What I am insterested in is the $online_user_details.<property>
field which I will be extracting and useing to create
- A
Model
which will have each property listed. The first character will be converted to lowercase. - A list of constants since (in my case) the properites will be used to index values from a dictionary.
- A set of
case
statements which I will use when taking the property to get the value for my Model.
The code you need may vary but the approach is very adapatable.
NOTE
that this will not generate the full file but will instead generate a snippet of code you can paste within a source code file.
This can be run with bash
no problem. I am using zsh
for the heck of it.
#!/usr/bin/env zsh
#
# DESC:
# The goal of this script is to generate accessors and constants
#
# SAMPLE INPUT:
# There blank is intensional
# ~/tmp % head -10 t.txt
#
# Write-Host "-- City.............................: $($online_user_details.City)" -ForegroundColor Cyan
# Write-Host "-- DisplayName......................: $($online_user_details.DisplayName)" -ForegroundColor Cyan
# Write-Host "-- Enabled..........................: $($online_user_details.Enabled)" -ForegroundColor Cyan
# Write-Host "-- FirstName........................: $($online_user_details.FirstName)" -ForegroundColor Cyan
#
# OUTPUT:
# This will create output txt files who's contents can be pasted into
#
echo "=============================================================="
echo " Building Accessors "
echo "=============================================================="
cat t.txt |awk '{printf("%s\n",$4);}' | \
awk '{FS="."}{printf("%s\n",$2);}' |\
sed 's/)"$//' | \
grep -v -e '^$' | \
sed 's/\b\(.\)/\l\1/g' | \
awk '{printf("public string %s { get; set; }\n",$1);}' | \
sort | \
uniq | \
tee accessors.txt
echo "=============================================================="
echo " Constants "
echo "=============================================================="
cat t.txt |awk '{printf("%s\n",$4);}' | \
awk '{FS="."}{printf("%s\n",$2);}' |\
sed 's/)"$//' | \
grep -v -e '^$' | \
sort | \
uniq | \
awk '{printf("CONST_%s %s\n", $1, $1);}' | \
sed 's/^[^ ]*/\U&\E/' | \
awk '{printf("public const string %s = \"%s\";\n", $1, $2);}' | \
tee constants.txt
echo "=============================================================="
echo " Switch Case "
echo "=============================================================="
cat t.txt |awk '{printf("%s\n",$4);}' | \
awk '{FS="."}{printf("%s\n",$2);}' |\
sed 's/)"$//' | \
grep -v -e '^$' | \
sort | \
uniq | \
awk '{printf("CONST_%s %s\n", $1, $1);}' | \
sed 's/^[^ ]*/\U&\E/' | \
awk '{printf("%s %s %s\n", $2, $1, $2);}' | \
sed 's/\b\(.\)/\l\1/' | \
awk '{printf("case UserDetailsConstants.%s:\n model.%s = pspi.Value != null ? pspi.Value.ToString() : null;\n break;\n", $2, $1);}' | \
tee switch_case.txt
#
# end of script
#
This script will extract the property names from t.txt
, set the first character to be lowercase, and then build the output like public string ____ { get; set; }
. This can be pasted in to a Model
file.
public string city { get; set; }
public string displayName { get; set; }
public string enabled { get; set; }
public string firstName { get; set; }
The script above take the same t.txt
file and creates a two column output. The second column is left alone but the first has "CONST_" pre-pended and the entire string is made uppercase. Finally, the public const string ____ = "___";
is written.
This can be pasted into an existing file or a spacific UserDtailsConstants.cs
file.
public const string CONST_CITY = "City";
public const string CONST_DISPLAYNAME = "DisplayName";
public const string CONST_ENABLED = "Enabled";
public const string CONST_FIRSTNAME = "FirstName";
The pspi
object nas a Name
and Value
associated. The main switch
statement will key off the Name
. Using the constants created above, if the case
is true, we copy the value as a string into a model (based on the accessors above) if the Value
is not null. Otherwise, we set the model's property to null. Later on this will be converted to JSON so we don't want empty properties.
This code can be pasted into a switch
statement block before the default
and used to copy the correct value to a model property based on the name within a dictionary.
case UserDetailsConstants.CONST_CITY:
model.city = pspi.Value != null ? pspi.Value.ToString() : null;
break;
case UserDetailsConstants.CONST_DIALPLAN:
model.dialPlan = pspi.Value != null ? pspi.Value.ToString() : null;
break;
case UserDetailsConstants.CONST_DISPLAYNAME:
model.displayName = pspi.Value != null ? pspi.Value.ToString() : null;
break;
case UserDetailsConstants.CONST_ENABLED:
model.enabled = pspi.Value != null ? pspi.Value.ToString() : null;
break;
case UserDetailsConstants.CONST_ENTERPRISEVOICEENABLED:
model.enterpriseVoiceEnabled = pspi.Value != null ? pspi.Value.ToString() : null;
break;
case UserDetailsConstants.CONST_FIRSTNAME:
model.firstName = pspi.Value != null ? pspi.Value.ToString() : null;
break;
Assuming the t.txt
files is being pipped through these commands like above the following tries to explain what they do.
Snippet | Description |
---|---|
awk '{printf("%s\n",$4);}' |
Grab the 4th column from the file assuming space delimiters |
awk '{FS="."}{printf("%s\n",$2);}' |
Using a delimiter (field separator) of "." get the second column |
sed 's/)"$//' |
Remove the )" at the end of a string. |
grep -v -e '^$' |
Filter out any empty lines |
sed 's/\b\(.\)/\l\1/g' |
Take the first character of a word and convert it to lowercase. |
sed 's/^[^ ]*/\U&\E/' |
Take the first word in a line and make it all uppercase |
These scripts can be copied and heavily modified per project. I use this technique a lot when I need to create everything from the types of things above to SQL SELECT, UPDATE, and INSERT statments. It can help create consisent and bug free code during an typically typo prone process.