Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CSharpObjectBuilder / generate C# new object from json
- Loading branch information
Showing
3 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using DataPowerTools.Extensions; | ||
|
||
namespace DataPowerTools.PowerTools | ||
{ | ||
public class CSharpObjectBuilder | ||
{ | ||
private readonly List<CSharpObjectInitDef> _defs = []; | ||
|
||
//private readonly StringBuilder _s = new StringBuilder(); | ||
|
||
public CSharpObjectBuilder() | ||
{ | ||
|
||
} | ||
|
||
public void AddCSharpObjectDef(IEnumerable<CSharpObjectInitDef> defs) | ||
{ | ||
_defs.AddRange(defs); | ||
} | ||
|
||
public void AddCSharpObjectDef(CSharpObjectInitDef def) | ||
{ | ||
_defs.Add(def); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
var s = new StringBuilder(); | ||
|
||
switch (_defs.Count) | ||
{ | ||
case 0: | ||
return string.Empty; | ||
case 1: | ||
return BuildDefinition(_defs.First()); | ||
} | ||
|
||
return BuildDefinition(_defs.ToArray()); | ||
} | ||
|
||
public static string BuildDefinition(string val, CSharpObjInitType initType) | ||
{ | ||
switch (initType) | ||
{ | ||
case CSharpObjInitType.Default: | ||
case CSharpObjInitType.String: | ||
return $@"""{val}"""; | ||
break; | ||
case CSharpObjInitType.Numeric: | ||
return val; | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(initType), initType, null); | ||
} | ||
} | ||
|
||
public static string BuildDefinition(CSharpObjectInitDef[] initsDefs) | ||
{ | ||
var subItems = initsDefs | ||
.Select(BuildDefinition) | ||
.JoinStr(",\r\n"); | ||
|
||
var template = $@"new[] {{ | ||
{subItems.Indent(1)} | ||
}}"; | ||
|
||
return template; | ||
} | ||
|
||
public static string BuildDefinition(CSharpObjectInitDef init) | ||
{ | ||
var subItems = init | ||
.Inits | ||
.Select(def => $@"{def.Name.Replace(" ", "")} = {BuildDefinition(def.Value, def.DataType)}") | ||
.JoinStr(",\r\n"); | ||
|
||
var template = $@"new() {{ | ||
{subItems.Indent(1)} | ||
}}"; | ||
|
||
return template; | ||
} | ||
|
||
public class CSharpObjectInitDef | ||
{ | ||
//public string? TypeName | ||
public CSharpObjectInit[] Inits { get; set; } | ||
} | ||
|
||
public class CSharpObjectInit | ||
{ | ||
public string Name { get; set; } | ||
public string Value { get; set; } | ||
public CSharpObjInitType DataType { get; set; } | ||
} | ||
|
||
public enum CSharpObjInitType | ||
{ | ||
Default, | ||
String, | ||
Numeric | ||
} | ||
} | ||
} |