| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package serialization | 5 package serialization |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "mojo/public/go/bindings" | 9 "mojo/public/go/bindings" |
| 10 "mojom/mojom_parser/generated/mojom_files" | 10 "mojom/mojom_parser/generated/mojom_files" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 61 // translateMojomFile translates from a mojom.MojomFile (the pure Go | 61 // translateMojomFile translates from a mojom.MojomFile (the pure Go |
| 62 // representation used by the parser) to a mojom_files.MojomFile (the | 62 // representation used by the parser) to a mojom_files.MojomFile (the |
| 63 // Mojo Go representation used for serialization.) | 63 // Mojo Go representation used for serialization.) |
| 64 func translateMojomFile(f *mojom.MojomFile) (file mojom_files.MojomFile) { | 64 func translateMojomFile(f *mojom.MojomFile) (file mojom_files.MojomFile) { |
| 65 // file_name field | 65 // file_name field |
| 66 file.FileName = f.CanonicalFileName | 66 file.FileName = f.CanonicalFileName |
| 67 | 67 |
| 68 // module_namespace field | 68 // module_namespace field |
| 69 » file.ModuleNamespace = newString(f.ModuleNamespace) | 69 » file.ModuleNamespace = &f.ModuleNamespace |
| 70 | 70 |
| 71 // attributes field | 71 // attributes field |
| 72 if f.Attributes != nil { | 72 if f.Attributes != nil { |
| 73 file.Attributes = new([]mojom_types.Attribute) | 73 file.Attributes = new([]mojom_types.Attribute) |
| 74 for _, attr := range f.Attributes.List { | 74 for _, attr := range f.Attributes.List { |
| 75 *(file.Attributes) = append(*(file.Attributes), translat
eMojomAttribute(&attr)) | 75 *(file.Attributes) = append(*(file.Attributes), translat
eMojomAttribute(&attr)) |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 // imports field | 79 // imports field |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 376 } |
| 377 | 377 |
| 378 func translateMapType(mapType mojom.MapTypeRef) *mojom_types.TypeMapType { | 378 func translateMapType(mapType mojom.MapTypeRef) *mojom_types.TypeMapType { |
| 379 return &mojom_types.TypeMapType{mojom_types.MapType{ | 379 return &mojom_types.TypeMapType{mojom_types.MapType{ |
| 380 Nullable: mapType.Nullable(), | 380 Nullable: mapType.Nullable(), |
| 381 KeyType: translateTypeRef(mapType.KeyType()), | 381 KeyType: translateTypeRef(mapType.KeyType()), |
| 382 ValueType: translateTypeRef(mapType.ValueType())}} | 382 ValueType: translateTypeRef(mapType.ValueType())}} |
| 383 } | 383 } |
| 384 | 384 |
| 385 func translateUserTypeRef(userType *mojom.UserTypeRef) *mojom_types.TypeTypeRefe
rence { | 385 func translateUserTypeRef(userType *mojom.UserTypeRef) *mojom_types.TypeTypeRefe
rence { |
| 386 » typeKey := newString(userType.ResolvedType().TypeKey()) | 386 » typeKey := stringPointer(userType.ResolvedType().TypeKey()) |
| 387 » identifier := newString(userType.Identifier()) | 387 » identifier := stringPointer(userType.Identifier()) |
| 388 return &mojom_types.TypeTypeReference{mojom_types.TypeReference{ | 388 return &mojom_types.TypeTypeReference{mojom_types.TypeReference{ |
| 389 Nullable: userType.Nullable(), | 389 Nullable: userType.Nullable(), |
| 390 IsInterfaceRequest: userType.IsInterfaceRequest(), | 390 IsInterfaceRequest: userType.IsInterfaceRequest(), |
| 391 Identifier: identifier, | 391 Identifier: identifier, |
| 392 TypeKey: typeKey}} | 392 TypeKey: typeKey}} |
| 393 } | 393 } |
| 394 | 394 |
| 395 func translateValueRef(valueRef mojom.ValueRef) mojom_types.Value { | 395 func translateValueRef(valueRef mojom.ValueRef) mojom_types.Value { |
| 396 switch valueRef := valueRef.(type) { | 396 switch valueRef := valueRef.(type) { |
| 397 case mojom.LiteralValue: | 397 case mojom.LiteralValue: |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity | 451 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity |
| 452 case mojom.SimpleTypeDouble_NAN: | 452 case mojom.SimpleTypeDouble_NAN: |
| 453 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity | 453 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity |
| 454 default: | 454 default: |
| 455 panic(fmt.Sprintf("Unrecognized BuiltInConstantValue %v", t)) | 455 panic(fmt.Sprintf("Unrecognized BuiltInConstantValue %v", t)) |
| 456 } | 456 } |
| 457 return &builtInValue | 457 return &builtInValue |
| 458 } | 458 } |
| 459 | 459 |
| 460 func translateUserValueRef(r *mojom.UserValueRef) *mojom_types.ValueUserValueRef
erence { | 460 func translateUserValueRef(r *mojom.UserValueRef) *mojom_types.ValueUserValueRef
erence { |
| 461 » valueKey := newString(r.ResolvedDeclaredValue().ValueKey()) | 461 » valueKey := stringPointer(r.ResolvedDeclaredValue().ValueKey()) |
| 462 return &mojom_types.ValueUserValueReference{mojom_types.UserValueReferen
ce{ | 462 return &mojom_types.ValueUserValueReference{mojom_types.UserValueReferen
ce{ |
| 463 Identifier: r.Identifier(), | 463 Identifier: r.Identifier(), |
| 464 ValueKey: valueKey, | 464 ValueKey: valueKey, |
| 465 ResolvedConcreteValue: translateConcreteValue(r.ResolvedConcrete
Value())}} | 465 ResolvedConcreteValue: translateConcreteValue(r.ResolvedConcrete
Value())}} |
| 466 } | 466 } |
| 467 | 467 |
| 468 func translateConcreteValue(v mojom.ConcreteValue) mojom_types.Value { | 468 func translateConcreteValue(v mojom.ConcreteValue) mojom_types.Value { |
| 469 switch t := v.(type) { | 469 switch t := v.(type) { |
| 470 case mojom.BuiltInConstantValue: | 470 case mojom.BuiltInConstantValue: |
| 471 return translateBuiltInConstantValue(t) | 471 return translateBuiltInConstantValue(t) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 493 declData.Attributes = new([]mojom_types.Attribute) | 493 declData.Attributes = new([]mojom_types.Attribute) |
| 494 for _, attr := range d.Attributes().List { | 494 for _, attr := range d.Attributes().List { |
| 495 *(declData.Attributes) = append(*(declData.Attributes),
translateMojomAttribute(&attr)) | 495 *(declData.Attributes) = append(*(declData.Attributes),
translateMojomAttribute(&attr)) |
| 496 } | 496 } |
| 497 } | 497 } |
| 498 | 498 |
| 499 // min_version field | 499 // min_version field |
| 500 // TODO(rudominer) Eliminate the min_version field from struct Declarati
onData | 500 // TODO(rudominer) Eliminate the min_version field from struct Declarati
onData |
| 501 | 501 |
| 502 // short_name field | 502 // short_name field |
| 503 » declData.ShortName = newString(d.SimpleName()) | 503 » declData.ShortName = stringPointer(d.SimpleName()) |
| 504 | 504 |
| 505 // full_identifier field | 505 // full_identifier field |
| 506 » declData.FullIdentifier = newString(d.FullyQualifiedName()) | 506 » declData.FullIdentifier = stringPointer(d.FullyQualifiedName()) |
| 507 | 507 |
| 508 // declared_ordinal field | 508 // declared_ordinal field |
| 509 if d.DeclaredOrdinal() < 0 { | 509 if d.DeclaredOrdinal() < 0 { |
| 510 declData.DeclaredOrdinal = -1 | 510 declData.DeclaredOrdinal = -1 |
| 511 } else { | 511 } else { |
| 512 declData.DeclaredOrdinal = int32(d.DeclaredOrdinal()) | 512 declData.DeclaredOrdinal = int32(d.DeclaredOrdinal()) |
| 513 } | 513 } |
| 514 | 514 |
| 515 // declaration_order | 515 // declaration_order |
| 516 // TODO(rudominer) DeclarationOrder is currently not populated. | 516 // TODO(rudominer) DeclarationOrder is currently not populated. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 545 } | 545 } |
| 546 } | 546 } |
| 547 return &declarations | 547 return &declarations |
| 548 } | 548 } |
| 549 | 549 |
| 550 func translateMojomAttribute(a *mojom.MojomAttribute) (attribute mojom_types.Att
ribute) { | 550 func translateMojomAttribute(a *mojom.MojomAttribute) (attribute mojom_types.Att
ribute) { |
| 551 // TODO(rudominer) Improve representation of attribute values. | 551 // TODO(rudominer) Improve representation of attribute values. |
| 552 return mojom_types.Attribute{a.Key, fmt.Sprintf("%v", a.Value.Value())} | 552 return mojom_types.Attribute{a.Key, fmt.Sprintf("%v", a.Value.Value())} |
| 553 } | 553 } |
| 554 | 554 |
| 555 // newString is a convenience function for creating a pointer to a string whose
value | 555 // stringPointer is a convenience function for creating a pointer to a string wh
ose value |
| 556 // is the specified string. It is necessary to create pointers to strings becaus
e | 556 // is the specified string. It may be used in situations where the compiler will |
| 557 // that is how the Mojom type string? (i.e. nullable string) is represented in | 557 // not allow you to take the address of a string value directly, such as the |
| 558 // return value of a function. It is necessary to create pointers to strings bec
ause |
| 559 // that is how the Mojom type |string?| (i.e. nullable string) is represented in |
| 558 // in the Mojom Go bindings. | 560 // in the Mojom Go bindings. |
| 559 func newString(s string) *string { | 561 func stringPointer(s string) *string { |
| 560 » t := new(string) | 562 » return &s |
| 561 » *t = s | |
| 562 » return t | |
| 563 } | 563 } |
| OLD | NEW |