| 737 | } |
| 738 | |
| 739 | CheckedError Parser::ParseField(StructDef &struct_def) { |
| 740 | std::string name = attribute_; |
| 741 | |
| 742 | if (LookupCreateStruct(name, false, false)) |
| 743 | return Error("field name can not be the same as table/struct name"); |
| 744 | |
| 745 | if (!IsLowerSnakeCase(name)) { |
| 746 | Warning("field names should be lowercase snake_case, got: " + name); |
| 747 | } |
| 748 | |
| 749 | std::vector<std::string> dc = doc_comment_; |
| 750 | EXPECT(kTokenIdentifier); |
| 751 | EXPECT(':'); |
| 752 | Type type; |
| 753 | ECHECK(ParseType(type)); |
| 754 | |
| 755 | if (struct_def.fixed) { |
| 756 | auto valid = IsScalar(type.base_type) || IsStruct(type); |
| 757 | if (!valid && IsArray(type)) { |
| 758 | const auto &elem_type = type.VectorType(); |
| 759 | valid |= IsScalar(elem_type.base_type) || IsStruct(elem_type); |
| 760 | } |
| 761 | if (!valid) |
| 762 | return Error("structs may contain only scalar or struct fields"); |
| 763 | } |
| 764 | |
| 765 | if (!struct_def.fixed && IsArray(type)) |
| 766 | return Error("fixed-length array in table must be wrapped in struct"); |
| 767 | |
| 768 | if (IsArray(type)) { |
| 769 | advanced_features_ |= reflection::AdvancedArrayFeatures; |
| 770 | if (!SupportsAdvancedArrayFeatures()) { |
| 771 | return Error( |
| 772 | "Arrays are not yet supported in all " |
| 773 | "the specified programming languages."); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | FieldDef *typefield = nullptr; |
| 778 | if (type.base_type == BASE_TYPE_UNION) { |
| 779 | // For union fields, add a second auto-generated field to hold the type, |
| 780 | // with a special suffix. |
| 781 | ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), |
| 782 | type.enum_def->underlying_type, &typefield)); |
| 783 | } else if (IsVector(type) && type.element == BASE_TYPE_UNION) { |
| 784 | advanced_features_ |= reflection::AdvancedUnionFeatures; |
| 785 | // Only cpp, js and ts supports the union vector feature so far. |
| 786 | if (!SupportsAdvancedUnionFeatures()) { |
| 787 | return Error( |
| 788 | "Vectors of unions are not yet supported in at least one of " |
| 789 | "the specified programming languages."); |
| 790 | } |
| 791 | // For vector of union fields, add a second auto-generated vector field to |
| 792 | // hold the types, with a special suffix. |
| 793 | Type union_vector(BASE_TYPE_VECTOR, nullptr, type.enum_def); |
| 794 | union_vector.element = BASE_TYPE_UTYPE; |
| 795 | ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), union_vector, |
| 796 | &typefield)); |
nothing calls this directly
no test coverage detected