-
Notifications
You must be signed in to change notification settings - Fork 18.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add truncate
function for Go templates#30484
Conversation
3f4db31
to
5ccc8bb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Design LGTM 🐮
/cc @thaJeztah @dnephin @cpuguy83 @tiborvass @AkihiroSuda @runcom @icecrime
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests could use the assertion helpers. I think it makes them a lot easier to read when they are shorter.
The error messages are also consistent that way, and include things like "Expected no error, but got: ..."
tm, err := Parse(`{{shorten .}}`) | ||
if err != nil { | ||
t.Fatal(err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.NilError(t, err)
var b bytes.Buffer | ||
if err := tm.Execute(&b, "tupx5xzf6hvsrhnruz5cr8gwp"); err != nil { | ||
t.Fatal(err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.NilError(t, tm.Execute(&b, "tupx5xzf6hvsrhnruz5cr8gwp"))
want := "tupx5xzf6hvs" | ||
if b.String() != want { | ||
t.Fatalf("expected %s, got %s", want, b.String()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.Equal(t, b.String(), "tupx5xzf6hvs")
"lower": strings.ToLower, | ||
"upper": strings.ToUpper, | ||
"pad": padWithSpace, | ||
"shorten": stringid.TruncateID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this works on any string, I think it should take an integer argument to shorten to a certain length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not to say that TruncateID
should take an argument ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point 👍
5ccc8bb
to
3dd0c8a
Compare
Thanks @dnephin the PR has been updated with unit tests using assertion helpers. Other tests in |
Thanks @cpuguy83 @vdemeester for the comment. Now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The correct word your looking for is |
I agree that Moving it back to design for a bit |
We currently have
I am thinking maybe we could consider |
👍 for |
This fix is part of the discussion in 28199 about using `truncate` to replace `--no-trunc`. As part of the fix, a new function `truncate` has been added for Go templates so that it is possible to use ``` docker stack services --format "{{truncate .ID 5}}: {{.Mode}} {{.Replicas}}" ``` to show truncated ID. A unit test has been added. This fix is related to 28199. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
3dd0c8a
to
7fa8d5e
Compare
shorten
function for Go templatestruncate
function for Go templates
Thanks all for the review. The PR has been updated with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@cpuguy83 @dnephin @thaJeztah @vdemeester feel free to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
This fix is part of the discussion in #28199 about using
truncate
to replace--no-trunc
.As part of the fix, a new function
truncate
has been added for Go templates so that it is possible to useto show truncated ID (limited by length).
A unit test has been added.
This fix is related to #28199.
Signed-off-by: Yong Tang yong.tang.github@outlook.com