initStreamData initializes the streaming payload data structures and methods.
(data *MethodData, m *expr.MethodExpr, vname, rname, resultRef string, scope *codegen.NameScope)
| 1415 | |
| 1416 | // initStreamData initializes the streaming payload data structures and methods. |
| 1417 | func (d *ServicesData) initStreamData(data *MethodData, m *expr.MethodExpr, vname, rname, resultRef string, scope *codegen.NameScope) { |
| 1418 | if !m.IsStreaming() && !m.HasMixedResults() { |
| 1419 | return |
| 1420 | } |
| 1421 | var ( |
| 1422 | spayloadName string |
| 1423 | spayloadRef string |
| 1424 | spayloadDef string |
| 1425 | spayloadDesc string |
| 1426 | spayloadEx any |
| 1427 | srname = rname // streaming result name |
| 1428 | srref = resultRef // streaming result ref |
| 1429 | ) |
| 1430 | |
| 1431 | // If StreamingResult is different from Result, use it for streaming |
| 1432 | if m.HasMixedResults() && m.StreamingResult != nil && m.StreamingResult.Type != expr.Empty { |
| 1433 | srname = scope.GoTypeName(m.StreamingResult) |
| 1434 | srref = scope.GoTypeRef(m.StreamingResult) |
| 1435 | data.StreamingResult = srname |
| 1436 | data.StreamingResultRef = srref |
| 1437 | if dt, ok := m.StreamingResult.Type.(expr.UserType); ok { |
| 1438 | data.StreamingResultDef = scope.GoTypeDef(dt.Attribute(), false, true) |
| 1439 | } |
| 1440 | data.StreamingResultDesc = m.StreamingResult.Description |
| 1441 | if data.StreamingResultDesc == "" { |
| 1442 | data.StreamingResultDesc = fmt.Sprintf("%s is the streaming result type of the %s service %s method.", |
| 1443 | srname, m.Service.Name, m.Name) |
| 1444 | } |
| 1445 | data.StreamingResultEx = m.StreamingResult.Example(d.Root.API.ExampleGenerator) |
| 1446 | } |
| 1447 | |
| 1448 | if m.StreamingPayload != nil && m.StreamingPayload.Type != expr.Empty { |
| 1449 | spayloadName = scope.GoTypeName(m.StreamingPayload) |
| 1450 | spayloadRef = scope.GoTypeRef(m.StreamingPayload) |
| 1451 | if dt, ok := m.StreamingPayload.Type.(expr.UserType); ok { |
| 1452 | spayloadDef = scope.GoTypeDef(dt.Attribute(), false, true) |
| 1453 | } |
| 1454 | spayloadDesc = m.StreamingPayload.Description |
| 1455 | if spayloadDesc == "" { |
| 1456 | spayloadDesc = fmt.Sprintf("%s is the streaming payload type of the %s service %s method.", |
| 1457 | spayloadName, m.Service.Name, m.Name) |
| 1458 | } |
| 1459 | spayloadEx = m.StreamingPayload.Example(d.Root.API.ExampleGenerator) |
| 1460 | } |
| 1461 | // For JSON-RPC WebSocket: |
| 1462 | // - Client streaming (no result streaming): no endpoint struct needed, just payload |
| 1463 | // - Bidirectional streaming: endpoint struct needed for both payload and stream |
| 1464 | endpointStruct := vname + "EndpointInput" |
| 1465 | if data.IsJSONRPC && m.IsStreaming() && !data.IsJSONRPCSSE && m.Stream == expr.ClientStreamKind { |
| 1466 | endpointStruct = "" |
| 1467 | } |
| 1468 | // For mixed results with SSE, treat as server streaming |
| 1469 | streamKind := m.Stream |
| 1470 | if m.HasMixedResults() && !m.IsStreaming() { |
| 1471 | // Mixed results with SSE should be treated as server streaming |
| 1472 | streamKind = expr.ServerStreamKind |
| 1473 | } |
| 1474 | svrStream := &StreamData{ |
no test coverage detected