processObjectType converts object types to Schema.Struct or Schema.Class.
(t *checker.Type, ctx processingContext)
| 435 | |
| 436 | // processObjectType converts object types to Schema.Struct or Schema.Class. |
| 437 | func (g *StructuralSchemaGen) processObjectType(t *checker.Type, ctx processingContext) (*ast.Node, bool, error) { |
| 438 | properties := g.Checker.GetPropertiesOfType(t) |
| 439 | propertyAssignments := make([]*ast.Node, 0, len(properties)) |
| 440 | |
| 441 | for _, prop := range properties { |
| 442 | propName := g.Checker.SymbolToString(prop) |
| 443 | propType := g.Checker.GetTypeOfSymbol(prop) |
| 444 | |
| 445 | isOptional := prop.Flags&ast.SymbolFlagsOptional != 0 |
| 446 | |
| 447 | var schemaExpr *ast.Node |
| 448 | if isOptional { |
| 449 | innerExpr, err := g.processType(propType, ctx) |
| 450 | if err != nil { |
| 451 | return nil, false, err |
| 452 | } |
| 453 | schemaExpr = g.createApiCall("optional", []*ast.Node{innerExpr}) |
| 454 | } else { |
| 455 | var err error |
| 456 | schemaExpr, err = g.processType(propType, ctx) |
| 457 | if err != nil { |
| 458 | return nil, false, err |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Create property name node |
| 463 | var propNameNode *ast.Node |
| 464 | if validIdentifierRegex.MatchString(propName) { |
| 465 | propNameNode = g.Tracker.NewIdentifier(propName) |
| 466 | } else { |
| 467 | propNameNode = g.Tracker.NewStringLiteral(propName, 0) |
| 468 | } |
| 469 | |
| 470 | assignment := g.Tracker.NewPropertyAssignment(nil, propNameNode, nil, nil, schemaExpr) |
| 471 | propertyAssignments = append(propertyAssignments, assignment) |
| 472 | } |
| 473 | |
| 474 | // Handle index signatures |
| 475 | indexInfos := checker.Checker_getIndexInfosOfType(g.Checker, t) |
| 476 | args := []*ast.Node{ |
| 477 | g.Tracker.NewObjectLiteralExpression(g.Tracker.NewNodeList(propertyAssignments), len(propertyAssignments) > 0), |
| 478 | } |
| 479 | |
| 480 | type recordEntry struct { |
| 481 | key *ast.Node |
| 482 | value *ast.Node |
| 483 | } |
| 484 | var records []recordEntry |
| 485 | for _, info := range indexInfos { |
| 486 | keyType := checker.IndexInfo_keyType(info) |
| 487 | valueType := checker.IndexInfo_valueType(info) |
| 488 | |
| 489 | keySchema, err := g.processType(keyType, ctx) |
| 490 | if err != nil { |
| 491 | return nil, false, err |
| 492 | } |
| 493 | valueSchema, err := g.processType(valueType, ctx) |
| 494 | if err != nil { |
no test coverage detected