Skip to content

Commit

Permalink
Generate typed return values for arrays.
Browse files Browse the repository at this point in the history
Fixes oniksan#38.

Given:

```
message Foo {
    repeated int32 i = 1;
    repeated string s = 2;
    repeated Bar b = 3;
}
```

Previously godobuf would generate:

```
get_i() -> Array
get_s() -> Array
get_b() -> Array
```

Now it generates:

```
get_i() -> Array[int]
get_s() -> Array[string]
get_b() -> Array[Bar]
```

To do this, we must assign a typed array when constructing the PBField.
Godot does not currently have a typed array constructor: godotengine/godot#72627 (comment)
To work around this, we create a local typed variable, like so:

```
class Test1:
    func _init():
        var __rf_double_default: Array[float] = []
        __rf_double = PBField.new("rf_double", PB_DATA_TYPE.DOUBLE, PB_RULE.REPEATED, 23, true, __rf_double_default)
...
    func get_rf_double() -> Array[float]:
        return __rf_double.value
```
  • Loading branch information
rcorre committed Jan 21, 2024
1 parent b84a64e commit 7a7cc66
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions addons/protobuf/parser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,12 @@ class Translator:
return text + "RESERVED"
return text

func generate_gdscript_type(field : Analysis.ASTField) -> String:
if field.field_type == Analysis.FIELD_TYPE.MESSAGE:
var type_name : String = class_table[field.type_class_id].parent_name + "." + class_table[field.type_class_id].name
return type_name.substr(1, type_name.length() - 1)
return generate_gdscript_simple_type(field)

func generate_gdscript_simple_type(field : Analysis.ASTField) -> String:
if field.field_type == Analysis.FIELD_TYPE.INT32:
return "int"
Expand Down Expand Up @@ -1730,7 +1736,15 @@ class Translator:
var text : String = ""
var f : Analysis.ASTField = field_table[field_index]
var field_name : String = "__" + f.name
var pbfield_text : String = field_name + " = PBField.new("
var pbfield_text : String
var default_var_name := field_name + "_default"
if f.qualificator == Analysis.FIELD_QUALIFICATOR.REPEATED:
var type_name := generate_gdscript_type(f)
if type_name:
text = tabulate("var %s: Array[%s] = []\n" % [default_var_name, type_name], nesting)
else:
text = tabulate("var %s: Array = []\n" % [default_var_name], nesting)
pbfield_text += field_name + " = PBField.new("
pbfield_text += "\"" + f.name + "\", "
pbfield_text += generate_field_type(f) + ", "
pbfield_text += generate_field_rule(f) + ", "
Expand All @@ -1740,7 +1754,7 @@ class Translator:
elif f.option == Analysis.FIELD_OPTION.NOT_PACKED:
pbfield_text += "false"
if f.qualificator == Analysis.FIELD_QUALIFICATOR.REPEATED:
pbfield_text += ", []"
pbfield_text += ", " + default_var_name
else:
pbfield_text += ", " + default_dict_text() + "[" + generate_field_type(f) + "]"
pbfield_text += ")\n"
Expand Down Expand Up @@ -1798,7 +1812,7 @@ class Translator:
the_class_name = the_class_name.substr(1, the_class_name.length() - 1)
text += generate_has_oneof(field_index, nesting)
if f.qualificator == Analysis.FIELD_QUALIFICATOR.REPEATED:
text += tabulate("func get_" + f.name + "() -> Array:\n", nesting)
text += tabulate("func get_" + f.name + "() -> Array[" + the_class_name + "]:\n", nesting)
else:
text += tabulate("func get_" + f.name + "() -> " + the_class_name + ":\n", nesting)
nesting += 1
Expand Down Expand Up @@ -1914,7 +1928,8 @@ class Translator:
argument_type = " : " + gd_type
text += generate_has_oneof(field_index, nesting)
if f.qualificator == Analysis.FIELD_QUALIFICATOR.REPEATED:
text += tabulate("func get_" + f.name + "() -> Array:\n", nesting)
var array_type := "[" + gd_type + "]" if gd_type else ""
text += tabulate("func get_" + f.name + "() -> Array" + array_type + ":\n", nesting)
else:
text += tabulate("func get_" + f.name + "()" + return_type + ":\n", nesting)
nesting += 1
Expand Down

0 comments on commit 7a7cc66

Please sign in to comment.