CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
Go.cs
Go to the documentation of this file.
1 using System.Linq;
2 
3 namespace CodeGen.generators
4 {
9  public class GoGenerator : Generator
10  {
11  private const string ClassFormat = "type {0} struct {{{1}}}{2}{3}";
12  private string Indent { get; } = GeneratorConf.GetIndent(true, 4);
13 
15  protected override string GenerateClass(Class @class)
16  {
17  string fields = "", methods = "", classes = "";
18  fields = @class.Fields?.Aggregate($"\n{fields}", (current, field) => $"{current}{GenerateField(field)}\n");
19  methods = @class.Methods?.Aggregate($"\n\n{methods}",
20  (current, method) => current + $"func ({@class.Name}) {GenerateMethod(method)}\n\n");
21  classes = @class.Classes?.Aggregate(classes, (current, cls) => current + GenerateClass(cls));
22  return string.Format(ClassFormat, @class.Name, fields, methods, classes);
23  }
24 
26  public override string GenerateField(Field field)
27  {
28  var result = Indent;
29 
30  if (field.Access == "public")
31  {
32  field.Name = field.Name?.First().ToString().ToUpper() + field.Name?.Substring(1);
33  }
34 
35  result += $"{field.Name} {field.Type}";
36 
37  return result;
38  }
39 
41  public override string GenerateMethod(Method method)
42  {
43  var result = "";
44  if (method.Access == "public")
45  {
46  method.Name = method.Name?.First().ToString().ToUpper() + method.Name?.Substring(1);
47  }
48  else
49  {
50  method.Name = method.Name?.First().ToString().ToLower() + method.Name?.Substring(1);
51  }
52 
53  result += method.Name + '(';
54 
55  for (var i = 0; i < method.Parameters?.Length; i++)
56  {
57  result += method.Parameters[i].Name + ' ' + method.Parameters[i].Type;
58  if (i + 1 < method.Parameters.Length)
59  {
60  result += ", ";
61  }
62  }
63 
64  result += ')';
65 
66  if (method.Type != "")
67  {
68  result += " " + method.Type;
69  }
70 
71  result += " {";
72 
73  if (method.Type != "")
74  {
75  result += $"\n{Indent}return nil\n";
76  }
77 
78  result += '}';
79 
80  return result;
81  }
82  }
83 }
Generator for Go
Definition: Go.cs:9
string Type
Represents return type of the method. Type: string
Definition: Models.cs:178
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
Definition: Models.cs:48
string Name
Represents the name of the method. Type: string
Definition: Models.cs:172
string Name
Represents the name of the variable. Type: string
Definition: Models.cs:102
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: Go.cs:15
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: Go.cs:41
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Definition: Go.cs:26
string Access
Represents access level of the field. Type: string
Definition: Models.cs:127
Interface of language generator
string Access
Represents access level of the method. Type: string
Definition: Models.cs:184
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121