addValidation adds a validation function (if any) for the given user type and recurses through the user type adding other validation functions (if any). req if true indicates that the validation is generated for validating request (server-side) messages.
(att *expr.AttributeExpr, attName string, sd *ServiceData, req bool)
| 785 | // req if true indicates that the validation is generated for validating |
| 786 | // request (server-side) messages. |
| 787 | func addValidation(att *expr.AttributeExpr, attName string, sd *ServiceData, req bool) *ValidationData { |
| 788 | ut, ok := att.Type.(expr.UserType) |
| 789 | if !ok { |
| 790 | return nil |
| 791 | } |
| 792 | vtx := protoBufTypeContext(sd.PkgName, sd.Scope, false) |
| 793 | // Validation helper names must be derived from the same protobuf-aware |
| 794 | // scope used by the validation templates so that function declarations |
| 795 | // and call sites (e.g. Message_) stay in sync regardless of traversal |
| 796 | // order or reserved-name handling. |
| 797 | name := vtx.Scope.Name(att, "", vtx.Pointer, vtx.UseDefault) |
| 798 | ref := protoBufGoFullTypeRef(att, sd.PkgName, sd.Scope) |
| 799 | kind := validateClient |
| 800 | if req { |
| 801 | kind = validateServer |
| 802 | } |
| 803 | att = userTypeAttribute(ut) |
| 804 | for _, n := range sd.validations { |
| 805 | if n.SrcName == name { |
| 806 | if n.Kind != kind { |
| 807 | n.Kind = validateBoth |
| 808 | collectValidations(att, attName, req, sd) |
| 809 | } |
| 810 | return n |
| 811 | } |
| 812 | } |
| 813 | removeMeta(att) |
| 814 | if def := codegen.ValidationCode(att, ut, vtx, true, expr.IsAlias(att.Type), false, attName); def != "" { |
| 815 | v := &ValidationData{ |
| 816 | // Validation function names must match the identifiers used by |
| 817 | // validation templates. The template uses the scoped type name |
| 818 | // directly (no Goify) to preserve proto-reserved names like Message_. |
| 819 | Name: "Validate" + name, |
| 820 | Def: def, |
| 821 | ArgName: attName, |
| 822 | SrcName: name, |
| 823 | SrcRef: ref, |
| 824 | Kind: kind, |
| 825 | } |
| 826 | sd.validations = append(sd.validations, v) |
| 827 | collectValidations(att, attName, req, sd) |
| 828 | return v |
| 829 | } |
| 830 | return nil |
| 831 | } |
| 832 | |
| 833 | // collectValidations recurses through the attribute and collects the |
| 834 | // validation functions. |
no test coverage detected