You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by GitBox <gi...@apache.org> on 2022/09/02 23:48:15 UTC

[GitHub] [thrift] fishy commented on a diff in pull request #2469: THRIFT-5423: Support go parameter validation in IDL

fishy commented on code in PR #2469:
URL: https://github.com/apache/thrift/pull/2469#discussion_r962064918


##########
lib/go/thrift/application_exception.go:
##########
@@ -59,9 +61,119 @@ type TApplicationException interface {
 	Write(ctx context.Context, oprot TProtocol) error
 }
 
-type tApplicationException struct {
+type ValidationError struct {
 	message string
-	type_   int32
+	check   string
+}
+
+func (e *ValidationError) Check() string {
+	return e.check
+}
+
+func (e ValidationError) Error() string {
+	return e.message
+}
+
+func (p *ValidationError) Read(ctx context.Context, iprot TProtocol) error {
+	_, err := iprot.ReadStructBegin(ctx)
+	if err != nil {
+		return err
+	}
+
+	message := ""
+	check := ""
+
+	for {
+		_, ttype, id, err := iprot.ReadFieldBegin(ctx)
+		if err != nil {
+			return err
+		}
+		if ttype == STOP {
+			break
+		}
+		switch id {
+		case 1:
+			if ttype == STRING {
+				if message, err = iprot.ReadString(ctx); err != nil {
+					return err
+				}
+			} else {
+				if err = SkipDefaultDepth(ctx, iprot, ttype); err != nil {
+					return err
+				}
+			}
+		case 2:
+			if ttype == STRING {
+				if check, err = iprot.ReadString(ctx); err != nil {
+					return err
+				}
+			} else {
+				if err = SkipDefaultDepth(ctx, iprot, ttype); err != nil {
+					return err
+				}
+			}
+		default:
+			if err = SkipDefaultDepth(ctx, iprot, ttype); err != nil {
+				return err
+			}
+		}
+		if err = iprot.ReadFieldEnd(ctx); err != nil {
+			return err
+		}
+	}
+	if err := iprot.ReadStructEnd(ctx); err != nil {
+		return err
+	}
+
+	p.message = message
+	p.check = check
+
+	return nil
+}
+
+func (p *ValidationError) Write(ctx context.Context, oprot TProtocol) (err error) {
+	err = oprot.WriteStructBegin(ctx, "ValidationError")
+	if err != nil {
+		return
+	}
+	if len(p.Error()) > 0 {
+		err = oprot.WriteFieldBegin(ctx, "message", STRING, 1)
+		if err != nil {
+			return
+		}
+		err = oprot.WriteString(ctx, p.message)
+		if err != nil {
+			return
+		}
+		err = oprot.WriteFieldEnd(ctx)
+		if err != nil {
+			return
+		}
+		err = oprot.WriteFieldBegin(ctx, "check", STRING, 2)
+		if err != nil {
+			return
+		}
+		err = oprot.WriteString(ctx, p.check)
+		if err != nil {
+			return
+		}
+		err = oprot.WriteFieldEnd(ctx)
+		if err != nil {
+			return
+		}
+	}
+	err = oprot.WriteFieldStop(ctx)
+	if err != nil {
+		return
+	}
+	err = oprot.WriteStructEnd(ctx)
+	return
+}
+
+type tApplicationException struct {
+	message         string
+	type_           int32
+	validationError *ValidationError

Review Comment:
   this should not be specified to `validationError *ValidationError`, but just a generic `err error`. see `TTransportException` for an example: https://github.com/apache/thrift/blob/e0ee2c7514b812b5a18bfe9aab9594c5879dc34f/lib/go/thrift/transport_exception.go#L48



##########
lib/go/thrift/application_exception.go:
##########
@@ -124,6 +252,17 @@ func (p *tApplicationException) Read(ctx context.Context, iprot TProtocol) error
 					return err
 				}
 			}
+		case 3:

Review Comment:
   It's likely a bad idea to write/read the additional `ValidationError` details over the wire (the implementation of `Read` and `Write` of `ValidationError`). the details should be purely local additional information for now. to be able to actually communicate that over the wire, we need to expand the spec to define the wire protocol for it.



##########
lib/go/test/tests/validate_test.go:
##########
@@ -0,0 +1,304 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package tests
+
+import (
+	"encoding/json"
+	"errors"
+	"strconv"
+	"strings"
+	"testing"
+
+	"github.com/apache/thrift/lib/go/test/gopath/src/validatetest"
+	thrift "github.com/apache/thrift/lib/go/thrift"
+)
+
+func TestBasicValidator(t *testing.T) {
+	bt := validatetest.NewBasicTest()
+	if err := bt.Validate(); err != nil {
+		t.Error(err)
+	}
+	bt = validatetest.NewBasicTest()
+	bt.Bool1 = thrift.BoolPtr(false)
+	if err := bt.Validate(); err == nil {
+		t.Error("Expected vt.const error for Bool1")
+	} else if !strings.Contains(err.Error(), "vt.const") {

Review Comment:
   the whole point of adding `ValidationError` is to use something better than `strings.Contains` for unit tests. what we want here is something like this:
   
   ```go
   } else {
     var ve *thrift.ValidationError
     if errors.As(err, &ve) {
       if ve.Check() != "vt.const" {
         t.Errorf(...)
       }
       if ve.Field() != "bool1" {
         t.Errorf(...)
       }
       ....
     } else {
       t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
     }
   }
   ```



##########
lib/go/thrift/application_exception.go:
##########
@@ -77,8 +189,23 @@ func (e tApplicationException) Error() string {
 	return defaultApplicationExceptionMessage[e.type_]
 }
 
+func (e tApplicationException) Unwrap() error {
+	if e.validationError != nil {
+		return e.validationError
+	}
+	return e

Review Comment:
   no you can't make `Unwrap` to return self. that will cause infinite loops. this function should simple be `return e.err`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org