hairyhenderson/gomplate

Provide TrimLeft and TrimRight Support

jsolana opened this issue · 8 comments

Situation

We are using gomplate to process *.proto file to generate YAMLfiles. These proto files may belong to third party providers.
At the end, to check everything is correct, we are using yamllint to check it.

Sometimes, *.proto files contains trailing spaces and to avoid fails during the validation using yamllint, we are using TrimSpace but the problem is, we are loosing left indentation. Eg:

message MessageA{
string field1 = 1;
string field2 = 2;
...
}

Other operations as Trim affect also left and right and we can't use TrimSuffix to fix it.

Solution

Provide:

  • strings.TrimLeft
  • strings.TrimRight

Wdyt?

If make sense I can do it by myself :)

Thanks @jsolana and thanks for the PR! I'm fine with adding these, but I'm curious about the use-case. Can you provide an example of the template you're using to process the .proto files?

I want to create Backstage API entities from *.proto files eg:

apiVersion: backstage.io/v1alpha1
kind: API
metadata:
  name: helloworld-grpc-api
  namespace: dev-x
spec:
  type: grpc
  lifecycle: testing
  owner: group:default/dev-x
  definition: |

    syntax = "proto3";

    option go_package = "gitlab.otters.xyz/platform/dev-x/deployment/helloworld_grpc.git/proto/v1/helloworld";

    package helloworld;

    // The HelloWorld service definition.
    service HelloWorld {
      // Sends a greeting
      rpc SayHello (HelloRequest) returns (HelloReply) {}
    }

    // The request message containing the user's name.
    message HelloRequest {
      string name = 1;
    }

    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }

---

Im generating them using gomplate, the problem is, if the origin where the proto files belong (may be third party services) contains trailing spaces etc,.. running yamlling will fails

Right now Im doing:

{{- $content := file.Read $file -}}
{{- $lines := strings.Split "\n" $content -}}
{{- range $line := $lines -}}
    {{- $trimmed := $line | strings.TrimSpace -}}
    {{- if ne $trimmed "" -}}
        {{"\n"}}{{ $trimmed | indent 4 }}
    {{- else -}}
{{"\n"}}
    {{- end -}}
{{- end -}}

The problem with it is, we are losing the indentation defined in the origin then, the Backstage API generated is like this one:

apiVersion: backstage.io/v1alpha1
kind: API
metadata:
  name: helloworld-grpc-api
  namespace: dev-x
spec:
  type: grpc
  lifecycle: testing
  owner: group:default/dev-x
  definition: |

    syntax = "proto3";

    option go_package = "gitlab.otters.xyz/platform/dev-x/deployment/helloworld_grpc.git/proto/v1/helloworld";

    package helloworld;

    // The HelloWorld service definition.
    service HelloWorld {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }

    // The request message containing the user's name.
    message HelloRequest {
    string name = 1;
    }

    // The response message containing the greetings
    message HelloReply {
    string message = 1;
    }

---

(message's field and service op with no indentation)

A couple thoughts:

  • you shouldn't need file.Read - include should work (though it should work either way)
  • the {{- and -}}s that you're using are what are ultimately responsible for losing the indentation

Obviously what you've posted is a subset of what you're actually doing, but I wonder why you need to iterate through each line, and why not simply pass the whole proto file through indent 4?

That was the first approach but it didn't work keeping trailing spaces at the end of some lines

{{- $content := file.Read $file -}}
...
{{ $content | indent 4 }}

Running yamllint -c .yamllint catalog-info.yaml I have:

  164:78    error    trailing spaces  (trailing-spaces)
  184:1     error    trailing spaces  (trailing-spaces)
  277:78    error    trailing spaces  (trailing-spaces)
  297:1     error    trailing spaces  (trailing-spaces)

The problem is, I can't fix the *.proto files in the source and the only way to fix it meanwhile is reading line by line and using TrimSpace function (losing the original indentation)

Original indentation I meant the whitespaces in the original proto file (not related to next indent gomplate calls)

@hairyhenderson let me know if you need more information to clarify the why
Thanks a lot!

This line:

{{- $content := file.Read $file -}}

might be responsible for what you're seeing - the trailing -}} causes all following whitespace to be elided, including newlines and whitespace following that.

Either way, TrimLeft/Right are fine additions. I'll re-review the PR after I release v4.0.1!

Thanks mate!