diff --git a/backend/internal/adj/adj.go b/backend/internal/adj/adj.go index 41a740b5c..8fcb5add6 100644 --- a/backend/internal/adj/adj.go +++ b/backend/internal/adj/adj.go @@ -10,8 +10,7 @@ import ( ) const ( - RepoUrl = "https://github.com/HyperloopUPV-H8/adj.git" // URL of the ADJ repository - + RepoURL = "https://github.com/Hyperloop-UPV/adj.git" // URL of the ADJ repository ) var RepoPath = getRepoPath() diff --git a/backend/internal/adj/git.go b/backend/internal/adj/git.go index 95b48d20d..e0700771f 100644 --- a/backend/internal/adj/git.go +++ b/backend/internal/adj/git.go @@ -9,7 +9,12 @@ import ( "github.com/go-git/go-git/v5/plumbing" ) -// WARNING: Doing tricks on it +// updateRepo ensures that the local ADJ repository matches the specified remote branch. +// It first performs a test clone to verify remote accessibility (including internet +// connectivity). If the remote branch is accessible, the local repository is completely +// removed and replaced with a clean, shallow clone of that branch. +// If the remote is not accessible, the existing local repository is left untouched. + func updateRepo(AdjBranch string) error { var err error @@ -18,7 +23,7 @@ func updateRepo(AdjBranch string) error { return nil } else { cloneOptions := &git.CloneOptions{ - URL: RepoUrl, + URL: RepoURL, ReferenceName: plumbing.NewBranchReferenceName(AdjBranch), SingleBranch: true, Depth: 1, @@ -45,6 +50,7 @@ func updateRepo(AdjBranch string) error { return err } + // After checking that the repo is accessible, clone or update (overwrite) the local ADJ repo if _, err = os.Stat(RepoPath); os.IsNotExist(err) { _, err = git.PlainClone(RepoPath, false, cloneOptions) if err != nil { diff --git a/backend/internal/utils/units.go b/backend/internal/utils/units.go index 32d9386bd..cd835434a 100644 --- a/backend/internal/utils/units.go +++ b/backend/internal/utils/units.go @@ -1,3 +1,4 @@ +// Package utils provides utility functions for handling unit conversions and operations. package utils import ( @@ -13,6 +14,7 @@ const ( Separator = "#" ) +// operationExp matches an operator followed by a decimal number var operationExp = regexp.MustCompile(fmt.Sprintf(`([+\-\/*]{1})(%s)`, DecimalRegex)) type Units struct { @@ -20,6 +22,7 @@ type Units struct { Operations Operations } +// ParseUnits given a units literal string and a map of global units, returns the corresponding Units func ParseUnits(literal string, globalUnits map[string]Operations) (Units, error) { // TODO: puede fallar si no tiene op y no estan en global o si las op que tiene estan mal if literal == "" { return Units{ @@ -59,19 +62,26 @@ func ParseUnits(literal string, globalUnits map[string]Operations) (Units, error }, nil } +// Operations is a list of operations to be applied in order type Operations []Operation +// NewOperations given an operations literal string, returns the corresponding Operations func NewOperations(literal string) (Operations, error) { + // Empty operations if literal == "" { return make(Operations, 0), nil } + // Find all operations in the literal + // match structure [[full_match, operator, operand], ...] matches := operationExp.FindAllStringSubmatch(literal, -1) + // If no matches found, return an error if matches == nil { return nil, fmt.Errorf("incorrect operations: %s", literal) } + // create a operation for each match operations := make([]Operation, 0) for _, match := range matches { operation := getOperation(match[1], match[2]) @@ -80,6 +90,7 @@ func NewOperations(literal string) (Operations, error) { return operations, nil } +// given an operator and operand string, returns the corresponding Operation func getOperation(operator string, operand string) Operation { numOperand, err := strconv.ParseFloat(operand, 64) if err != nil { @@ -91,6 +102,7 @@ func getOperation(operator string, operand string) Operation { } } +// Convert applies each operation in order to the value given func (operations Operations) Convert(value float64) float64 { result := value for _, op := range operations { @@ -99,6 +111,7 @@ func (operations Operations) Convert(value float64) float64 { return result } +// Revert reverts each operation in reverse order from the value given func (operations Operations) Revert(value float64) float64 { result := value for i := len(operations) - 1; i >= 0; i-- { @@ -107,11 +120,13 @@ func (operations Operations) Revert(value float64) float64 { return result } +// Operation representation of a single operation composed by a operator and an operand type Operation struct { - Operator string - Operand float64 + Operator string // "+", "-", "*", "/" + Operand float64 // number } +// applies the operation to the given value func (operation Operation) convert(value float64) float64 { switch operation.Operator { case "+": @@ -126,6 +141,7 @@ func (operation Operation) convert(value float64) float64 { return value } +// reverts the operation from the given value func (operation Operation) revert(value float64) float64 { switch operation.Operator { case "+":