erlang自定义二进制协议
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 regels
4.9 KiB

  1. -ifndef(gpb_hrl).
  2. -define(gpb_hrl, true).
  3. -type gpb_scalar() ::
  4. int32 | int64 | uint32 | uint64 | sint32 | sint64
  5. | fixed32 | fixed64 | sfixed32 | sfixed64
  6. | bool
  7. | float | double
  8. | string
  9. | bytes.
  10. -type gpb_map_key() :: % "any scalar type except floating point types and bytes"
  11. int32 | int64 | uint32 | uint64 | sint32 | sint64
  12. | fixed32 | fixed64 | sfixed32 | sfixed64
  13. | bool
  14. | string.
  15. %% It is not possible to have maps in maps directly,
  16. %% so this type is any gpb_field_type() except {map,K,V}.
  17. -type gpb_map_value() ::
  18. gpb_scalar()
  19. | {enum,atom()}
  20. | {msg,atom()}.
  21. -type gpb_field_type() :: %% Erlang type Comment
  22. int32 | int64 % integer() variable-length encoded
  23. | uint32 | uint64 % integer() variable-length encoded
  24. | sint32 | sint64 % integer() variable-length zig-zag encoded
  25. | fixed32 | fixed64 % integer() always 4 | 8 bytes on wire
  26. | sfixed32 | sfixed64 % integer() always 4 | 8 bytes on wire
  27. | bool % true | false
  28. | float | double % float()
  29. | string % string() UTF-8 encoded
  30. % | binary() iff option `strings_as_binaries'
  31. | bytes % binary()
  32. | {enum,atom()} % atom() the enum literal is the atom
  33. | {msg,atom()} % record() the message name is record name
  34. % | map() iff option `maps'
  35. | {group,atom()} % record() name is <msg name>_<field name>
  36. % | map() iff option `maps'
  37. | {map,gpb_map_key(),gpb_map_value()}. % [{K,V}] | map()
  38. %% An intermediary type temporarily used internally within gpb during parsing,
  39. %% neither returned from gpb, nor accepted as input go gpb.
  40. -type gpb_internal_intermediary_ref() ::
  41. {ref, term()} |
  42. {msg, list()} |
  43. {group, list()} |
  44. {enum, list()}.
  45. -type gpb_internal_intermediary_map_ref() ::
  46. {map, gpb_map_key(), gpb_map_value() | gpb_internal_intermediary_ref()}.
  47. %% The following two definitions (`gpb_field' and `gpb_rpc') are to
  48. %% avoid clashes with other code, since the `field' and `rpc' are
  49. %% really too general names, they should have been prefixed.
  50. %%
  51. %% Unfortunately, they are already part of the API, so they can't
  52. %% be changed without breaking backwards compatibility.
  53. %% (They appear as parameters or return values for functions in `gpb'
  54. %% in generated code.)
  55. %%
  56. %% In case a clash, it is possible to redefine the name locally.
  57. %% The recommendation is to redefine them with prefix, ie to `gpb_field'
  58. %% and `gpb_rpc', since this is what they will change to in some future.
  59. %%
  60. -ifdef(gpb_field_record_name).
  61. -define(gpb_field, ?gpb_field_record_name).
  62. -else.
  63. -define(gpb_field, field). %% odd definition is due to backwards compatibility
  64. -endif.
  65. -ifdef(gpb_rpc_record_name).
  66. -define(gpb_rpc, ?gpb_rpc_record_name).
  67. -else.
  68. -define(gpb_rpc, rpc). %% odd definition is due to backwards compatibility
  69. -endif.
  70. -record(?gpb_field, % NB: record name is (currently) `field' (not `gpb_field')!
  71. {name :: atom()
  72. | undefined, % temporarily in some phases
  73. fnum :: integer()
  74. | undefined, % temporarily in some phases
  75. rnum :: pos_integer() % field number in the record
  76. | undefined, % temporarily, during parsing
  77. type :: gpb_field_type() |
  78. gpb_internal_intermediary_ref() |
  79. gpb_internal_intermediary_map_ref()
  80. | undefined, % temporarily in some phases
  81. occurrence :: 'required' | 'optional' | 'repeated'
  82. | undefined, % temporarily in some phases
  83. opts = [] :: [term()]
  84. }).
  85. -record(gpb_oneof,
  86. {name :: atom()
  87. | undefined, % temporarily in some phases
  88. rnum :: pos_integer() % field number in the record
  89. | undefined, % temporarily, during parsing
  90. fields :: [#?gpb_field{}] % all fields have the same rnum
  91. | undefined % temporarily in some phases
  92. }).
  93. -record(?gpb_rpc, % NB: record name is (currently) `rpc' (not `gpb_rpc')!
  94. {name :: atom()
  95. | undefined, % temporarily in some phases
  96. input,
  97. output,
  98. input_stream :: boolean()
  99. | undefined, % temporarily in some phases
  100. output_stream :: boolean()
  101. | undefined, % temporarily in some phases
  102. opts :: [term()]
  103. | undefined % temporarily in some phases
  104. }).
  105. -endif.