fishy commented on code in PR #2469: URL: https://github.com/apache/thrift/pull/2469#discussion_r963038624
########## compiler/cpp/src/thrift/generate/go_validator_generator.h: ########## @@ -0,0 +1,72 @@ +/* + * 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. + */ + +#ifndef T_GO_VALIDATOR_GENERATOR_H +#define T_GO_VALIDATOR_GENERATOR_H + +#include "thrift/generate/t_generator.h" +#include "thrift/generate/t_go_generator.h" +#include "thrift/generate/validator_parser.h" +#include <fstream> +#include <iostream> +#include <limits> +#include <string> +#include <vector> + +class go_validator_generator { +public: + go_validator_generator(t_go_generator* gg) : go_generator(gg){}; + void generate_struct_validator(std::ostream& out, t_struct* tstruct); + + struct generator_context { + std::string field_symbol; Review Comment: we probably should store the original field name instead of field symbol. for example, for thrift struct of: ```thrift struct foo { 1: bool bool0 = true (vt.const = "true") } ``` the field name itself would be `"bool0"` but the go symbol would be `"Bool0"`. ########## lib/go/thrift/application_exception.go: ########## @@ -59,9 +62,35 @@ type TApplicationException interface { Write(ctx context.Context, oprot TProtocol) error } +type ValidationError struct { + message string + check string + fieldSymbol string +} + +func (e *ValidationError) Check() string { + return e.check +} + +func (e *ValidationError) Field() string { + if fs := strings.Split(e.fieldSymbol, "."); len(fs) > 1 { Review Comment: why is this `Split` needed? in which case would we use `.` to construct field names/symbols? ########## lib/go/thrift/application_exception.go: ########## @@ -77,8 +106,20 @@ func (e tApplicationException) Error() string { return defaultApplicationExceptionMessage[e.type_] } +func (e tApplicationException) Unwrap() error { + return e.err +} + func NewTApplicationException(type_ int32, message string) TApplicationException { - return &tApplicationException{message, type_} + return &tApplicationException{message, type_, nil} +} + +func NewValidationException(type_ int32, check string, field string, message string) TApplicationException { + return &tApplicationException{ + type_: type_, + message: message, + err: &ValidationError{message, check, field}, Review Comment: nit: avoid using unkeyed struct constructors. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
