Skip to content

Commit

Permalink
create getProductName function
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Proud committed Dec 5, 2023
1 parent ac416aa commit 0b65a48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
8 changes: 8 additions & 0 deletions sdk/subproduct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sdk

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -99,3 +100,10 @@ func TestGetSubProductsDetailsInvalidMajorVersion(t *testing.T) {
assert.ErrorIs(t, err, ErrorInvalidSubProductMajorVersion)
assert.Empty(t, subProductDetails.Code, "Expected response to be empty")
}

func TestGetProductName(t *testing.T) {
reEndVersion := regexp.MustCompile(`[0-9]+.*`)
productName := "VMware ESXi 8.0 native ixgben ENS 1.18.2.0 NIC Driver for Intel Ethernet Controllers 82599, x520, x540, x550, and x552 family"
productName = getProductName(productName, "vmware_vsphere", "Driver CDs", reEndVersion)
assert.Equal(t, productName, "Driver - native ixgben ENS")
}
38 changes: 23 additions & 15 deletions sdk/subproducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sdk

import (
"errors"
"fmt"
"regexp"
"sort"
"strings"
Expand All @@ -24,7 +25,7 @@ type SubProductSliceElement struct {
var ErrorInvalidSubProduct = errors.New("subproduct: invalid subproduct requested")
var ErrorInvalidSubProductMajorVersion = errors.New("subproduct: invalid major version requested")

func (c *Client) GetSubProductsMap(slug string) (data map[string]SubProductDetails, err error) {
func (c *Client) GetSubProductsMap(slug string) (subProductMap map[string]SubProductDetails, err error) {
c.EnsureProductDetailMap()
if err != nil {
return
Expand All @@ -35,7 +36,7 @@ func (c *Client) GetSubProductsMap(slug string) (data map[string]SubProductDetai
return
}

subProductMap := make(map[string]SubProductDetails)
subProductMap = make(map[string]SubProductDetails)

var majorVersions []string
majorVersions, err = c.GetMajorVersionsSlice(slug)
Expand All @@ -59,12 +60,13 @@ func (c *Client) GetSubProductsMap(slug string) (data map[string]SubProductDetai
for _, dlgEdition := range dlgEditionsList {
for _, dlgList := range dlgEdition.DlgList {
productCode := strings.ToLower(dlgList.Code)
productName := dlgList.Name
// productName := dlgList.Name
// Regex captures numbers and all text after
reEndVersion := regexp.MustCompile(`[0-9]+.*`)
// Regex detects numbers surrounded by - or _
reMidVersion := regexp.MustCompile(`(-|_)([0-9.]+)(-|_)`)

productName := getProductName(dlgList.Name, slug, dlgEdition.Name, reEndVersion)
// Horizon clients don't follow a common pattern for API naming. This block aligns the pattern
if strings.HasPrefix(productCode, "cart") {
productCode = ModifyHorizonClientCode(productCode)
Expand All @@ -84,16 +86,6 @@ func (c *Client) GetSubProductsMap(slug string) (data map[string]SubProductDetai
}
}

// Special case for Horizon due to inconsistent naming
if slug == "vmware_horizon" {
reNumbers := regexp.MustCompile(`[0-9.,]+`)
reSpace := regexp.MustCompile(`\s+`)
productName = strings.TrimSpace(reNumbers.ReplaceAllString(productName, ""))
productName = reSpace.ReplaceAllString(productName, " ")
} else {
productName = strings.TrimSpace(reEndVersion.ReplaceAllString(productName, ""))
}

// Initalize the struct for a product code for the first time
if _, ok := subProductMap[productCode]; !ok {
subProductMap[productCode] = SubProductDetails{
Expand All @@ -111,11 +103,27 @@ func (c *Client) GetSubProductsMap(slug string) (data map[string]SubProductDetai
}
}
}

data = subProductMap
return
}

func getProductName(productName, slug, dlgEditionName string, reEndVersion *regexp.Regexp) (string) {
// Special case for Horizon due to inconsistent naming
if slug == "vmware_horizon" {
reNumbers := regexp.MustCompile(`[0-9.,]+`)
reSpace := regexp.MustCompile(`\s+`)
productName := strings.TrimSpace(reNumbers.ReplaceAllString(productName, ""))
return reSpace.ReplaceAllString(productName, " ")
// Special case for ESXi drivers to make human readable
} else if slug == "vmware_vsphere" && dlgEditionName == "Driver CDs" {
stripEsx := regexp.MustCompile(`VMware ESXi [0-9]+.[0-9]+ `)
productName = fmt.Sprintf("Driver - %s", stripEsx.ReplaceAllString(productName, ""))
return strings.TrimSpace(reEndVersion.ReplaceAllString(productName, ""))
} else {
return strings.TrimSpace(reEndVersion.ReplaceAllString(productName, ""))
}

}

func ModifyHorizonClientCode(productCode string) (string) {
productCode = strings.Replace(productCode, "-", "_", 1)

Expand Down

0 comments on commit 0b65a48

Please sign in to comment.