Skip to content

Commit ca5b341

Browse files
committed
use anonymous class
1 parent db7f390 commit ca5b341

1 file changed

Lines changed: 70 additions & 7 deletions

File tree

src/main/scala/io/viash/languages/CSharp.scala

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package io.viash.languages
1919

2020
import io.viash.helpers.Resources
21-
import io.viash.config.arguments.Argument
21+
import io.viash.config.arguments._
2222
import io.viash.config.Config
2323
import io.viash.config.resources.ScriptInjectionMods
2424

@@ -38,13 +38,76 @@ object CSharp extends Language {
3838
.mkString("\n")
3939

4040
val paramsCode = if (argsMetaAndDeps.nonEmpty) {
41-
// Parse JSON once and extract all sections
42-
val parseOnce = "// Parse JSON parameters once and extract all sections\nvar _viashJsonData = ViashJsonParser.ParseJson();\n"
43-
val extractSections = argsMetaAndDeps.map { case (dest, _) =>
44-
s"var $dest = _viashJsonData.ContainsKey(\"$dest\") ? (Dictionary<string, object>)_viashJsonData[\"$dest\"] : new Dictionary<string, object>();"
45-
}.mkString("\n")
41+
// Parse JSON once
42+
val parseOnce = "// Parse JSON parameters\nvar _viashJsonData = ViashJsonParser.ParseJson();\n\n"
4643

47-
parseOnce + extractSections
44+
// Generate anonymous object for each section (par, meta, dep)
45+
val sections = argsMetaAndDeps.map { case (dest, params) =>
46+
// Generate JSON extraction and anonymous object creation
47+
val jsonExtract = s"""var _${dest}Json = _viashJsonData.ContainsKey("$dest") ? (Dictionary<string, object>)_viashJsonData["$dest"] : new Dictionary<string, object>();"""
48+
49+
// Generate field assignments for anonymous object
50+
val fieldAssignments = params.map { par =>
51+
val jsonKey = par.plainName
52+
val fieldAssignment = par match {
53+
// Multiple values - extract as arrays
54+
case a: BooleanArgumentBase if a.multiple =>
55+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? ((List<object>)_${dest}Json["$jsonKey"]).Select(x => Convert.ToBoolean(x)).ToArray() : new bool[0]"""
56+
case a: IntegerArgument if a.multiple =>
57+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? ((List<object>)_${dest}Json["$jsonKey"]).Select(x => Convert.ToInt32(x)).ToArray() : new int[0]"""
58+
case a: LongArgument if a.multiple =>
59+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? ((List<object>)_${dest}Json["$jsonKey"]).Select(x => Convert.ToInt64(x)).ToArray() : new long[0]"""
60+
case a: DoubleArgument if a.multiple =>
61+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? ((List<object>)_${dest}Json["$jsonKey"]).Select(x => Convert.ToDouble(x)).ToArray() : new double[0]"""
62+
case a: FileArgument if a.multiple =>
63+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? ((List<object>)_${dest}Json["$jsonKey"]).Select(x => x?.ToString()).ToArray() : new string[0]"""
64+
case a: StringArgument if a.multiple =>
65+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? ((List<object>)_${dest}Json["$jsonKey"]).Select(x => x?.ToString()).ToArray() : new string[0]"""
66+
67+
// Optional values (nullable types)
68+
case a: BooleanArgumentBase if !a.required && a.flagValue.isEmpty =>
69+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? (bool?)Convert.ToBoolean(_${dest}Json["$jsonKey"]) : null"""
70+
case a: IntegerArgument if !a.required =>
71+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? (int?)Convert.ToInt32(_${dest}Json["$jsonKey"]) : null"""
72+
case a: LongArgument if !a.required =>
73+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? (long?)Convert.ToInt64(_${dest}Json["$jsonKey"]) : null"""
74+
case a: DoubleArgument if !a.required =>
75+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? (double?)Convert.ToDouble(_${dest}Json["$jsonKey"]) : null"""
76+
case a: FileArgument if !a.required =>
77+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? _${dest}Json["$jsonKey"]?.ToString() : null"""
78+
case a: StringArgument if !a.required =>
79+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? _${dest}Json["$jsonKey"]?.ToString() : null"""
80+
81+
// Required values
82+
case _: BooleanArgumentBase =>
83+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? Convert.ToBoolean(_${dest}Json["$jsonKey"]) : false"""
84+
case _: IntegerArgument =>
85+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? Convert.ToInt32(_${dest}Json["$jsonKey"]) : 0"""
86+
case _: LongArgument =>
87+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? Convert.ToInt64(_${dest}Json["$jsonKey"]) : 0L"""
88+
case _: DoubleArgument =>
89+
s""" $jsonKey = _${dest}Json.ContainsKey("$jsonKey") && _${dest}Json["$jsonKey"] != null ? Convert.ToDouble(_${dest}Json["$jsonKey"]) : 0.0"""
90+
case _: FileArgument =>
91+
" " + jsonKey + " = _" + dest + "Json.ContainsKey(\"" + jsonKey + "\") && _" + dest + "Json[\"" + jsonKey + "\"] != null ? _" + dest + "Json[\"" + jsonKey + "\"]?.ToString() : \"\""
92+
case _: StringArgument =>
93+
" " + jsonKey + " = _" + dest + "Json.ContainsKey(\"" + jsonKey + "\") && _" + dest + "Json[\"" + jsonKey + "\"] != null ? _" + dest + "Json[\"" + jsonKey + "\"]?.ToString() : \"\""
94+
}
95+
fieldAssignment
96+
}
97+
98+
// Generate the anonymous object
99+
val anonObject = if (params.nonEmpty) {
100+
s"""var $dest = new {
101+
${fieldAssignments.mkString(",\n")}
102+
};"""
103+
} else {
104+
s"var $dest = new {};"
105+
}
106+
107+
jsonExtract + "\n" + anonObject
108+
}
109+
110+
parseOnce + sections.mkString("\n\n")
48111
} else {
49112
""
50113
}

0 commit comments

Comments
 (0)