-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4167 from hashicorp/msgraph-importer-refactor
`tools/importer-msgraph-metadata`: refactor to generate JSON definitions using `data-api-sdk`
- Loading branch information
Showing
44 changed files
with
1,134 additions
and
1,737 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,5 @@ | ||
{ | ||
"dataSource": "MicrosoftGraph", | ||
"sourceInformation": "microsoftgraph/msgraph-metadata", | ||
"gitRevision": "7b1feb7cf42dd1f08941b349209c1cf8602f47c8" | ||
} |
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
42 changes: 42 additions & 0 deletions
42
tools/importer-msgraph-metadata/components/normalize/duplicates.go
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,42 @@ | ||
package normalize | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func DeDuplicate(name string) string { | ||
nameSpaced := regexp.MustCompile("([A-Z])").ReplaceAllString(name, " $1") | ||
nameParts := strings.Split(strings.TrimSpace(nameSpaced), " ") | ||
newParts := make([]string, 0) | ||
for i := 0; i < len(nameParts); i++ { | ||
if i > 0 && strings.EqualFold(nameParts[i], nameParts[i-1]) { | ||
continue | ||
} | ||
newParts = append(newParts, nameParts[i]) | ||
} | ||
return strings.Join(newParts, "") | ||
} | ||
|
||
func DeDuplicateName(name string) string { | ||
words := make([]string, 0) | ||
i := 0 | ||
for j, c := range name { | ||
char := string(c) | ||
if j > 0 && strings.ToUpper(char) == char { | ||
i++ | ||
} | ||
if len(words) <= i { | ||
words = append(words, "") | ||
} | ||
words[i] = words[i] + char | ||
} | ||
out := "" | ||
for k, word := range words { | ||
if k > 0 && strings.EqualFold(words[k-1], word) { | ||
continue | ||
} | ||
out = out + word | ||
} | ||
return out | ||
} |
124 changes: 124 additions & 0 deletions
124
tools/importer-msgraph-metadata/components/normalize/normalize.go
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,124 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package normalize | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func Singularize(name string) string { | ||
if len(name) >= 3 && name[len(name)-3:] == "ies" { | ||
return fmt.Sprintf("%sy", name[:len(name)-3]) | ||
} | ||
if len(name) >= 3 && name[len(name)-3:] == "ses" { | ||
return name[:len(name)-2] | ||
} | ||
if len(name) >= 1 && name[len(name)-1:] == "s" { | ||
return name[:len(name)-1] | ||
} | ||
return name | ||
} | ||
|
||
func Pluralize(name string) string { | ||
if name == "" { | ||
return "" | ||
} | ||
ret := fmt.Sprintf("%ss", name) | ||
if strings.EqualFold(name, "me") { | ||
return name | ||
} | ||
if len(name) == 0 { | ||
return ret | ||
} | ||
if strings.EqualFold(name[len(name)-2:], "ay") || strings.EqualFold(name[len(name)-2:], "ey") { | ||
return fmt.Sprintf("%ss", name) | ||
} | ||
if strings.EqualFold(name[len(name)-1:], "y") { | ||
return fmt.Sprintf("%sies", name[:len(name)-1]) | ||
} | ||
if strings.EqualFold(name[len(name)-1:], "s") { | ||
return name | ||
} | ||
if len(name) < 2 { | ||
return ret | ||
} | ||
if strings.EqualFold(name[len(name)-2:], "Of") { | ||
return name | ||
} | ||
return ret | ||
} | ||
|
||
func CleanName(name string) string { | ||
// Trim a "microsoft.graph" prefix from type names | ||
name = strings.TrimPrefix(name, "microsoft.graph") | ||
|
||
// Replace all periods with spaces to allow for title casing | ||
name = strings.ReplaceAll(name, ".", " ") | ||
|
||
// Convert name to title case | ||
name = cases.Title(language.AmericanEnglish, cases.NoLower).String(name) | ||
|
||
// Remove all non-alphanumeric characters, except for any leading underscores | ||
leading := regexp.MustCompile(`^_+`).FindString(name) | ||
trailing := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(name[len(leading):], "") | ||
name = leading + trailing | ||
|
||
// Odata should be OData | ||
name = regexp.MustCompile("^Odata").ReplaceAllString(name, "OData") | ||
|
||
// Innererror should be InnerError | ||
name = regexp.MustCompile("^Innererror").ReplaceAllString(name, "InnerError") | ||
|
||
// known issue where CloudPC appears with inconsistent casing | ||
name = regexp.MustCompile("(?i)CloudPc").ReplaceAllString(name, "CloudPC") | ||
|
||
return name | ||
} | ||
|
||
func CleanNameCamel(name string) string { | ||
name = CleanName(name) | ||
return strings.ToLower(name[0:1]) + name[1:] | ||
} | ||
|
||
type operationVerbs []string | ||
|
||
func (ov operationVerbs) Match(operation string) (*string, bool) { | ||
for _, v := range ov { | ||
if regexp.MustCompile(fmt.Sprintf("^%s$", v)).MatchString(operation) { | ||
return &v, true | ||
} | ||
if regexp.MustCompile(fmt.Sprintf("^%s[A-Z]", v)).MatchString(operation) { | ||
return &v, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
var Verbs = operationVerbs{ | ||
"Acquire", | ||
"Add", | ||
"Assign", | ||
"Check", | ||
"Discover", | ||
"Get", | ||
"Instantiate", | ||
"Parse", | ||
"Pause", | ||
"Provision", | ||
"Remove", | ||
"Renew", | ||
"Restart", | ||
"Restore", | ||
"Set", | ||
"Start", | ||
"Stop", | ||
"Unset", | ||
"Update", | ||
"Validate", | ||
} |
Oops, something went wrong.