Skip to content

Commit

Permalink
Add support for RPC status (#331)
Browse files Browse the repository at this point in the history
* Add support for RPC status

* Add test

* Move function
  • Loading branch information
SebastianThiebaud authored and MrMage committed Nov 9, 2018
1 parent 2fd06dc commit b6dc500
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions Sources/CgRPC/shim/cgrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ void cgrpc_metadata_array_unref_fields(cgrpc_metadata_array *array);
size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
char *cgrpc_metadata_array_copy_key_at_index(cgrpc_metadata_array *array, size_t index);
char *cgrpc_metadata_array_copy_value_at_index(cgrpc_metadata_array *array, size_t index);
cgrpc_byte_buffer *cgrpc_metadata_array_copy_data_value_at_index(cgrpc_metadata_array *array, size_t index);
void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
cgrpc_metadata_array *cgrpc_metadata_array_copy(cgrpc_metadata_array *src);
void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
Expand Down
5 changes: 5 additions & 0 deletions Sources/CgRPC/shim/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ char *cgrpc_metadata_array_copy_value_at_index(cgrpc_metadata_array *array, size
return str;
}

cgrpc_byte_buffer *cgrpc_metadata_array_copy_data_value_at_index(cgrpc_metadata_array *array, size_t index) {
size_t length = GRPC_SLICE_LENGTH(array->metadata[index].value);
return cgrpc_byte_buffer_create_by_copying_data(GRPC_SLICE_START_PTR(array->metadata[index].value), length);
}

void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *destination,
cgrpc_metadata_array *source) {
destination->count = source->count;
Expand Down
11 changes: 10 additions & 1 deletion Sources/SwiftGRPC/Core/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class Metadata {
defer { cgrpc_free_copied_string(valueData) }
return String(cString: valueData, encoding: String.Encoding.utf8)
}

public func add(key: String, value: String) throws {
if !ownsFields {
throw Error.doesNotOwnFields
Expand Down Expand Up @@ -129,4 +129,13 @@ extension Metadata {

return nil
}

public func data(forKey key: String) -> Data? {
for index in 0..<count() {
guard self.key(index) == key else { continue }
let byteBuffer = ByteBuffer(underlyingByteBuffer: cgrpc_metadata_array_copy_data_value_at_index(underlyingArray, index))
return byteBuffer.data()
}
return nil
}
}
5 changes: 5 additions & 0 deletions Tests/SwiftGRPCTests/MetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ class MetadataTests: XCTestCase {
let metadata = try! Metadata(["foo": "bar"])
XCTAssertEqual(["foo": "bar"], metadata.copy().dictionaryRepresentation)
}

func testExtractData() {
let metadata = try! Metadata(["foo": "bar"])
XCTAssertEqual("bar".data(using: .utf8), metadata.data(forKey: "foo"))
}
}

0 comments on commit b6dc500

Please sign in to comment.