Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Typescript transformation - etcd #3451

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,52 @@
*
* SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors
* SPDX-License-Identifier: EPL-2.0
*/
*/

const EtcdFacade = Java.type("org.eclipse.dirigible.components.api.etcd.EtcdFacade");

export function getClient() {
export function getClient(): Client {
const native = EtcdFacade.getClient();
var client = new Client(native);
return client;
};

class Client {

constructor(private native) { }
constructor(private native: any) {
this.native = native;
}

putStringValue(key, value) {
if (typeof (key) !== 'string') {
return new Error('Key is not a string.');
} else if (typeof (value) !== 'string') {
return new Error('Value is not a string.');
} else {
this.native.put(StringToByteSequence(key), StringToByteSequence(value));
}
putStringValue(key: string, value: string): void {
this.native.put(StringToByteSequence(key), StringToByteSequence(value));
}

putByteArrayValue(key, value) {
if (typeof (key) !== 'string') {
return new Error('Key is not a string.');
} else if (!(value instanceof Int8Array)) {
return new Error('Value is not a Int8Array.');
} else {
this.native.put(StringToByteSequence(key), ByteArrayToByteSequence(value));
}
putByteArrayValue(key: string, value: Int8Array): void {
this.native.put(StringToByteSequence(key), ByteArrayToByteSequence(value));
}

getHeader(key) {
getHeader(key: string): Header {
return this.get(key, this.native).getHeader();
}

getKvsStringValue(key) {
if (typeof (key) !== 'string') {
return new Error('Key is not a string.');
} else {
return this.get(key, this.native).getKvsString();
}
getKvsStringValue(key: string): {[key: string]: any} {
return this.get(key, this.native).getKvsString();
}

getKvsByteArrayValue(key) {
if (typeof (key) !== 'string') {
return new Error('Key is not a string.');
} else {
getKvsByteArrayValue(key: string): {[key: string]: Int8Array}{
return this.get(key, this.native).getKvsByteArray();
}
}

getCount(key) {
getCount(key: string): number {
return this.get(key, this.native).getCount();
}

delete(key) {
if (typeof (key) !== 'string') {
return new Error('Key is not a string.');
} else {
this.native.delete(StringToByteSequence(key));
}
delete(key: string): void {
this.native.delete(StringToByteSequence(key));
}

private get(key, native) {
//? is native needed as a parameter?
private get(key: string, native?: any): GetResponse {
var etcdCompletableFuture = native.get(StringToByteSequence(key));
var native = etcdCompletableFuture.get();
var etcdGetReponse = new GetResponse(native);
Expand All @@ -86,66 +65,68 @@ class GetResponse {

constructor(private native) { }

getHeader() {
getHeader(): Header {
var native = this.native.getHeader();
var header = new Header(native);
return header;
}

getKvsString() {
getKvsString(): {[key: string]: string} {
return KeyValueObjectString(this.native.getKvs());
}

getKvsByteArray() {
getKvsByteArray(): {[key: string]: Int8Array}{
return KeyValueObjectByteArray(this.native.getKvs());
}
getCount() {
getCount(): number {
return this.native.getCount();
}
}

class Header {
constructor(private native) { }
constructor(private native) {
this.native = native;
}

getRevision() {
getRevision(): number {
return this.native.getRevision();
}

getClusterId() {
getClusterId(): number {
return this.native.getClusterId();
}

getMemberId() {
getMemberId(): number {
return this.native.getMemberId();
}

getRaftTerm() {
getRaftTerm(): number {
return this.native.getRaftTerm();
}
}

function StringToByteSequence(str) {
function StringToByteSequence(str: string): any /* ByteSequence */ {
return EtcdFacade.stringToByteSequence(str);
}

function ByteArrayToByteSequence(arr) {
function ByteArrayToByteSequence(arr: Int8Array): any /* ByteSequence */{
return EtcdFacade.byteArrayToByteSequence(arr);
}

function ByteSequenceToString(value) {
function ByteSequenceToString(value): string {
return EtcdFacade.byteSequenceToString(value);
}

function KeyValueObjectString(kvsList) {
function KeyValueObjectString(kvsList): {[key: string]: string} {
return KeyValueObject(kvsList, ByteSequenceToString);
}

function KeyValueObjectByteArray(kvsList) {
function KeyValueObjectByteArray(kvsList): {[key: string]: Int8Array} {
return KeyValueObject(kvsList, BytesToArray);
}

function KeyValueObject(kvsList, func) {
var kvObject = {};
function KeyValueObject(kvsList, func: Function): {[key: string]: any} {
var kvObject: {[key: string]: any} = {};
kvsList.forEach(kvs => {
var key = ByteSequenceToString(kvs.getKey());
var value = func(kvs.getValue());
Expand All @@ -155,7 +136,7 @@ function KeyValueObject(kvsList, func) {
return kvObject;
}

function BytesToArray(value) {
function BytesToArray(value): Int8Array {
var array = value.getBytes();
var result = new Int8Array(array.length);
for (var i = 0; i < array.length; i++) {
Expand Down