From 0503a0b797098b718a0e2f1df84b787f028ddf0a Mon Sep 17 00:00:00 2001 From: David Zhao Date: Fri, 5 Jul 2024 19:56:04 -1000 Subject: [PATCH] Fix logic error prevent request.json from functioning (#354) * Fix logic error prevent request.json from functioning * do not choke on partial JSON, since we don't differentiated required fields --- cmd/lk/proto.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/lk/proto.go b/cmd/lk/proto.go index a62a5518..bafc12c0 100644 --- a/cmd/lk/proto.go +++ b/cmd/lk/proto.go @@ -28,6 +28,10 @@ import ( const flagRequest = "request" +var unmarshaller = protojson.UnmarshalOptions{ + AllowPartial: true, +} + type protoType[T any] interface { *T proto.Message @@ -58,7 +62,7 @@ func ReadRequestFileOrLiteral[T any, P protoType[T]](pathOrLiteral string) (*T, var err error // This allows us to read JSON from either CLI arg or FS - if _, err = os.Stat(pathOrLiteral); err != nil { + if _, err = os.Stat(pathOrLiteral); err == nil { reqBytes, err = os.ReadFile(pathOrLiteral) } else { reqBytes = []byte(pathOrLiteral) @@ -68,7 +72,7 @@ func ReadRequestFileOrLiteral[T any, P protoType[T]](pathOrLiteral string) (*T, } var req P = new(T) - err = protojson.Unmarshal(reqBytes, req) + err = unmarshaller.Unmarshal(reqBytes, req) if err != nil { return nil, err } @@ -85,7 +89,7 @@ func RequestFlag[T any, P protoType[T]]() *cli.StringFlag { func RequestDesc[T any, _ protoType[T]]() string { typ := reflect.TypeFor[T]().Name() - return typ + " as JSON file (see cmd/lk/examples)" + return typ + " as JSON file" } func createAndPrint[T any, P protoType[T], R any](