0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

API Best Practices ~フィールドはスカラやenumではなく、繰り返しメッセージにする

Posted at

フィールドはスカラやenumではなく、繰り返しメッセージにする

一般的に拡張していくと、単一の繰り返しフィールドが複数の関連する繰り返しフィールドへ足し算されていくかと思いますが、これはよくありません。

繰り返しメッセージから始めると、拡張性がよくなります。

// Describes a type of enhancement applied to a photo
enum EnhancementType {
  ENHANCEMENT_TYPE_UNSPECIFIED;
  RED_EYE_REDUCTION;
  SKIN_SOFTENING;
}

message PhotoEnhancement {
  optional EnhancementType type;
}

message PhotoEnhancementReply {
  // Good: repeated message で定義しておくと、後からフィールド追加できたり
  // enum より拡張性が高い
  repeated PhotoEnhancement enhancements;

  // Bad: もしパラメーター追加したい場合は、同じように repeated hogeType を追加するか(ひどい)
  // このフィールドを非推奨にして繰り返しメッセージを導入する必要がある。
  repeated EnhancementType enhancement_types;
}

これは map にも同様に適用されます。 map<string, string> よりは map<string, MyProto> にしておくとよいでしょう。

例外として、レイテンシクリティカルなアプリケーションでは、プリミティブ型の並列配列は、単一のメッセージ配列よりも高速です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?