Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions src/platform/atcoder/atcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions src/platform/codeforces/codeforces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/platform/generic_platform/generic_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
8 changes: 4 additions & 4 deletions src/platform/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
14 changes: 5 additions & 9 deletions src/platform/leetcode/leetcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"prepare-code/src/config"
"prepare-code/src/platform/generic_platform"
"prepare-code/src/types"
"prepare-code/src/uuid"
"strings"
"time"
)

var ExecutionMap = make(map[string]interface{})

type Session struct {
Platform types.Platform
Id string
Platform types.Instance
Language string
FileName string

Expand All @@ -28,7 +30,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()
Expand All @@ -38,6 +40,7 @@ func NewSession(platform types.Platform, language string) (Session, error) {
}

return Session{
Id: uuid.New(),
Platform: platform,
Language: language,
FileName: fileName,
Expand All @@ -56,7 +59,7 @@ func (s Session) GetFileName() string {
return s.FileName
}

func (s Session) GetPlatform() types.Platform {
func (s Session) GetPlatform() types.Instance {
return s.Platform
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/platformDto.go → src/types/platform_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package types

type PlatformName string

type Platform interface {
type Instance interface {
// platform name
GetPlatformName() PlatformName

Expand Down
71 changes: 71 additions & 0 deletions src/uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading