Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yamlfmt: added and implemented functional features (#43)
* yamlfmt: added and implemented functional features While adding functionality for small edge cases or formatting differences, I found that the Formatter code was becoming harder to reason about. It would grow linearly with the number of little formatting things I had to do. I have adjusted the pattern to instead be a collection of functional features that can be added at-will. Created a new `Feature` type that has a Before and After action. Also added the FeatureList type, with code to apply the before or after actions in order on byte slice input. I converted all existing candidates in the formatter code to use this functional feature pattern instead. * hotfix: make hotfix package create feature * yamlfmt: wrap feature apply errors This wraps feature apply errors with the name and type of action. * basic: change feature configuration pattern Instead of a receiver method that configures the features in place on the formatter, make it so the features are returned from a function that reads the config. Avoids deliberate mutability.
- Loading branch information
1 parent
e05c99f
commit 6da562a
Showing
13 changed files
with
340 additions
and
135 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package basic | ||
|
||
import ( | ||
"github.com/google/yamlfmt" | ||
"github.com/google/yamlfmt/internal/hotfix" | ||
) | ||
|
||
var ( | ||
featIncludeDocumentStart = yamlfmt.Feature{ | ||
Name: "Include Document Start", | ||
AfterAction: func(content []byte) ([]byte, error) { | ||
documentStart := "---\n" | ||
return append([]byte(documentStart), content...), nil | ||
}, | ||
} | ||
featEmojiSupport = yamlfmt.Feature{ | ||
Name: "Emoji Support", | ||
AfterAction: hotfix.ParseUnicodePoints, | ||
} | ||
featCRLFSupport = yamlfmt.Feature{ | ||
Name: "CRLF Support", | ||
BeforeAction: hotfix.StripCRBytes, | ||
AfterAction: hotfix.WriteCRLFBytes, | ||
} | ||
) | ||
|
||
func ConfigureFeaturesFromConfig(config *Config) yamlfmt.FeatureList { | ||
features := []yamlfmt.Feature{} | ||
if config.EmojiSupport { | ||
features = append(features, featEmojiSupport) | ||
} | ||
if config.IncludeDocumentStart { | ||
features = append(features, featIncludeDocumentStart) | ||
} | ||
if config.LineEnding == yamlfmt.LineBreakStyleCRLF { | ||
features = append(features, featCRLFSupport) | ||
} | ||
if config.RetainLineBreaks { | ||
linebreakStr := "\n" | ||
if config.LineEnding == yamlfmt.LineBreakStyleCRLF { | ||
linebreakStr = "\r\n" | ||
} | ||
featLineBreak := hotfix.MakeFeatureRetainLineBreak(linebreakStr, config.Indent) | ||
features = append(features, featLineBreak) | ||
} | ||
return features | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,22 +1,38 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package hotfix | ||
|
||
func StripCRBytes(crlfContent []byte) []byte { | ||
// yamlfmt.FeatureFunc | ||
func StripCRBytes(crlfContent []byte) ([]byte, error) { | ||
onlyLf := []byte{} | ||
for _, b := range crlfContent { | ||
if b != '\r' { | ||
onlyLf = append(onlyLf, b) | ||
} | ||
} | ||
return onlyLf | ||
return onlyLf, nil | ||
} | ||
|
||
func WriteCRLFBytes(lfContent []byte) []byte { | ||
// yamlfmt.FeatureFunc | ||
func WriteCRLFBytes(lfContent []byte) ([]byte, error) { | ||
crlfContent := []byte{} | ||
for _, b := range lfContent { | ||
if b == '\n' { | ||
crlfContent = append(crlfContent, '\r') | ||
} | ||
crlfContent = append(crlfContent, b) | ||
} | ||
return crlfContent | ||
return crlfContent, nil | ||
} |
Oops, something went wrong.