github.com/googleapis/api-linter@v1.65.2/rules/internal/utils/comments.go (about)

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import "strings"
    18  
    19  // SeparateInternalComments splits the given comment block into "external" and
    20  // "internal" comments based on https://google.aip.dev/192#internal-comments.
    21  func SeparateInternalComments(comments ...string) struct {
    22  	Internal []string
    23  	External []string
    24  } {
    25  	answer := struct {
    26  		Internal []string
    27  		External []string
    28  	}{}
    29  	for _, c := range comments {
    30  		for len(c) > 0 {
    31  			// Anything before the `(--` is external string.
    32  			open := strings.SplitN(c, "(--", 2)
    33  			if ex := strings.TrimSpace(open[0]); ex != "" {
    34  				answer.External = append(answer.External, ex)
    35  			}
    36  			if len(open) > 1 {
    37  				c = strings.TrimSpace(open[1])
    38  			} else {
    39  				break
    40  			}
    41  
    42  			// Now that the opening component is tokenized, anything before
    43  			// the `--)` is internal string.
    44  			close := strings.SplitN(c, "--)", 2)
    45  			if in := strings.TrimSpace(close[0]); in != "" {
    46  				answer.Internal = append(answer.Internal, in)
    47  			}
    48  			if len(close) > 1 {
    49  				c = strings.TrimSpace(close[1])
    50  			} else {
    51  				break
    52  			}
    53  		}
    54  	}
    55  	return answer
    56  }