Skip to content

Commit

Permalink
Merge pull request #6 from bbredesen/cleanup/issue-3
Browse files Browse the repository at this point in the history
issue #3, add val sort by name, mod sort by val
  • Loading branch information
bbredesen committed Mar 6, 2023
2 parents 2572b3a + 821abae commit 68667a6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion def/bitmask_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (t *bitmaskType) Resolve(tr TypeRegistry, vr ValueRegistry) *IncludeSet {
func (t *bitmaskType) PrintPublicDeclaration(w io.Writer) {
t.internalType.PrintPublicDeclaration(w)

sort.Sort(byValue(t.values))
sort.Sort(ByValue(t.values))

if len(t.values) > 0 {
fmt.Fprint(w, "const (\n")
Expand Down
2 changes: 1 addition & 1 deletion def/enum_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (t *enumType) PrintPublicDeclaration(w io.Writer) {
t.internalType.PrintPublicDeclaration(w)
}

sort.Sort(byValue(t.values))
sort.Sort(ByValue(t.values))

if len(t.values) > 0 {
fmt.Fprint(w, "const (\n")
Expand Down
2 changes: 1 addition & 1 deletion def/external_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (t *externalType) Resolve(tr TypeRegistry, vr ValueRegistry) *IncludeSet {
// PrintPublicDeclaration is for external types just needs to print constants
func (t *externalType) PrintPublicDeclaration(w io.Writer) {

sort.Sort(byValue(t.values))
sort.Sort(ByValue(t.values))

if len(t.values) > 0 {
fmt.Fprint(w, "const (\n")
Expand Down
2 changes: 1 addition & 1 deletion def/handle_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (t *handleType) Resolve(tr TypeRegistry, vr ValueRegistry) *IncludeSet {
func (t *handleType) PrintPublicDeclaration(w io.Writer) {
t.internalType.PrintPublicDeclaration(w)

sort.Sort(byValue(t.values))
sort.Sort(ByValue(t.values))

if len(t.values) > 0 {
fmt.Fprint(w, "const (\n")
Expand Down
21 changes: 17 additions & 4 deletions def/registry_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ type ValueDefiner interface {
IsCore() bool
}

type byValue []ValueDefiner
type ByValue []ValueDefiner

// TODO: also sort by bitmask values and aliases
func (a byValue) Len() int { return len(a) }
func (a byValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byValue) Less(i, j int) bool {
func (a ByValue) Len() int { return len(a) }
func (a ByValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByValue) Less(i, j int) bool {
iNum, err1 := strconv.Atoi(a[i].ValueString())
jNum, err2 := strconv.Atoi(a[j].ValueString())
if err1 == nil && err2 == nil {
Expand All @@ -185,6 +185,19 @@ func (a byValue) Less(i, j int) bool {
return a[i].ValueString() < a[j].ValueString()
}

type ByValuePublicName []ValueDefiner // add for cleanup/issue-3

func (a ByValuePublicName) Len() int { return len(a) }
func (a ByValuePublicName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByValuePublicName) Less(i, j int) bool {
iNum, err1 := strconv.Atoi(a[i].PublicName())
jNum, err2 := strconv.Atoi(a[j].PublicName())
if err1 == nil && err2 == nil {
return iNum < jNum
}
return a[i].PublicName() < a[j].PublicName()
}

func WriteStringerCommands(w io.Writer, defs []TypeDefiner, cat TypeCategory, filenameBase string) {
typesPerCallLimit := 32

Expand Down
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,19 +369,31 @@ func printTypes(w io.Writer, types []def.TypeDefiner, vals map[string]def.ValueR
}

func printLooseValues(w io.Writer, valsByTypeName map[string]def.ValueRegistry) {
// sort and refactored for cleanup/issue-3

for k, vr := range valsByTypeName {
fmt.Fprintf(w, "// Platform-specific values for %s\n", k)
// Values will be sorted by const name for extension names/spec versions, and by value for typed consts
allValues := make([]def.ValueDefiner, 0, len(vr))
for _, val := range vr {
allValues = append(allValues, val)
}

if k == "" {
fmt.Fprint(w, "// Extension names and versions\n")
// Drop the values into a slice and sort by the const name
sort.Sort(def.ByValuePublicName(allValues))
} else {
fmt.Fprintf(w, "// Platform-specific values for %s\n", k)
sort.Sort(def.ByValue(allValues))
}

fmt.Fprintf(w, "const (\n")

i := 0
for _, val := range vr {
for _, val := range allValues {
val.PrintPublicDeclaration(w)
i++
}
fmt.Fprintf(w, ")\n\n")
}

}

func copyStaticFiles() {
Expand Down

0 comments on commit 68667a6

Please sign in to comment.