Make shared attribute working in Golang

This commit is contained in:
Aleksandr Altshuler 2025-01-03 12:52:23 +03:00 committed by Aleksandr Altshuler
parent 99fda81905
commit fab32d1753
2 changed files with 10 additions and 5 deletions

View File

@ -429,9 +429,6 @@ Current understood attributes:
it won't be accessible anymore by newer code. Note that if you deprecate a it won't be accessible anymore by newer code. Note that if you deprecate a
field that was previous required, old code may fail to validate new data (when field that was previous required, old code may fail to validate new data (when
using the optional verifier). using the optional verifier).
### `required`
- `required` (on a non-scalar table field): this field must always be set. By - `required` (on a non-scalar table field): this field must always be set. By
default, fields do not need to be present in the binary. This is desirable, as default, fields do not need to be present in the binary. This is desirable, as
it helps with forwards/backwards compatibility, and flexibility of data it helps with forwards/backwards compatibility, and flexibility of data
@ -452,6 +449,9 @@ Current understood attributes:
- `force_align: size` (on a vector): force the alignment of this vector to be - `force_align: size` (on a vector): force the alignment of this vector to be
something different than what the element size would normally dictate. Note: something different than what the element size would normally dictate. Note:
Now only work for generated C++ code. Now only work for generated C++ code.
- `shared` (on a field): For string fields, this enables the usage of string
pooling (i.e. `CreateSharedString`) as default serialization behavior. Note:
Now only work for generated C++ and Golang code.
- `bit_flags` (on an unsigned enum): the values of this field indicate bits, - `bit_flags` (on an unsigned enum): the values of this field indicate bits,
meaning that any unsigned value N specified in the schema will end up meaning that any unsigned value N specified in the schema will end up
representing 1<<N, or if you don't specify values at all, you'll get the representing 1<<N, or if you don't specify values at all, you'll get the

View File

@ -1113,8 +1113,13 @@ class GoGenerator : public BaseGenerator {
if (IsString(field.value.type)) { if (IsString(field.value.type)) {
code += "\t" + offset + " := flatbuffers.UOffsetT(0)\n"; code += "\t" + offset + " := flatbuffers.UOffsetT(0)\n";
code += "\tif t." + field_field + " != \"\" {\n"; code += "\tif t." + field_field + " != \"\" {\n";
code += "\t\t" + offset + " = builder.CreateString(t." + field_field + if (field.shared) {
")\n"; code += "\t\t" + offset + " = builder.CreateSharedString(t." + field_field +
")\n";
} else {
code += "\t\t" + offset + " = builder.CreateString(t." + field_field +
")\n";
}
code += "\t}\n"; code += "\t}\n";
} else if (IsVector(field.value.type) && } else if (IsVector(field.value.type) &&
field.value.type.element == BASE_TYPE_UCHAR && field.value.type.element == BASE_TYPE_UCHAR &&