Skip to content
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

feat: support pretty print json string #13

Merged
merged 9 commits into from
Jul 3, 2023
Merged

feat: support pretty print json string #13

merged 9 commits into from
Jul 3, 2023

Conversation

takaishi
Copy link
Collaborator

@takaishi takaishi commented May 1, 2023

This PR supports to print json string as pretty.

@takaishi takaishi self-assigned this May 1, 2023
@takaishi takaishi requested review from okkez and akihiro17 May 1, 2023 05:40
@takaishi takaishi marked this pull request as ready for review May 1, 2023 05:40
return old, nil
}

func copyPlan(old *tfjson.Plan) (*tfjson.Plan, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Q] deep copyが必要な理由ってなんでしょうか。そのまま書き換えるのだと駄目なんでしょうか。このデータ構造いじるのはterrraform-j2mdだけなのでオリジナルの方を保存しないといけないみたいなこともないんじゃないかと思いました。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/hashicorp/terraform-json/tree/main/sanitize
実装を揃えてdeep copyするようにしています。個人的に明確に目的があってdeep copyしているわけではないです。

Copy link
Contributor

@akihiro17 akihiro17 Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/hashicorp/terraform-json/blob/327989a875ab436289d7e01233c3655ec5ed167e/sanitize/copy.go

sanitizeで使われているのはdeep copyじゃなくてshallow copyなので実装が揃っていなさそうであるのと、sanitizeの方は明確な意図があってだたのcopyじゃないのを選択しているので、明確な意図がないのであれば普通のコピーで実装できないか検討して欲しいです。

ref. hashicorp/terraform-json#34 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あれ、shallow copyだ… なんかボケてたみたいです。対応します

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

明確な意図がないのであれば普通のコピーで実装できないか検討して欲しいです。

実装合わせるんじゃなくて普通のコピーでできないかまずは検討して欲しいです

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コピーも必要なさそうだったので、オリジナルのデータをそのまま書き換えるようにしました。

@@ -0,0 +1,97 @@
package pretty_print
Copy link
Contributor

@akihiro17 akihiro17 May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages are given lower case, single-word names; there should be no need for underscores or mixedCaps

https://go.dev/doc/effective_go#names

goのパッケージ名にはアンダースコア入れないようです。

あと、printはしてないのでpretty_printじゃなくてprettyplanとかformatterとかformatの方が適切じゃないかなと思いました。もしくはterraformパッケージ内にこの関数書いてしまうとか。


func prettyChangeValue(old interface{}) (interface{}, error) {
switch x := old.(type) {
case []interface{}:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits] 1.18からanyが使えるのでanyで良いんじゃないかと思いました。

https://go.dev/ref/spec#Predeclared_identifiers

}
case string:
var j json.RawMessage
if err := json.Unmarshal([]byte(old.(string)), &j); err == nil && json.Valid([]byte(old.(string))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json.Validしてjson文字列じゃなかったらreturnして、json.Validがtrueならjson.Unmarshalするんじゃないかと思ったのですが、先にjson.Unmarshalしている理由は何でしょうか。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

明確な意図はないですね。先にValidを呼ぶ方が自然に思えるのでそうしようと思いました

@takaishi
Copy link
Collaborator Author

takaishi commented Jun 2, 2023

すみません、通知の設定ミスで気づいていませんでした。確認します。

@takaishi takaishi requested a review from akihiro17 June 6, 2023 12:58
@@ -114,8 +115,12 @@ func NewPlanData(input []byte) (*PlanData, error) {
if err != nil {
return nil, fmt.Errorf("failed to sanitize plan: %w", err)
}
FormattedJsonPlan, err := format.FormatJsonPlan(sanitizedPlan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits] local変数の始まりが大文字になってるのが気になりました。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました。

"testing"
)

func Test_formatJsonChangeValue(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

外に公開していないメソッドのテストしかないのが気になりました。FormatJsonPlanに対してテストを書ければ十分だと思うのでこの場合はFormatJsonPlanに対してテストを書いた方が良いと思いました。

また、テストのパッケージはできるだけxxx_testとして別のパッケージにして欲しいです。
ref. https://engineering.mercari.com/blog/entry/2018-08-08-080000/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この記事は初めて読みましたが、確かにわけるメリットはありますね。
分割しました。

@takaishi takaishi requested a review from akihiro17 July 3, 2023 02:51
Copy link
Contributor

@akihiro17 akihiro17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動作確認しました。良さそうです 👍

go mod tidyを実行すると差分出るのでそれだけ最後修正お願いします。

git diff
diff --git a/go.mod b/go.mod
index b8c428f..1e71984 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,6 @@ go 1.18

 require (
        github.com/hashicorp/terraform-json v0.14.0
-       github.com/jinzhu/copier v0.3.5
        github.com/pmezard/go-difflib v1.0.0
 )

diff --git a/go.sum b/go.sum
index 7ffec86..f3fb121 100644
--- a/go.sum
+++ b/go.sum
@@ -13,8 +13,6 @@ github.com/hashicorp/go-version v1.5.0 h1:O293SZ2Eg+AAYijkVK3jR786Am1bhDEh2GHT0t
 github.com/hashicorp/go-version v1.5.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
 github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
 github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
-github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
-github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=

@takaishi takaishi merged commit 77cfdc9 into master Jul 3, 2023
@takaishi takaishi deleted the pretty-json branch July 3, 2023 07:59
@takaishi
Copy link
Collaborator Author

takaishi commented Jul 3, 2023

thank you!

@akihiro17
Copy link
Contributor

v0.0.5をリリースしました
https://github.com/reproio/terraform-j2md/releases/tag/v0.0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants