-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Libraries/Connect/Internal/Streaming/ClientOnlyStreamValidation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2022-2024 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
/// Namespace for performing client-only stream validation. | ||
enum ClientOnlyStreamValidation { | ||
/// Applies some validations which are only relevant for client-only streams. | ||
/// | ||
/// Should be called after all values have been received over a client stream. Since client | ||
/// streams only expect 1 result, all values returned from the server should be buffered before | ||
/// being validated here and returned to the caller. | ||
/// | ||
/// - parameter results: The buffered list of results to validate. | ||
/// | ||
/// - returns: The list of stream results which should be returned to the caller. | ||
static func validatedFinalClientStreamResults<T>( | ||
_ results: [StreamResult<T>] | ||
) -> [StreamResult<T>] { | ||
var messageCount = 0 | ||
for result in results { | ||
switch result { | ||
case .headers: | ||
continue | ||
case .message: | ||
messageCount += 1 | ||
case .complete(let code, _, _): | ||
if code != .ok { | ||
return results | ||
} | ||
} | ||
} | ||
|
||
if messageCount < 1 { | ||
return [ | ||
.complete( | ||
code: .internalError, error: ConnectError( | ||
code: .unimplemented, message: "unary stream has no messages" | ||
), trailers: nil | ||
), | ||
] | ||
} else if messageCount > 1 { | ||
return [ | ||
.complete( | ||
code: .internalError, error: ConnectError( | ||
code: .unimplemented, message: "unary stream has multiple messages" | ||
), trailers: nil | ||
), | ||
] | ||
} else { | ||
return results | ||
} | ||
} | ||
} |
54 changes: 0 additions & 54 deletions
54
Libraries/Connect/Internal/Streaming/StreamResult+ClientOnlyStream.swift
This file was deleted.
Oops, something went wrong.