-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1534f87
commit 38712ff
Showing
12 changed files
with
235 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package generator | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/hashicorp/pandora/tools/sdk/resourcemanager" | ||
"github.com/hashicorp/pandora/tools/sdk/services" | ||
) | ||
|
||
type CommonTypesGenerator struct{} | ||
|
||
func NewCommonTypesGenerator() CommonTypesGenerator { | ||
return CommonTypesGenerator{} | ||
} | ||
|
||
type SDKInput struct { | ||
CommonPackageName string | ||
CommonPackagePath string | ||
CommonTypes *services.ResourceManagerCommonTypes | ||
OutputDirectory string | ||
VersionName string | ||
} | ||
|
||
func (s *CommonTypesGenerator) GenerateForSDK(input SDKInput) error { | ||
input.VersionName = strings.ToLower(input.VersionName) | ||
commonTypesDirectory := filepath.Join(input.OutputDirectory, input.CommonPackagePath) | ||
|
||
if err := cleanAndRecreateWorkingDirectory(commonTypesDirectory); err != nil { | ||
return fmt.Errorf("cleaning/recreating working directory %q: %+v", commonTypesDirectory, err) | ||
} | ||
|
||
stages := map[string]func(SDKInput, string) error{ | ||
"commonTypes": s.commonTypes, | ||
} | ||
for name, stage := range stages { | ||
log.Printf("[DEBUG] Running Stage %q..", name) | ||
if err := stage(input, commonTypesDirectory); err != nil { | ||
return fmt.Errorf("generating %s: %+v", name, err) | ||
} | ||
} | ||
|
||
runGoFmt(commonTypesDirectory) | ||
runGoImports(commonTypesDirectory) | ||
return nil | ||
} | ||
|
||
func (s *CommonTypesGenerator) commonTypes(input SDKInput, outputDirectory string) error { | ||
data := ServiceGeneratorData{ | ||
apiVersion: input.VersionName, | ||
constants: input.CommonTypes.Constants, | ||
models: input.CommonTypes.Models, | ||
packageName: "models", | ||
source: resourcemanager.ApiDefinitionsSourceMicrosoftGraphMetadata, | ||
} | ||
|
||
gen := constantsTemplater{ | ||
constantTemplateFunc: templateForConstant, | ||
} | ||
if err := s.writeToPath(outputDirectory, "constants.go", gen, data); err != nil { | ||
return fmt.Errorf("templating constants: %+v", err) | ||
} | ||
|
||
for modelName, model := range input.CommonTypes.Models { | ||
fileName := fmt.Sprintf("model_%s.go", strings.ToLower(modelName)) | ||
gen := modelsTemplater{ | ||
name: modelName, | ||
model: model, | ||
} | ||
if err := s.writeToPath(outputDirectory, fileName, gen, data); err != nil { | ||
return fmt.Errorf("templating model for %q: %+v", modelName, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *CommonTypesGenerator) writeToPath(directory, filePath string, templater templaterForResource, data ServiceGeneratorData) error { | ||
fileContents, err := templater.template(data) | ||
if err != nil { | ||
return fmt.Errorf("templating: %+v", err) | ||
} | ||
|
||
fullFilePath := filepath.Join(directory, filePath) | ||
|
||
// remove any existing file if it exists | ||
_ = os.Remove(fullFilePath) | ||
file, err := os.Create(fullFilePath) | ||
defer file.Close() | ||
if err != nil { | ||
return fmt.Errorf("opening %q: %+v", fullFilePath, err) | ||
} | ||
|
||
_, _ = file.WriteString(*fileContents) | ||
return 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
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,37 @@ | ||
package resourcemanager | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type CommonTypesClient struct { | ||
Client | ||
} | ||
|
||
func (c CommonTypesClient) Get() (*CommonTypesDetails, error) { | ||
endpoint := fmt.Sprintf("%s%s/commonTypes", c.endpoint, c.apiEndpoint) | ||
resp, err := c.client.Get(endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO: handle this being a 404 etc | ||
|
||
var response CommonTypesDetails | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &response, nil | ||
} | ||
|
||
type CommonTypesDetails struct { | ||
// Constants is a map of key (Constant Name) to value (ConstantDetails) describing | ||
// each common Constant supported by this API | ||
Constants map[string]ConstantDetails `json:"constants"` | ||
|
||
// Models is a map of key (Model Name) to value (ModelDetails) describing | ||
// each common Model supported by this API | ||
Models map[string]ModelDetails `json:"models"` | ||
} |
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
Oops, something went wrong.