Skip to content

Commit

Permalink
chore: remove version from the proto package name (#405)
Browse files Browse the repository at this point in the history
Important note that I removed keeping unknown fields in serverside
protobuf, because we're not doing roundtrips of the same message so
those unknown fields are useless
  • Loading branch information
icehaunter authored Sep 7, 2023
1 parent 2662251 commit 53b2257
Show file tree
Hide file tree
Showing 31 changed files with 5,813 additions and 7,807 deletions.
282 changes: 130 additions & 152 deletions clients/typescript/src/_generated/protocol/satellite.ts

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions clients/typescript/src/satellite/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
SatPingResp,
SatRelation,
SatRelationColumn,
SatAuthHeader,
SatAuthHeaderPair,
SatSubsResp,
SatSubsReq,
SatSubsDataError,
Expand All @@ -32,7 +30,6 @@ import {
getSizeBuf,
getTypeFromCode,
SatPbMsg,
getProtocolVersion,
getFullTypeName,
startReplicationErrorToSatelliteError,
shapeRequestToSatShapeReq,
Expand Down Expand Up @@ -355,16 +352,10 @@ export class SatelliteClient extends EventEmitter implements Client {
}

authenticate({ clientId, token }: AuthState): Promise<AuthResponse> {
const headers = [
SatAuthHeaderPair.fromPartial({
key: SatAuthHeader.PROTO_VERSION,
value: getProtocolVersion(),
}),
]
const request = SatAuthReq.fromPartial({
id: clientId,
token: token,
headers: headers,
headers: [],
})
return this.rpc<AuthResponse>(request)
}
Expand Down
44 changes: 22 additions & 22 deletions clients/typescript/src/util/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { base64, typeDecoder } from './common'
import { getMaskBit } from './bitmaskHelpers'

type GetName<T extends { $type: string }> =
T['$type'] extends `Electric.Satellite.v1_4.${infer K}` ? K : never
T['$type'] extends `Electric.Satellite.${infer K}` ? K : never
type MappingTuples = {
[k in SatPbMsg as GetName<k>]: [number, SatPbMsgObj<k>]
}
Expand Down Expand Up @@ -301,57 +301,57 @@ export function shapeRequestToSatShapeReq(

export function msgToString(message: SatPbMsg): string {
switch (message.$type) {
case 'Electric.Satellite.v1_4.SatAuthReq':
case 'Electric.Satellite.SatAuthReq':
return `#SatAuthReq{id: ${message.id}, token: ${message.token}}`
case 'Electric.Satellite.v1_4.SatAuthResp':
case 'Electric.Satellite.SatAuthResp':
return `#SatAuthResp{id: ${message.id}}`
case 'Electric.Satellite.v1_4.SatErrorResp':
case 'Electric.Satellite.SatErrorResp':
return `#SatErrorResp{type: ${
Pb.SatErrorResp_ErrorCode[message.errorType]
}}`
case 'Electric.Satellite.v1_4.SatInStartReplicationReq': {
case 'Electric.Satellite.SatInStartReplicationReq': {
const schemaVersion = message.schemaVersion
? ` schema: ${message.schemaVersion},`
: ''
return `#SatInStartReplicationReq{lsn: ${base64.fromBytes(
message.lsn
)},${schemaVersion} subscriptions: [${message.subscriptionIds}]}`
}
case 'Electric.Satellite.v1_4.SatInStartReplicationResp':
case 'Electric.Satellite.SatInStartReplicationResp':
return `#SatInStartReplicationResp{${
message.err
? '`' + startReplicationErrorToSatelliteError(message.err) + '`'
: ''
}}`
case 'Electric.Satellite.v1_4.SatInStopReplicationReq':
case 'Electric.Satellite.SatInStopReplicationReq':
return `#SatInStopReplicationReq{}`
case 'Electric.Satellite.v1_4.SatInStopReplicationResp':
case 'Electric.Satellite.SatInStopReplicationResp':
return `#SatInStopReplicationResp{}`
case 'Electric.Satellite.v1_4.SatMigrationNotification':
case 'Electric.Satellite.SatMigrationNotification':
return `#SatMigrationNotification{to: ${message.newSchemaVersion}, from: ${message.newSchemaVersion}}`
case 'Electric.Satellite.v1_4.SatPingReq':
case 'Electric.Satellite.SatPingReq':
return `#SatPingReq{}`
case 'Electric.Satellite.v1_4.SatPingResp':
case 'Electric.Satellite.SatPingResp':
return `#SatPingResp{lsn: ${
message.lsn ? base64.fromBytes(message.lsn) : 'NULL'
}}`
case 'Electric.Satellite.v1_4.SatRelation': {
case 'Electric.Satellite.SatRelation': {
const cols = message.columns
.map((x) => `${x.name}: ${x.type}${x.primaryKey ? ' PK' : ''}`)
.join(', ')
return `#SatRelation{for: ${message.schemaName}.${message.tableName}, as: ${message.relationId}, cols: [${cols}]}`
}
case 'Electric.Satellite.v1_4.SatSubsDataBegin':
case 'Electric.Satellite.SatSubsDataBegin':
return `#SatSubsDataBegin{id: ${
message.subscriptionId
}, lsn: ${base64.fromBytes(message.lsn)}}`
case 'Electric.Satellite.v1_4.SatSubsDataEnd':
case 'Electric.Satellite.SatSubsDataEnd':
return `#SatSubsDataEnd{}`
case 'Electric.Satellite.v1_4.SatShapeDataBegin':
case 'Electric.Satellite.SatShapeDataBegin':
return `#SatShapeDataBegin{id: ${message.requestId}}`
case 'Electric.Satellite.v1_4.SatShapeDataEnd':
case 'Electric.Satellite.SatShapeDataEnd':
return `#SatShapeDataEnd{}`
case 'Electric.Satellite.v1_4.SatSubsDataError': {
case 'Electric.Satellite.SatSubsDataError': {
const shapeErrors = message.shapeRequestError.map(
(x) =>
`${x.requestId}: ${Pb.SatSubsDataError_ShapeReqError_Code[x.code]} (${
Expand All @@ -361,11 +361,11 @@ export function msgToString(message: SatPbMsg): string {
const code = Pb.SatSubsDataError_Code[message.code]
return `#SatSubsDataError{id: ${message.subscriptionId}, code: ${code}, msg: "${message.message}", errors: [${shapeErrors}]}`
}
case 'Electric.Satellite.v1_4.SatSubsReq':
case 'Electric.Satellite.SatSubsReq':
return `#SatSubsReq{id: ${
message.subscriptionId
}, shapes: ${JSON.stringify(message.shapeRequests)}}`
case 'Electric.Satellite.v1_4.SatSubsResp': {
case 'Electric.Satellite.SatSubsResp': {
if (message.err) {
const shapeErrors = message.err.shapeRequestError.map(
(x) =>
Expand All @@ -380,11 +380,11 @@ export function msgToString(message: SatPbMsg): string {
return `#SatSubsReq{id: ${message.subscriptionId}}`
}
}
case 'Electric.Satellite.v1_4.SatUnsubsReq':
case 'Electric.Satellite.SatUnsubsReq':
return `#SatUnsubsReq{ids: ${message.subscriptionIds}}`
case 'Electric.Satellite.v1_4.SatUnsubsResp':
case 'Electric.Satellite.SatUnsubsResp':
return `#SatUnsubsResp{}`
case 'Electric.Satellite.v1_4.SatOpLog':
case 'Electric.Satellite.SatOpLog':
return `#SatOpLog{ops: [${message.ops.map(opToString).join(', ')}]}`
}
}
Expand Down
2 changes: 1 addition & 1 deletion clients/typescript/test/migrators/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const migrationMetaData = {
})
),
],
protocol_version: 'Electric.Satellite.v1_4',
protocol_version: 'Electric.Satellite',
version: '20230613112725_814',
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"ops": [
"GocBCgVzdGFycxISCgJpZBIEVEVYVBoGCgR0ZXh0EhoKCmF2YXRhcl91cmwSBFRFWFQaBgoEdGV4dBIUCgRuYW1lEgRURVhUGgYKBHRleHQSGgoKc3RhcnJlZF9hdBIEVEVYVBoGCgR0ZXh0EhgKCHVzZXJuYW1lEgRURVhUGgYKBHRleHQiAmlkChIyMDIzMDYxMzExMjcyNV84MTQS1QES0gFDUkVBVEUgVEFCTEUgInN0YXJzIiAoCiAgImlkIiBURVhUIE5PVCBOVUxMLAogICJhdmF0YXJfdXJsIiBURVhUIE5PVCBOVUxMLAogICJuYW1lIiBURVhULAogICJzdGFycmVkX2F0IiBURVhUIE5PVCBOVUxMLAogICJ1c2VybmFtZSIgVEVYVCBOT1QgTlVMTCwKICBDT05TVFJBSU5UICJzdGFyc19wa2V5IiBQUklNQVJZIEtFWSAoImlkIikKKSBXSVRIT1VUIFJPV0lEOwo="
],
"protocol_version": "Electric.Satellite.v1_4",
"protocol_version": "Electric.Satellite",
"version": "20230613112725_814"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"ops": [
"Gk4KBWJlZXJzEhIKAmlkEgRURVhUGgYKBHRleHQSFwoHc3Rhcl9pZBIEVEVYVBoGCgR0ZXh0GhQKB3N0YXJfaWQSBXN0YXJzGgJpZCICaWQKEjIwMjMwNjEzMTEyNzM1Xzk5MhLVARLSAUNSRUFURSBUQUJMRSAiYmVlcnMiICgKICAiaWQiIFRFWFQgTk9UIE5VTEwsCiAgInN0YXJfaWQiIFRFWFQsCiAgQ09OU1RSQUlOVCAiYmVlcnNfc3Rhcl9pZF9ma2V5IiBGT1JFSUdOIEtFWSAoInN0YXJfaWQiKSBSRUZFUkVOQ0VTICJzdGFycyIgKCJpZCIpLAogIENPTlNUUkFJTlQgImJlZXJzX3BrZXkiIFBSSU1BUlkgS0VZICgiaWQiKQopIFdJVEhPVVQgUk9XSUQ7Cg=="
],
"protocol_version": "Electric.Satellite.v1_4",
"protocol_version": "Electric.Satellite",
"version": "20230613112735_992"
}
2 changes: 1 addition & 1 deletion clients/typescript/test/satellite/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ test.serial('default and null test', async (t) => {
})

const serializedRow: Proto.SatOpRow = {
$type: 'Electric.Satellite.v1_4.SatOpRow',
$type: 'Electric.Satellite.SatOpRow',
nullsBitmask: new Uint8Array([40]),
values: [
new Uint8Array([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ const migrationWithFKs: SchemaChange[] = [
columns: [{ name: 'id' }, { name: 'item_id' }],
fks: [
{
$type: 'Electric.Satellite.v1_4.SatOpMigrate.ForeignKey',
$type: 'Electric.Satellite.SatOpMigrate.ForeignKey',
fkCols: ['item_id'],
pkTable: 'test_items',
pkCols: ['id'],
Expand Down
5 changes: 1 addition & 4 deletions components/electric/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ rm_offset_storage:
rm offset_storage_*

update_protobuf: deps
mix electric.gen.proto.package \
--output-path=./lib/electric/satellite/protobuf_package.ex \
${PROTO_FILE}
mix protox.generate \
--output-path=./lib/electric/satellite/protobuf_messages.ex \
--keep-unknown-fields=false \
${PROTO_FILE}
sed -E -i "s/Electric.Satellite.V[0-9]+/$$(cat lib/electric/satellite/protobuf_package.ex | tail -n +2 | head -n 1 | sed -E 's/defmodule (.*) do/\1/')/" ./lib/electric/satellite/protobuf.ex
mix protox.generate \
--output-path=./lib/electric/postgres/schema/proto/messages.ex \
--namespace Electric.Postgres.Schema.Proto \
Expand Down
2 changes: 1 addition & 1 deletion components/electric/lib/electric/plug/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Electric.Plug.Migrations do
version: version,
ops: &1,
format: "SatOpMigrate",
protocol_version: PB.get_long_proto_vsn()
protocol_version: "Electric.Satellite"
}
)
|> Jason.encode!()
Expand Down
57 changes: 2 additions & 55 deletions components/electric/lib/electric/satellite/protobuf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ defmodule Electric.Satellite.Protobuf do
# This is a version provided in the corresponding protocol buffer file
# Make sure to bump it here and in the using macro below.

import Electric.Satellite.V14

alias Electric.Satellite.V14.{
alias Electric.Satellite.{
SatErrorResp,
SatAuthReq,
SatAuthResp,
Expand Down Expand Up @@ -84,7 +82,7 @@ defmodule Electric.Satellite.Protobuf do
quote do
alias Electric.Satellite.Protobuf, as: PB

alias Electric.Satellite.V14.{
alias Electric.Satellite.{
SatErrorResp,
SatAuthReq,
SatAuthHeaderPair,
Expand Down Expand Up @@ -177,55 +175,4 @@ defmodule Electric.Satellite.Protobuf do
{:ok, type, iodata} = encode(msg)
{:ok, [<<type::size(8)>> | iodata]}
end

@spec get_long_proto_vsn() :: String.t()
def get_long_proto_vsn() do
package()
end

@spec get_proto_vsn() :: {:ok, Version.t()} | {:error, term()}
def get_proto_vsn() do
parse_proto_vsn(package())
end

@doc """
Version is expected to be of the following format:
"Namespace.vMajor_Minor"
where:
- Namespace is one or multiple napespaces joined by dot
- MAJOR is a major version, integers only
- MINOR is a minor version, integers only
"""
@spec parse_proto_vsn(String.t()) :: {:ok, Version.t()} | {:error, term()}
def parse_proto_vsn(version) do
try do
version =
version
|> String.split(".")
|> List.last()

parse = Regex.named_captures(~r/^v(?<major>\d*)_(?<minor>\d*)$/, version)

{:ok,
%Version{
major: String.to_integer(parse["major"]),
minor: String.to_integer(parse["minor"])
}}
rescue
_ ->
Logger.warning("failed to encode: #{inspect(version)}")
{:error, :bad_version}
end
end

@doc """
Check if client's version of protocol is compatible with current version
"""
@spec is_compatible(Version.t(), Version.t()) :: boolean()
def is_compatible(
%Version{major: srv_maj, minor: srv_min},
%Version{major: cli_maj, minor: cli_min}
) do
srv_maj == cli_maj and srv_min >= cli_min
end
end
Loading

0 comments on commit 53b2257

Please sign in to comment.