device: added proto schema for updating device values

This commit is contained in:
Tigor Hutasuhut 2024-08-08 10:40:22 +07:00
parent 107330e49b
commit cc75353de1
6 changed files with 78 additions and 6 deletions

3
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/bufbuild/protovalidate-go v0.6.3
github.com/fatih/color v1.17.0
github.com/jaswdr/faker/v2 v2.3.0
github.com/mattn/go-sqlite3 v1.14.16
github.com/mattn/go-sqlite3 v1.14.17
github.com/rs/cors v1.11.0
github.com/spf13/cobra v1.8.1
github.com/stephenafamo/bob v0.28.1
@ -23,6 +23,7 @@ require (
github.com/aarondl/json v0.0.0-20221020222930-8b0db17ef1bf // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/google/cel-go v0.20.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect

8
go.sum
View file

@ -34,8 +34,8 @@ github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsM
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/google/cel-go v0.20.1 h1:nDx9r8S3L4pE61eDdt8igGj8rf5kjYR3ILxWIpWNi84=
github.com/google/cel-go v0.20.1/go.mod h1:kWcIzTsPX0zmQ+H3TirHstLLf9ep5QTsZBN9u4dOYLg=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
@ -63,8 +63,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=

View file

@ -1,6 +1,7 @@
package converter
import (
"github.com/aarondl/opt/omit"
"github.com/tigorlazuardi/bluemage/go/gen/models"
device "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1"
)
@ -8,6 +9,10 @@ import (
// goverter:converter
// goverter:extend BoolToInt8
// goverter:extend Int8ToBool
// goverter:extend PtrBoolToOmitInt8
// goverter:extend PtrStringToOmitString
// goverter:extend PtrFloat64ToOmitFloat64
// goverter:extend PtrInt32ToOmitInt32
type DeviceConverter interface {
// goverter:ignore CreatedAt UpdatedAt
// goverter:map Nsfw NSFW
@ -17,6 +22,10 @@ type DeviceConverter interface {
// goverter:ignore state sizeCache unknownFields
// goverter:map NSFW Nsfw
ModelsDeviceToGetDeviceResponse(*models.Device) *device.GetDeviceResponse
// goverter:ignore Slug SingleFolderMode CreatedAt UpdatedAt
// goverter:map Nsfw NSFW
DeviceSetterProtoToModelsDeviceSetter(*device.DeviceSetter) *models.DeviceSetter
}
func BoolToInt8(b bool) int8 {
@ -26,6 +35,33 @@ func BoolToInt8(b bool) int8 {
return 0
}
func PtrBoolToOmitInt8(b *bool) omit.Val[int8] {
if b == nil {
return omit.Val[int8]{}
}
v := *b
if v {
return omit.From(int8(1))
}
return omit.From(int8(0))
}
func PtrStringToOmitString(s *string) omit.Val[string] {
return omit.FromPtr(s)
}
func PtrFloat64ToOmitFloat64(f *float64) omit.Val[float64] {
return omit.FromPtr(f)
}
func PtrInt32ToOmitInt32(i *int32) omit.Val[int32] {
return omit.FromPtr(i)
}
func PtrInt64ToOmitInt64(i *int64) omit.Val[int64] {
return omit.FromPtr(i)
}
func Int8ToBool(i int8) bool {
return i > 0
}

View file

@ -40,7 +40,7 @@ message CreateDeviceRequest {
// accepted by the device even though it is 2 times bigger than the device resolution.
//
// If you want to filter image size, use min_x, min_y, max_x, max_y fields.
double aspect_ratio_tolerance = 6;
double aspect_ratio_tolerance = 6 [(buf.validate.field).double.gte = 0];
// The minimum x resolution required for an image to be accepted by the device.
//

View file

@ -5,6 +5,7 @@ package device.v1;
import "device/v1/create.proto";
import "device/v1/get.proto";
import "device/v1/list.proto";
import "device/v1/update.proto";
option go_package = "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1";
@ -23,4 +24,9 @@ service DeviceService {
// CreateDevice creates a new device.
rpc CreateDevice(CreateDeviceRequest) returns (CreateDeviceResponse) {}
// UpdateDevice updates a device.
//
// Only fields that are set in the request will be updated.
rpc UpdateDevice(UpdateDeviceRequest) returns (UpdateDeviceResponse) {}
}

View file

@ -0,0 +1,29 @@
syntax = "proto3";
package device.v1;
import "buf/validate/validate.proto";
option go_package = "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1";
message UpdateDeviceRequest {
string slug = 1;
DeviceSetter set = 2;
}
message DeviceSetter {
optional bool disabled = 1;
optional string name = 2 [(buf.validate.field).string.min_len = 1];
optional double resolution_x = 3 [(buf.validate.field).double.gt = 0];
optional double resolution_y = 4 [(buf.validate.field).double.gt = 0];
optional double aspect_ratio_tolerance = 5 [(buf.validate.field).double.gte = 0];
optional int32 min_x = 6 [(buf.validate.field).int32.gte = 0];
optional int32 min_y = 7 [(buf.validate.field).int32.gte = 0];
optional int32 max_x = 8 [(buf.validate.field).int32.gte = 0];
optional int32 max_y = 9 [(buf.validate.field).int32.gte = 0];
optional bool nsfw = 10;
}
message UpdateDeviceResponse {
string slug = 1;
}