From c89133aa9abf86b8c0aea526f0255a1fa66a00c2 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Sat, 13 Jun 2026 23:55:15 +0530 Subject: [PATCH 1/3] refactor: rename platform types and methods for consistency; update platform resolution logic --- main.go | 2 +- src/platform/atcoder/atcoder.go | 14 +++++--------- src/platform/codeforces/codeforces.go | 14 +++++--------- src/platform/generic_platform/generic_platform.go | 10 +++++----- src/platform/identifier.go | 8 ++++---- src/platform/leetcode/leetcode.go | 14 +++++--------- src/session/session.go | 6 +++--- src/types/platformDto.go | 2 +- 8 files changed, 29 insertions(+), 41 deletions(-) diff --git a/main.go b/main.go index ad7ac36..6dc4688 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ func main() { } // which platform it is - platform, err := platform.GetPlatform(url) + platform, err := platform.ResolvePlatform(url) if err != nil { fmt.Println("[-] Error while getting platform: ", err) return diff --git a/src/platform/atcoder/atcoder.go b/src/platform/atcoder/atcoder.go index 54d8c32..542fb45 100644 --- a/src/platform/atcoder/atcoder.go +++ b/src/platform/atcoder/atcoder.go @@ -13,31 +13,27 @@ const urlFormat string = "https://atcoder.jp/contests/abc461/tasks/abc461_a" const codeNameDelimiter string = "-" -type AtCoderPlatform struct { +type AtCoderInstance struct { name types.PlatformName codeName string } -func (ac AtCoderPlatform) GetPlatformName() types.PlatformName { +func (ac AtCoderInstance) GetPlatformName() types.PlatformName { return constants.PlatformAtCoder } -func (ac AtCoderPlatform) GetCodeName() string { +func (ac AtCoderInstance) GetCodeName() string { return ac.codeName } -func (ac AtCoderPlatform) GetPlatform(url string) (types.Platform, error) { - return GetPlatform(url) -} - // This is the main method -func GetPlatform(url string) (types.Platform, error) { +func GetInstance(url string) (types.Instance, error) { extractedCodeName, err := extractCodeName(url) if err != nil { return nil, err } - return AtCoderPlatform{ + return AtCoderInstance{ name: constants.PlatformCodeforces, codeName: extractedCodeName, // placeholder for actual extraction logic }, nil diff --git a/src/platform/codeforces/codeforces.go b/src/platform/codeforces/codeforces.go index 09bcf85..5e275ab 100644 --- a/src/platform/codeforces/codeforces.go +++ b/src/platform/codeforces/codeforces.go @@ -10,25 +10,21 @@ import ( const urlFormat string = "https://codeforces.com/problemset/problem/%s/%s" const codeNameDelimiter string = "-" -type CodeforcesPlatform struct { +type CodeforcesInstance struct { name types.PlatformName codeName string } -func (cf CodeforcesPlatform) GetPlatformName() types.PlatformName { +func (cf CodeforcesInstance) GetPlatformName() types.PlatformName { return constants.PlatformCodeforces } -func (cf CodeforcesPlatform) GetCodeName() string { +func (cf CodeforcesInstance) GetCodeName() string { return cf.codeName } -func (cf CodeforcesPlatform) GetPlatform(url string) (types.Platform, error) { - return GetPlatform(url) -} - // This is the main method -func GetPlatform(url string) (types.Platform, error) { +func GetInstance(url string) (types.Instance, error) { // extract code name from url // example: https://codeforces.com/problemset/problem/1234/A @@ -39,7 +35,7 @@ func GetPlatform(url string) (types.Platform, error) { return nil, err } - return CodeforcesPlatform{ + return CodeforcesInstance{ name: constants.PlatformCodeforces, codeName: extractedCodeName, // placeholder for actual extraction logic }, nil diff --git a/src/platform/generic_platform/generic_platform.go b/src/platform/generic_platform/generic_platform.go index b4c6fda..b726227 100644 --- a/src/platform/generic_platform/generic_platform.go +++ b/src/platform/generic_platform/generic_platform.go @@ -5,23 +5,23 @@ import ( "prepare-code/src/types" ) -type GenericPlatform struct { +type GenericInstance struct { name types.PlatformName codeName string } // GetCodeName implements types.Platform. -func (g GenericPlatform) GetCodeName() string { +func (g GenericInstance) GetCodeName() string { return g.codeName } // GetPlatformName implements types.Platform. -func (g GenericPlatform) GetPlatformName() types.PlatformName { +func (g GenericInstance) GetPlatformName() types.PlatformName { return constants.PlatformGeneric } -func GetPlatform() types.Platform { - return GenericPlatform{ +func GetPlatform() types.Instance { + return GenericInstance{ name: constants.PlatformGeneric, codeName: "generic", } diff --git a/src/platform/identifier.go b/src/platform/identifier.go index fbb8a47..97ddc60 100644 --- a/src/platform/identifier.go +++ b/src/platform/identifier.go @@ -12,17 +12,17 @@ import ( /* Take the url and understand what platform it is */ -func GetPlatform(url string) (types.Platform, error) { +func ResolvePlatform(url string) (types.Instance, error) { if len(url) == 0 { return nil, errors.New("empty url") } if strings.Contains(url, "codeforces.com") { - return codeforces.GetPlatform(url) + return codeforces.GetInstance(url) } else if strings.Contains(url, "leetcode.com") { - return leetcode.GetPlatform(url) + return leetcode.GetInstance(url) } else if strings.Contains(url, "atcoder.jp") { - return atcoder.GetPlatform(url) + return atcoder.GetInstance(url) } else { return nil, errors.New("unknown platform or platform not supported") } diff --git a/src/platform/leetcode/leetcode.go b/src/platform/leetcode/leetcode.go index 046aedb..615f40b 100644 --- a/src/platform/leetcode/leetcode.go +++ b/src/platform/leetcode/leetcode.go @@ -20,7 +20,7 @@ Using below API to get problem details: https://alfa-leetcode-api.onrender.com/ */ -type LeetcodePlatform struct { +type LeetcodeInstance struct { name types.PlatformName codeName string } @@ -30,20 +30,16 @@ const UrlCodeDelimiter string = "-" const CodeNameDelimiter string = "." const ApiUrl string = "https://alfa-leetcode-api.onrender.com/select?titleSlug=%s" -func (lc LeetcodePlatform) GetPlatformName() types.PlatformName { +func (lc LeetcodeInstance) GetPlatformName() types.PlatformName { return constants.PlatformLeetCode } -func (lc LeetcodePlatform) GetCodeName() string { +func (lc LeetcodeInstance) GetCodeName() string { return lc.codeName } -func (lc LeetcodePlatform) GetPlatform(url string) (types.Platform, error) { - return GetPlatform(url) -} - // This is the main method -func GetPlatform(url string) (types.Platform, error) { +func GetInstance(url string) (types.Instance, error) { // extract code name from url // example: https://leetcode.com/problems/longest-increasing-subsequence/description @@ -53,7 +49,7 @@ func GetPlatform(url string) (types.Platform, error) { return nil, err } - return LeetcodePlatform{ + return LeetcodeInstance{ name: constants.PlatformLeetCode, codeName: extractedCodeName, // placeholder for actual extraction logic }, nil diff --git a/src/session/session.go b/src/session/session.go index 3c8a673..a0ade33 100644 --- a/src/session/session.go +++ b/src/session/session.go @@ -16,7 +16,7 @@ import ( var ExecutionMap = make(map[string]interface{}) type Session struct { - Platform types.Platform + Platform types.Instance Language string FileName string @@ -28,7 +28,7 @@ type Session struct { // : apply weight group to aquire lock in terms of writes } -func NewSession(platform types.Platform, language string) (Session, error) { +func NewSession(platform types.Instance, language string) (Session, error) { fileName := platform.GetCodeName() + "." + language dir, err := os.Getwd() @@ -56,7 +56,7 @@ func (s Session) GetFileName() string { return s.FileName } -func (s Session) GetPlatform() types.Platform { +func (s Session) GetPlatform() types.Instance { return s.Platform } diff --git a/src/types/platformDto.go b/src/types/platformDto.go index 22d3ecb..07fb2e7 100644 --- a/src/types/platformDto.go +++ b/src/types/platformDto.go @@ -2,7 +2,7 @@ package types type PlatformName string -type Platform interface { +type Instance interface { // platform name GetPlatformName() PlatformName From 35626d4b13d6dc6b27ab56aa9cfa39d49a747529 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Sat, 13 Jun 2026 23:56:03 +0530 Subject: [PATCH 2/3] feat: add PlatformName type and Instance interface for platform handling --- src/types/{platformDto.go => platform_dto.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/types/{platformDto.go => platform_dto.go} (100%) diff --git a/src/types/platformDto.go b/src/types/platform_dto.go similarity index 100% rename from src/types/platformDto.go rename to src/types/platform_dto.go From 01d422a5cf358cff1d9f2aef87925bc1821aa793 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Sun, 14 Jun 2026 00:34:42 +0530 Subject: [PATCH 3/3] feat: add UUID generation functionality for session management --- src/session/session.go | 3 ++ src/uuid/uuid.go | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/uuid/uuid.go diff --git a/src/session/session.go b/src/session/session.go index a0ade33..7c466c7 100644 --- a/src/session/session.go +++ b/src/session/session.go @@ -9,6 +9,7 @@ import ( "prepare-code/src/config" "prepare-code/src/platform/generic_platform" "prepare-code/src/types" + "prepare-code/src/uuid" "strings" "time" ) @@ -16,6 +17,7 @@ import ( var ExecutionMap = make(map[string]interface{}) type Session struct { + Id string Platform types.Instance Language string FileName string @@ -38,6 +40,7 @@ func NewSession(platform types.Instance, language string) (Session, error) { } return Session{ + Id: uuid.New(), Platform: platform, Language: language, FileName: fileName, diff --git a/src/uuid/uuid.go b/src/uuid/uuid.go new file mode 100644 index 0000000..8fa2830 --- /dev/null +++ b/src/uuid/uuid.go @@ -0,0 +1,71 @@ +package uuid + +import ( + "crypto/rand" + "encoding/binary" + "fmt" + "time" +) + +func New() string { + const Retries = 3 + for i := 0; i < Retries; i++ { + // Assumption no repeating of UUID + uuid, err := generateUUID() + if err != nil { + // error while generating UUID retrying + continue + } + + // check if uuid exists + return uuid + } + + x, _ := generateUUIDFromNano() + return x +} + +func generateUUID() (string, error) { + uuid := make([]byte, 16) + _, err := rand.Read(uuid) + if err != nil { + return "", err + } + + // Set the 4 most significant bits of the 7th byte to 0100 (Version 4) + uuid[6] = (uuid[6] & 0x0f) | 0x40 + + // Set the 2 most significant bits of the 9th byte to 10 (Variant 1) + uuid[8] = (uuid[8] & 0x3f) | 0x80 + + // Format matching the canonical 8-4-4-4-12 string layout + return fmt.Sprintf("%x-%x-%x-%x-%x", + uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:16]), nil +} + +func generateUUIDFromNano() (string, error) { + // 1. Get current nanoseconds as an int64 + nanoseconds := time.Now().UnixNano() + + // 2. Create a 16-byte slice for the UUID + uuidBytes := make([]byte, 16) + + // 3. Put the 8-byte nanosecond integer into the first 8 bytes + binary.BigEndian.PutUint64(uuidBytes[0:8], uint64(nanoseconds)) + + // 4. Fill the remaining 8 bytes with crypto random data to ensure uniqueness + _, err := rand.Read(uuidBytes[8:16]) + if err != nil { + return "", err + } + + // 5. Apply RFC 9562 compliance bitmasks + // Set Version 4 (0100) on the 7th byte + uuidBytes[6] = (uuidBytes[6] & 0x0f) | 0x40 + // Set Variant 1 (10) on the 9th byte + uuidBytes[8] = (uuidBytes[8] & 0x3f) | 0x80 + + // 6. Format into the canonical 8-4-4-4-12 string layout + return fmt.Sprintf("%x-%x-%x-%x-%x", + uuidBytes[0:4], uuidBytes[4:6], uuidBytes[6:8], uuidBytes[8:10], uuidBytes[10:16]), nil +}