Skip to content

Commit eed1f58

Browse files
Address comments
1 parent 7b3fe10 commit eed1f58

File tree

10 files changed

+65
-70
lines changed

10 files changed

+65
-70
lines changed

templates/android/library/src/main/java/io/appwrite/ID.kt.twig

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@ import java.time.Instant
44
import kotlin.math.floor
55
import kotlin.random.Random
66

7-
// Generate an hex ID based on timestamp
8-
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9-
private fun hexTimestamp(): String {
10-
val currentTimestamp = Instant.now().toEpochMilli() / 1000.0
11-
val secondsPart = floor(currentTimestamp).toLong()
12-
val microsecondsPart = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
13-
14-
// Convert both parts to hexadecimal format
15-
val hexTimestamp = "%08x%05x".format(secondsPart, microsecondsPart)
16-
return hexTimestamp
17-
}
18-
197
class ID {
208
companion object {
9+
// Generate an hex ID based on timestamp
10+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
11+
private fun hexTimestamp(): String {
12+
val now = Instant.now().toEpochMilli() / 1000.0
13+
val secondsSinceEpoch = floor(now).toLong()
14+
val msecs = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
15+
16+
// Convert both parts to hexadecimal format
17+
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, msecs)
18+
return hexTimestamp
19+
}
20+
2121
fun custom(id: String): String
2222
= id
2323

2424
// Generate a unique ID with padding to have a longer ID
25-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2625
fun unique(padding: Int = 7): String {
2726
val baseId = hexTimestamp()
2827
val randomPadding = (1..padding)

templates/dart/lib/id.dart.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class ID {
1515
}
1616

1717
// Generate a unique ID with padding to have a longer ID
18-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1918
static String unique({int padding = 7}) {
2019
String id = _hexTimestamp();
2120

templates/deno/src/id.ts.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export class ID {
1717
}
1818

1919
// Generate a unique ID with padding to have a longer ID
20-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2120
public static unique(padding: number = 7): string {
2221
const baseId = ID.#hexTimestamp();
2322
let randomPadding = '';

templates/dotnet/src/Appwrite/ID.cs.twig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@ namespace {{ spec.title | caseUcfirst }}
66
{
77
// Generate an hex ID based on timestamp
88
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9-
private static string hexTimestamp()
9+
private static string HexTimestamp()
1010
{
1111
var now = DateTime.UtcNow;
12-
var secondsSinceEpoch = (long)(now - new DateTime(1970, 1, 1)).TotalSeconds;
13-
var msecs = (now - new DateTime(1970, 1, 1)).TotalMilliseconds - secondsSinceEpoch * 1000;
12+
var epoch = (now - new DateTime(1970, 1, 1));
13+
var secondsSinceEpoch = (long)epoch.TotalSeconds;
14+
var msecs = epoch.TotalMilliseconds - secondsSinceEpoch * 1000;
1415
var microsecondsPart = (long)(msecs * 1000); // Convert milliseconds to microseconds
1516

1617
// Convert to hexadecimal
17-
var hexTimestamp = secondsSinceEpoch.ToString("x") + microsecondsPart.ToString("x").PadLeft(5, '0');
18+
var hexTimestamp = secondsSinceEpoch.ToString("x")
19+
+ microsecondsPart.ToString("x").PadLeft(5, '0');
1820
return hexTimestamp;
1921
}
2022

2123
// Generate a unique ID with padding to have a longer ID
22-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2324
public static string Unique(int padding = 7)
2425
{
2526
var random = new Random();
26-
var baseId = hexTimestamp();
27+
var baseId = HexTimestamp();
2728
var randomPadding = "";
2829

2930
for (int i = 0; i < padding; i++)

templates/kotlin/src/main/kotlin/io/appwrite/ID.kt.twig

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import java.time.Instant
44
import kotlin.math.floor
55
import kotlin.random.Random
66

7-
// Generate an hex ID based on timestamp
8-
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9-
private fun hexTimestamp(): String {
10-
val currentTimestamp = Instant.now().toEpochMilli() / 1000.0
11-
val secondsPart = floor(currentTimestamp).toLong()
12-
val microsecondsPart = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
7+
class ID {
8+
// Generate an hex ID based on timestamp
9+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
10+
private fun hexTimestamp(): String {
11+
val now = Instant.now().toEpochMilli() / 1000.0
12+
val secondsSinceEpoch = floor(currentTimestamp).toLong()
13+
val millisecondsSinceEpoch = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
1314

14-
// Convert both parts to hexadecimal format
15-
val hexTimestamp = "%08x%05x".format(secondsPart, microsecondsPart)
16-
return hexTimestamp
17-
}
15+
// Convert both parts to hexadecimal format
16+
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, millisecondsSinceEpoch)
17+
return hexTimestamp
18+
}
1819

19-
class ID {
2020
companion object {
2121
fun custom(id: String): String
2222
= id
2323
// Generate a unique ID with padding to have a longer ID
24-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2524
fun unique(padding: Int = 7): String {
2625
val baseId = hexTimestamp()
2726
val randomPadding = (1..padding)

templates/node/lib/id.js.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class ID {
88
const microsecondsPart = msecs * 1000; // Convert milliseconds to microseconds
99

1010
// Convert to hexadecimal
11-
const hexTimestamp = secondsSinceEpoch.toString(16) + microsecondsPart.toString(16).padStart(5, '0');
11+
const hexTimestamp = secondsSinceEpoch.toString(16)
12+
+ microsecondsPart.toString(16).padStart(5, '0');
1213
return hexTimestamp;
1314
}
1415

1516
// Generate a unique ID with padding to have a longer ID
16-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1717
static unique = (padding = 7) => {
1818
const baseId = ID.#hexTimestamp();
1919
let randomPadding = '';

templates/python/package/id.py.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class ID:
1717
return id
1818

1919
# Generate a unique ID with padding to have a longer ID
20-
# Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2120
@staticmethod
2221
def unique(padding = 7):
2322
base_id = ID.__hex_timestamp()
24-
random_padding = ''.join(random.choice('0123456789abcdef') for _ in range(padding))
23+
random_bytes = os.urandom(ceil(padding / 2))
24+
random_padding = random_bytes.hex()[:padding]
2525
return base_id + random_padding

templates/ruby/lib/container/id.rb.twig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
require 'securerandom'
22

3-
#Generate an hex ID based on timestamp
4-
#Recreated from https://www.php.net/manual/en/function.uniqid.php
5-
def hex_timestamp
6-
now = Time.now
7-
seconds_since_epoch = now.to_i
8-
microseconds_part = now.usec
9-
hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
10-
hex_timestamp
11-
end
12-
133
module {{spec.title | caseUcfirst}}
144
class ID
5+
#Generate an hex ID based on timestamp
6+
#Recreated from https://www.php.net/manual/en/function.uniqid.php
7+
private def hex_timestamp
8+
now = Time.now
9+
seconds_since_epoch = now.to_i
10+
microseconds_part = now.usec
11+
hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
12+
hex_timestamp
13+
end
14+
1515
def self.custom(id)
1616
id
1717
end
1818

1919
# Generate a unique ID with padding to have a longer ID
20-
# Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2120
def self.unique(padding=7)
2221
base_id = hex_timestamp
2322
random_padding = SecureRandom.hex(padding)

templates/swift/Sources/ID.swift.twig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import Foundation
22

3-
// Generate an hex ID based on timestamp
4-
// Recreated from https://www.php.net/manual/en/function.uniqid.php
5-
func hexTimestamp() -> String {
6-
let now = Date()
7-
let secondsSinceEpoch = Int(now.timeIntervalSince1970)
8-
let microsecondsPart = Int((now.timeIntervalSince1970 - Double(secondsSinceEpoch)) * 1_000_000)
9-
let hexTimestamp = String(format: "%08x%05x", secondsSinceEpoch, microsecondsPart)
10-
return hexTimestamp
11-
}
12-
133
public class ID {
4+
// Generate an hex ID based on timestamp
5+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
6+
private func hexTimestamp() -> String {
7+
let now = Date()
8+
let secondsSinceEpoch = Int(now.timeIntervalSince1970)
9+
let microsecondsPart = Int((now.timeIntervalSince1970 - Double(secondsSinceEpoch)) * 1_000_000)
10+
let hexTimestamp = String(format: "%08x%05x", secondsSinceEpoch, microsecondsPart)
11+
return hexTimestamp
12+
}
13+
1414
public static func custom(_ id: String) -> String {
1515
return id
1616
}
1717

1818
// Generate a unique ID with padding to have a longer ID
19-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2019
public static func unique(padding: Int = 7) -> String {
2120
let baseId = hexTimestamp()
2221
let randomPadding = (1...padding).map {

templates/web/src/id.ts.twig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// Generate an hex ID based on timestamp
2-
// Recreated from https://www.php.net/manual/en/function.uniqid.php
3-
function hexTimestamp(): string {
4-
const now = new Date();
5-
const secondsSinceEpoch = Math.floor(now.getTime() / 1000); // Get Unix Timestamp
6-
const microsecondsPart = now.getMilliseconds() * 1000; // Convert milliseconds to microseconds
7-
const hexTimestamp = `${secondsSinceEpoch.toString(16)}${microsecondsPart.toString(16).padStart(5, '0')}`;
8-
return hexTimestamp;
9-
}
10-
111
export class ID {
2+
// Generate an hex ID based on timestamp
3+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
4+
function #hexTimestamp(): string {
5+
const now = new Date();
6+
const secondsSinceEpoch = Math.floor(now.getTime() / 1000); // Get Unix Timestamp
7+
const microsecondsPart = now.getMilliseconds() * 1000; // Convert milliseconds to microseconds
8+
const hexTimestamp = secondsSinceEpoch.toString(16)
9+
+ microsecondsPart.toString(16).padStart(5, '0');
10+
return hexTimestamp;
11+
}
12+
1213
public static custom(id: string): string {
1314
return id
1415
}
1516

1617
public static unique(padding: number = 7): string {
1718
// Generate a unique ID with padding to have a longer ID
18-
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1919
const baseId = hexTimestamp();
2020
let randomPadding = '';
2121
for (let i = 0; i < padding; i++) {

0 commit comments

Comments
 (0)