Skip to content

Commit

Permalink
refactor: I think this finally works... (Part II)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralvarezdev committed Dec 3, 2024
1 parent ce2e992 commit 34dd14e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
3 changes: 0 additions & 3 deletions types/rest/errors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package rest

var (
MissingInterceptions = "missing interceptions: %v [%v]"
MissingEndpointInterception = "missing endpoint interception: %v [%v]"
MissingChildrenMap = "missing children map: %v [%v]"
FailedToTraverse = "failed to traverse: %v [%v]"
)
58 changes: 27 additions & 31 deletions types/rest/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,53 @@ func NewMap(interceptions *map[string]map[Method]grpc.Method, childrenMaps *map[

// Traverse traverses the REST map
func (m *Map) Traverse(debug bool, relativeURI string, restMethod Method) (*grpc.Method, error) {
var firstPartUri, secondPartUri string
// Check if the whole relative URI is in the interceptions map
if m.Interceptions != nil {
if methodMap, ok := (*m.Interceptions)[relativeURI]; ok {
if grpcMethod, ok := methodMap[restMethod]; ok {
return &grpcMethod, nil
}
}
}

// Get the second part index of the relative URI
secondPartUriIndex := strings.Index(relativeURI[1:], "/")
if secondPartUriIndex != -1 {
secondPartUriIndex++
// Get the index of the possible URI
var possibleBaseUri, possibleUri string
possibleUriIndex := strings.Index(relativeURI[1:], "/")
if possibleUriIndex != -1 {
possibleUriIndex++
}

// Check if the URI is an endpoint or a URI with a parameter
isEndpoint := false
if secondPartUriIndex == -1 {
firstPartUri = "/"
isEndpoint = true
} else if relativeURI[secondPartUriIndex+1] == ':' {
firstPartUri = relativeURI[secondPartUriIndex:]
isEndpoint = true
if possibleUriIndex == -1 {
possibleUri = "/"
} else {
firstPartUri = relativeURI[:secondPartUriIndex]
secondPartUri = relativeURI[secondPartUriIndex:]
possibleBaseUri = relativeURI[:possibleUriIndex]
possibleUri = relativeURI[possibleUriIndex:]
}

// Check if debug is enabled
if debug {
// Print if the URI is an endpoint, first half URI and second half URI
fmt.Printf("Is endpoint: %t\n", isEndpoint)
fmt.Printf("First half URI: %s\n", firstPartUri)
fmt.Printf("Second half URI: %s\n", secondPartUri)
// Print the possible base URI and URI
fmt.Printf("Possible base URI: %s\n", possibleBaseUri)
fmt.Printf("Possible URI: %s\n", possibleUri)
}

// Check if the first part of the URI is in the interceptions map
if m.Interceptions != nil && isEndpoint {
if methodMap, ok := (*m.Interceptions)[firstPartUri]; ok {
if m.Interceptions != nil {
// Check if the possible URI is in the interceptions map
if methodMap, ok := (*m.Interceptions)[possibleUri]; ok {
if grpcMethod, ok := methodMap[restMethod]; ok {
return &grpcMethod, nil
}
}
}

// Check if the URI is an endpoint
if isEndpoint {
return nil, fmt.Errorf(MissingEndpointInterception, relativeURI, restMethod)
}

if m.ChildrenMaps != nil {
// Check if the first part of the URI is in the children maps
// Check if the possible base URI is in the children maps
// If it is, traverse the child map
if childMap, ok := (*m.ChildrenMaps)[firstPartUri]; ok {
return childMap.Traverse(debug, secondPartUri, restMethod)
if childMap, ok := (*m.ChildrenMaps)[possibleBaseUri]; ok {
return childMap.Traverse(debug, possibleUri, restMethod)
}
return nil, fmt.Errorf(FailedToTraverse, relativeURI, restMethod)
}

return nil, fmt.Errorf(MissingChildrenMap, relativeURI, restMethod)
return nil, fmt.Errorf(MissingEndpointInterception, relativeURI, restMethod)
}

0 comments on commit 34dd14e

Please sign in to comment.