Skip to content

Commit

Permalink
automatically format generated files with go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Sep 21, 2023
1 parent 67aee16 commit a2291d4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
15 changes: 12 additions & 3 deletions pkg/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package conversion
import (
"bytes"
"fmt"
"go/format"
"io"
"math"
"net/http"
Expand Down Expand Up @@ -520,6 +521,14 @@ func processField(fieldDef *dialectField) (*outField, error) {
return outF, nil
}

func formatSource(src []byte) []byte {
formattedSource, err := format.Source(src)
if err != nil {
return src
}
return formattedSource
}

func writeDialect(
dir string,
defName string,
Expand All @@ -541,7 +550,7 @@ func writeDialect(
return err
}

return os.WriteFile(filepath.Join(dir, "dialect.go"), buf.Bytes(), 0o644)
return os.WriteFile(filepath.Join(dir, "dialect.go"), formatSource(buf.Bytes()), 0o644)
}

func writeEnum(
Expand All @@ -560,7 +569,7 @@ func writeEnum(
return err
}

return os.WriteFile(filepath.Join(dir, "enum_"+strings.ToLower(enum.Name)+".go"), buf.Bytes(), 0o644)
return os.WriteFile(filepath.Join(dir, "enum_"+strings.ToLower(enum.Name)+".go"), formatSource(buf.Bytes()), 0o644)
}

func writeMessage(
Expand All @@ -579,7 +588,7 @@ func writeMessage(
return err
}

return os.WriteFile(filepath.Join(dir, "message_"+strings.ToLower(msg.OrigName)+".go"), buf.Bytes(), 0o644)
return os.WriteFile(filepath.Join(dir, "message_"+strings.ToLower(msg.OrigName)+".go"), formatSource(buf.Bytes()), 0o644)
}

// Convert converts a XML definition into a Golang definition.
Expand Down
19 changes: 10 additions & 9 deletions pkg/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,22 @@ const testDialect = `<?xml version="1.0"?>
var testDialectGo = `//autogenerated:yes
//nolint:revive,misspell,govet,lll
package testdialect
// Detected anomaly info measured by onboard sensors and actuators.
type MessageAMessage struct {
// a test uint8
TestUint8 A_TYPE ` + "`" + `mavenum:"uint8"` + "`" + `
// a test string
TestString string ` + "`" + `mavlen:"16" mavname:"Test_string"` + "`" + `
// a test array
TestArray [4]uint32
// a test extension
MissionType MAV_MISSION_TYPE ` + "`" + `mavenum:"uint8" mavext:"true"` + "`" + `
// a test uint8
TestUint8 A_TYPE ` + "`" + `mavenum:"uint8"` + "`" + `
// a test string
TestString string ` + "`" + `mavlen:"16" mavname:"Test_string"` + "`" + `
// a test array
TestArray [4]uint32
// a test extension
MissionType MAV_MISSION_TYPE ` + "`" + `mavenum:"uint8" mavext:"true"` + "`" + `
}
// GetID implements the message.Message interface.
func (*MessageAMessage) GetID() uint32 {
return 43000
return 43000
}
`

Expand Down

0 comments on commit a2291d4

Please sign in to comment.