forked from Henry-Hiles/nexus
wip go 2
This commit is contained in:
parent
77d9f9bdc1
commit
2a86bdafeb
12 changed files with 156 additions and 199 deletions
41
lib/helpers/extensions/gomuks_buffer.dart
Normal file
41
lib/helpers/extensions/gomuks_buffer.dart
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import "dart:convert";
|
||||
import "dart:ffi";
|
||||
import "dart:typed_data";
|
||||
|
||||
import "package:ffi/ffi.dart";
|
||||
import "package:nexus/src/third_party/gomuks.g.dart";
|
||||
|
||||
extension GomuksBufferX on GomuksBuffer {
|
||||
/// Safely converts the Go buffer into a Dart `Uint8List`
|
||||
Uint8List toBytes() {
|
||||
if (base == nullptr || length <= 0) return Uint8List(0);
|
||||
return base.asTypedList(length);
|
||||
}
|
||||
|
||||
/// Decodes the bytes as JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
final bytes = toBytes();
|
||||
if (bytes.isEmpty) return {};
|
||||
return jsonDecode(utf8.decode(bytes));
|
||||
}
|
||||
}
|
||||
|
||||
extension JsonToGomuksBuffer on Map<String, dynamic> {
|
||||
GomuksBuffer toGomuksBuffer() {
|
||||
final jsonString = json.encode(this);
|
||||
final bytes = utf8.encode(jsonString);
|
||||
|
||||
final dataPtr = calloc<Uint8>(bytes.length);
|
||||
dataPtr.asTypedList(bytes.length).setAll(0, bytes);
|
||||
|
||||
final bufPtr = calloc<GomuksBuffer>();
|
||||
bufPtr.ref.base = dataPtr;
|
||||
bufPtr.ref.length = bytes.length;
|
||||
|
||||
final bufByValue = bufPtr.ref;
|
||||
|
||||
calloc.free(bufPtr);
|
||||
|
||||
return bufByValue;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue