-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the raw protobuf types aren't enough to help with things like getting the parent resource structs. Creating yet another intermediary struct to help facilitate it. leveraging that functionality to construct paths for resources with parents.
- Loading branch information
1 parent
d9cd1c4
commit 1e51ae7
Showing
9 changed files
with
1,786 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// package parser converts the schema | ||
// into a full-fledged struct that provides | ||
// more functionality for discovering resource references, etc. | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aep-dev/aepc/schema" | ||
) | ||
|
||
// ParsedService wraps schema.Service, but includes | ||
// helper functions for things like retrieving the resource | ||
// definitions within a service. | ||
type ParsedService struct { | ||
*schema.Service | ||
ResourceByType map[string]*ParsedResource | ||
} | ||
|
||
type ParsedResource struct { | ||
*schema.Resource | ||
Parents []*ParsedResource | ||
} | ||
|
||
func NewParsedService(s *schema.Service) (*ParsedService, error) { | ||
resourceByType, err := loadResourceByType(s) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to build service %q: %w", s, err) | ||
} | ||
ps := ParsedService{ | ||
Service: s, | ||
ResourceByType: resourceByType, | ||
} | ||
return &ps, nil | ||
} | ||
|
||
func loadResourceByType(s *schema.Service) (map[string]*ParsedResource, error) { | ||
resourceByType := map[string]*ParsedResource{} | ||
for _, r := range s.Resources { | ||
name := fmt.Sprintf("%s/%s", s.Name, r.Kind) | ||
resourceByType[name] = &ParsedResource{ | ||
Resource: r, | ||
Parents: []*ParsedResource{}, | ||
} | ||
} | ||
// populate resource parents | ||
for _, r := range resourceByType { | ||
for _, p := range r.Resource.Parents { | ||
parentResource, exists := resourceByType[p] | ||
if !exists { | ||
return nil, fmt.Errorf("parent %q for resource %q not found", p, r.Kind) | ||
} | ||
r.Parents = append(r.Parents, parentResource) | ||
} | ||
} | ||
return resourceByType, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters