From a2291d44c4abef06f75c56c0738348c74caccaf3 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 21 Sep 2023 16:26:34 +0200 Subject: [PATCH] automatically format generated files with go fmt --- pkg/conversion/conversion.go | 15 ++++++++++++--- pkg/conversion/conversion_test.go | 19 ++++++++++--------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/conversion/conversion.go b/pkg/conversion/conversion.go index 844434b30..524c7703e 100644 --- a/pkg/conversion/conversion.go +++ b/pkg/conversion/conversion.go @@ -4,6 +4,7 @@ package conversion import ( "bytes" "fmt" + "go/format" "io" "math" "net/http" @@ -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, @@ -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( @@ -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( @@ -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. diff --git a/pkg/conversion/conversion_test.go b/pkg/conversion/conversion_test.go index 1e62b39c6..74c9caa2b 100644 --- a/pkg/conversion/conversion_test.go +++ b/pkg/conversion/conversion_test.go @@ -47,21 +47,22 @@ const testDialect = ` 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 } `