Sub Class_Globals
Type MyType(Name As String, City As String, Country As String, _
Amount As Int, Grade As Float, Phones As List, TestMap As Map, TestArray() As Int)
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
'Run just once to put required Sub on the log, argument is direct copy of Type statement
GenerateCreateType($"
Type MyType(Name As String, City As String, Country As String, _
Amount As Int, Grade As Float, Phones As List, TestMap As Map, TestArray() As Int)
"$)
Dim Person1 As MyType = MyTypeFromMap(CreateMap("Name":"Mike", "City":"Roma", "Country":"Italia", _
"Amount": 1000000, "Grade": 2.5, "Phones": CreateList(Array(1111111111, 2222222222, 3333333333)), _
"TestMap": CreateMap("Member": "William"), "TestArray": Array As Int(1,2,3)))
Log(Person1.Name & TAB & Person1.City & TAB & Person1.Country & TAB & Person1.Amount & _
TAB & Person1.Grade & TAB & Person1.Phones.Size & TAB & Person1.testMap.Get("Member") & TAB & Person1.TestArray(2))
End Sub
Private Sub GenerateCreateType(s As String)
s = s.Replace(CRLF, " ").Replace(" _ ", " ") 'to combine multiline declaration
Dim v() As String = TwoParts("(", s.trim)
Dim tname As String = v(0).substring(v(0).IndexOf(" ")+ 1)
Dim sb As StringBuilder: sb.Initialize
Dim args() As String = Regex.Split("\,", v(1).substring2(0, v(1).Length - 1))
For i = 0 To args.Length - 1
v = IIf(args(i).IndexOf("(") > -1, TwoParts("(", args(i).trim), TwoParts(" ", args(i).trim))
sb.Append($"'${TAB}If mp.ContainsKey("${v(0)}") Then t1.${v(0)} = mp.Get("${v(0)}")${CRLF}"$)
Next
Dim s As String = $"
'Public Sub ${tname}FromMap(mp As Map) As ${tname}
' Dim t1 As ${tname}
' t1.Initialize
${sb.ToString}
' Return t1
'End Sub
"$
Log(s)
End Sub
Private Sub TwoParts(delim As String, s As String) As String()
s = s.trim
Dim result(2) As String
result(0) = s
Dim k As Int = s.IndexOf(delim)
If k > - 1 Then
result(0) = s.SubString2(0, k).trim
result(1) = s.SubString(k + delim.length).trim
End If
Return result
End Sub
Private Sub CreateList(ar() As Object) As List
Dim a As List
a.Initialize
For Each obj As Object In ar
a.Add(obj)
Next
Return a
End Sub
'Copied from log, generated by: GenerateCreateType
Public Sub MyTypeFromMap(mp As Map) As MyType
Dim t1 As MyType
t1.Initialize
If mp.ContainsKey("Name") Then t1.Name = mp.Get("Name")
If mp.ContainsKey("City") Then t1.City = mp.Get("City")
If mp.ContainsKey("Country") Then t1.Country = mp.Get("Country")
If mp.ContainsKey("Amount") Then t1.Amount = mp.Get("Amount")
If mp.ContainsKey("Grade") Then t1.Grade = mp.Get("Grade")
If mp.ContainsKey("Phones") Then t1.Phones = mp.Get("Phones")
If mp.ContainsKey("TestMap") Then t1.TestMap = mp.Get("TestMap")
If mp.ContainsKey("TestArray") Then t1.TestArray = mp.Get("TestArray")
Return t1
End Sub
'On Log
'Public Sub MyTypeFromMap(mp As Map) As MyType
' Dim t1 As MyType
' t1.Initialize
' If mp.ContainsKey("Name") Then t1.Name = mp.Get("Name")
' If mp.ContainsKey("City") Then t1.City = mp.Get("City")
' If mp.ContainsKey("Country") Then t1.Country = mp.Get("Country")
' If mp.ContainsKey("Amount") Then t1.Amount = mp.Get("Amount")
' If mp.ContainsKey("Grade") Then t1.Grade = mp.Get("Grade")
' If mp.ContainsKey("Phones") Then t1.Phones = mp.Get("Phones")
' If mp.ContainsKey("TestMap") Then t1.TestMap = mp.Get("TestMap")
' If mp.ContainsKey("TestArray") Then t1.TestArray = mp.Get("TestArray")
' Return t1
'End Sub