| 1 | /* |
| 2 | Copyright The containerd Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package mainscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild/cmd/go-fix-acronym`/ |
| 18 | |
| 19 | import ( |
| 20 | "go/astscip-go gomod github.com/golang/go/src go1.13 `go/ast`/" |
| 21 | "regexpscip-go gomod github.com/golang/go/src go1.13 regexp/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 23 | |
| 24 | "golang.org/x/tools/go/ast/astutilscip-go gomod golang.org/x/tools v0.1.9 `golang.org/x/tools/go/ast/astutil`/" |
| 25 | ) |
| 26 | |
| 27 | func compilePatternscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild/cmd/go-fix-acronym`/compilePattern().(c configscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild/cmd/go-fix-acronym`/config#) (*regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#, error) { |
| 28 | return regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.Compilescip-go gomod github.com/golang/go/src go1.13 regexp/Compile().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Joinscip-go gomod github.com/golang/go/src go1.13 strings/Join().(c.acronymsscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild/cmd/go-fix-acronym`/config#acronyms., "|")) |
| 29 | } |
| 30 | |
| 31 | func rewriteNodescip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild/cmd/go-fix-acronym`/rewriteNode().(pattern *regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#, node astscip-go gomod github.com/golang/go/src go1.13 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Node#) { |
| 32 | astutilscip-go gomod golang.org/x/tools v0.1.9 `golang.org/x/tools/go/ast/astutil`/.Applyscip-go gomod golang.org/x/tools v0.1.9 `golang.org/x/tools/go/ast/astutil`/Apply().( |
| 33 | node, |
| 34 | func(c *astutilscip-go gomod golang.org/x/tools v0.1.9 `golang.org/x/tools/go/ast/astutil`/.Cursorscip-go gomod golang.org/x/tools v0.1.9 `golang.org/x/tools/go/ast/astutil`/Cursor#) bool { |
| 35 | node := c.Nodescip-go gomod golang.org/x/tools v0.1.9 `golang.org/x/tools/go/ast/astutil`/Cursor#Node().() |
| 36 | ident, ok := node.(*astscip-go gomod github.com/golang/go/src go1.13 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#) |
| 37 | if !ok { |
| 38 | return true |
| 39 | } |
| 40 | |
| 41 | matches := pattern.FindAllStringSubmatchIndexscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#FindAllStringSubmatchIndex().(ident.Namescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#Name., -1) |
| 42 | if matches == nil { |
| 43 | return true |
| 44 | } |
| 45 | |
| 46 | var ( |
| 47 | result string |
| 48 | copied int |
| 49 | ) |
| 50 | for _, match := range matches { |
| 51 | // If there are submatches, ignore the first element and only process the submatches. |
| 52 | if len(match) > 2 { |
| 53 | match = match[2:] |
| 54 | } |
| 55 | |
| 56 | for i := 0; i < len(match); i += 2 { |
| 57 | if match[i] == -1 { |
| 58 | continue |
| 59 | } |
| 60 | result += ident.Namescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#Name.[copied:match[i]] |
| 61 | result += stringsscip-go gomod github.com/golang/go/src go1.13 strings/.ToUpperscip-go gomod github.com/golang/go/src go1.13 strings/ToUpper().(ident.Namescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#Name.[match[i]:match[i+1]]) |
| 62 | copied = match[i+1] |
| 63 | } |
| 64 | } |
| 65 | // Copy the rest. |
| 66 | result += ident.Namescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#Name.[copied:len(ident.Namescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#Name.)] |
| 67 | ident.Namescip-go gomod github.com/golang/go/src go1.13 `go/ast`/Ident#Name. = result |
| 68 | return false |
| 69 | }, |
| 70 | nil, |
| 71 | ) |
| 72 | } |