Skip to content

Commit

Permalink
Support marking with codable to support "Codable" on the generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Nov 25, 2024
1 parent 65334e3 commit af04a90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/parser/dflatc/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct Enum: Decodable {
var underlyingType: String?
var fields: [EnumVal]
var generated: Bool
var attributes: [[String: String]]
}

enum ElementTypeEnum: String, Decodable {
Expand Down Expand Up @@ -173,6 +174,18 @@ extension Field {
}
}

extension Enum {
var isCodable: Bool {
attributes.contains { $0["codable"] != nil }
}
}

extension Struct {
var isCodable: Bool {
attributes.contains { $0["codable"] != nil }
}
}

extension Enum {
func findEnumVal(_ value: Int) -> EnumVal? {
for field in fields {
Expand Down Expand Up @@ -201,7 +214,7 @@ func SetNamespace(_ namespace: [String], previous pns: inout [String], code: ino

func GenEnumDataModel(_ enumDef: Enum, code: inout String) {
code +=
"\npublic enum \(enumDef.name): \(SwiftType[enumDef.underlyingType!]!), DflatFriendlyValue, CaseIterable {\n"
"\npublic enum \(enumDef.name): \(SwiftType[enumDef.underlyingType!]!), DflatFriendlyValue, CaseIterable\(enumDef.isCodable ? ", Codable" : "") {\n"
for field in enumDef.fields {
code += " case \(field.name.firstLowercasedIfNotAllCaps()) = \(field.value)\n"
}
Expand Down Expand Up @@ -459,7 +472,7 @@ func GetStructDeserializer(_ structDef: Struct) -> String {
}

func GenStructDataModel(_ structDef: Struct, code: inout String) {
code += "\npublic struct \(structDef.name): Equatable, FlatBuffersDecodable {\n"
code += "\npublic struct \(structDef.name): Equatable, FlatBuffersDecodable\(structDef.isCodable ? ", Codable" : "") {\n"
for field in structDef.fields {
guard IsDataField(field) else { continue }
code += " public var \(field.name): \(GetFieldType(field))\n"
Expand Down Expand Up @@ -506,7 +519,7 @@ func GenStructDataModel(_ structDef: Struct, code: inout String) {

func GenRootDataModel(_ structDef: Struct, code: inout String) {
code +=
"\npublic final class \(structDef.name): Dflat.Atom, SQLiteDflat.SQLiteAtom, FlatBuffersDecodable, Equatable {\n"
"\npublic final class \(structDef.name): Dflat.Atom, SQLiteDflat.SQLiteAtom, FlatBuffersDecodable, Equatable\(structDef.isCodable ? ", Codable" : "") {\n"
code += " public static func == (lhs: \(structDef.name), rhs: \(structDef.name)) -> Bool {\n"
for field in structDef.fields {
guard IsDataField(field) else { continue }
Expand Down
2 changes: 2 additions & 0 deletions src/parser/dflats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const std::string GenUnion(const flatbuffers::EnumDef &enum_def) {
json += "\"generated\": ";
json += (enum_def.generated ? "true, " : "false, ");
json += "\"namespace\": [" + GenNamespace(*enum_def.defined_namespace) + "], ";
json += GenAttributes(enum_def.attributes) + ", ";
if (!enum_def.is_union) {
json += std::string("\"underlying_type\": \"") + idl_types[enum_def.underlying_type.base_type] + "\", ";
}
Expand Down Expand Up @@ -265,6 +266,7 @@ int main(int argc, const char **argv) {
parser->known_attributes_["primary"] = true;
parser->known_attributes_["indexed"] = true;
parser->known_attributes_["unique"] = true;
parser->known_attributes_["codable"] = true;
parser->known_attributes_["v"] = true;
ParseFile(*parser.get(), filename, contents, include_directories);

Expand Down

0 comments on commit af04a90

Please sign in to comment.