-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add feature `strip_directives` This PR adds the `strip_directives` feature, a hotfix that will allow yamlfmt to function with directives in the document for some cases. It's a very imperfect feature but should help in some minimal cases. * add missing license header
- Loading branch information
Showing
18 changed files
with
180 additions
and
19 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
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
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
4 changes: 4 additions & 0 deletions
4
integrationtest/command/testdata/strip_directives/after/a.yaml
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,4 @@ | ||
%YAML:1.0 | ||
a: 1 | ||
%TAG:hi | ||
b: 2 |
4 changes: 4 additions & 0 deletions
4
integrationtest/command/testdata/strip_directives/before/a.yaml
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,4 @@ | ||
%YAML:1.0 | ||
a: 1 | ||
%TAG:hi | ||
b: 2 |
Empty file.
Empty file.
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,101 @@ | ||
// Copyright 2024 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 | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"strings" | ||
|
||
"github.com/google/yamlfmt" | ||
) | ||
|
||
type directiveKey string | ||
|
||
var contextDirectivesKey directiveKey = "directives" | ||
|
||
type Directive struct { | ||
line int | ||
content string | ||
} | ||
|
||
func ContextWithDirectives(ctx context.Context, directives []Directive) context.Context { | ||
return context.WithValue(ctx, contextDirectivesKey, directives) | ||
} | ||
|
||
func DirectivesFromContext(ctx context.Context) []Directive { | ||
return ctx.Value(contextDirectivesKey).([]Directive) | ||
} | ||
|
||
func MakeFeatureStripDirectives(lineSepChar string) yamlfmt.Feature { | ||
return yamlfmt.Feature{ | ||
Name: "Strip Directives", | ||
BeforeAction: stripDirectivesFeature(lineSepChar), | ||
AfterAction: restoreDirectivesFeature(lineSepChar), | ||
} | ||
} | ||
|
||
func stripDirectivesFeature(lineSepChar string) yamlfmt.FeatureFunc { | ||
return func(ctx context.Context, content []byte) (context.Context, []byte, error) { | ||
directives := []Directive{} | ||
reader := bytes.NewReader(content) | ||
scanner := bufio.NewScanner(reader) | ||
result := "" | ||
currLine := 1 | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "%") { | ||
directives = append(directives, Directive{ | ||
line: currLine, | ||
content: line, | ||
}) | ||
} else { | ||
result += line + lineSepChar | ||
} | ||
currLine++ | ||
} | ||
return ContextWithDirectives(ctx, directives), []byte(result), nil | ||
} | ||
} | ||
|
||
func restoreDirectivesFeature(lineSepChar string) yamlfmt.FeatureFunc { | ||
return func(ctx context.Context, content []byte) (context.Context, []byte, error) { | ||
directives := DirectivesFromContext(ctx) | ||
directiveIdx := 0 | ||
doneDirectives := directiveIdx == len(directives) | ||
reader := bytes.NewReader(content) | ||
scanner := bufio.NewScanner(reader) | ||
result := "" | ||
currLine := 1 | ||
for scanner.Scan() { | ||
if !doneDirectives && currLine == directives[directiveIdx].line { | ||
result += directives[directiveIdx].content + lineSepChar | ||
currLine++ | ||
directiveIdx++ | ||
doneDirectives = directiveIdx == len(directives) | ||
} | ||
result += scanner.Text() + lineSepChar | ||
currLine++ | ||
} | ||
// Edge case: There technically can be a directive as the final line. This would be | ||
// useless as far as I can tell so maybe yamlfmt should just remove it anyway LOL but | ||
// no we'll keep it. | ||
if !doneDirectives && currLine == directives[directiveIdx].line { | ||
result += directives[directiveIdx].content + lineSepChar | ||
} | ||
return ctx, []byte(result), nil | ||
} | ||
} |