Skip to content

Commit

Permalink
Fix logic error prevent request.json from functioning (#354)
Browse files Browse the repository at this point in the history
* Fix logic error prevent request.json from functioning

* do not choke on partial JSON, since we don't differentiated required fields
  • Loading branch information
davidzhao authored Jul 6, 2024
1 parent 1304ef8 commit 0503a0b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cmd/lk/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (

const flagRequest = "request"

var unmarshaller = protojson.UnmarshalOptions{
AllowPartial: true,
}

type protoType[T any] interface {
*T
proto.Message
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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](
Expand Down

0 comments on commit 0503a0b

Please sign in to comment.