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
3 changes: 1 addition & 2 deletions backend/internal/adj/adj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions backend/internal/adj/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions backend/internal/utils/units.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package utils provides utility functions for handling unit conversions and operations.
package utils

import (
Expand All @@ -13,13 +14,15 @@ const (
Separator = "#"
)

// operationExp matches an operator followed by a decimal number
var operationExp = regexp.MustCompile(fmt.Sprintf(`([+\-\/*]{1})(%s)`, DecimalRegex))

type Units struct {
Name string
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{
Expand Down Expand Up @@ -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])
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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-- {
Expand All @@ -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 "+":
Expand All @@ -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 "+":
Expand Down
Loading