[hw-8] add: comment service

This commit is contained in:
3ybacTuK
2025-07-26 23:47:18 +03:00
parent 6420eaf3d7
commit 6e0d90a6d5
29 changed files with 1249 additions and 725 deletions

View File

@@ -100,7 +100,34 @@ func (m *Comment) validate(all bool) error {
errors = append(errors, err)
}
// no validation rules for CreatedAt
if all {
switch v := interface{}(m.GetCreatedAt()).(type) {
case interface{ ValidateAll() error }:
if err := v.ValidateAll(); err != nil {
errors = append(errors, CommentValidationError{
field: "CreatedAt",
reason: "embedded message failed validation",
cause: err,
})
}
case interface{ Validate() error }:
if err := v.Validate(); err != nil {
errors = append(errors, CommentValidationError{
field: "CreatedAt",
reason: "embedded message failed validation",
cause: err,
})
}
}
} else if v, ok := interface{}(m.GetCreatedAt()).(interface{ Validate() error }); ok {
if err := v.Validate(); err != nil {
return CommentValidationError{
field: "CreatedAt",
reason: "embedded message failed validation",
cause: err,
}
}
}
if len(errors) > 0 {
return CommentMultiError(errors)
@@ -223,9 +250,9 @@ func (m *CreateCommentRequest) validate(all bool) error {
errors = append(errors, err)
}
if l := utf8.RuneCountInString(m.GetText()); l < 1 || l > 255 {
if l := utf8.RuneCountInString(m.GetComment()); l < 1 || l > 255 {
err := CreateCommentRequestValidationError{
field: "Text",
field: "Comment",
reason: "value length must be between 1 and 255 runes, inclusive",
}
if !all {
@@ -336,34 +363,7 @@ func (m *CreateCommentResponse) validate(all bool) error {
var errors []error
if all {
switch v := interface{}(m.GetComment()).(type) {
case interface{ ValidateAll() error }:
if err := v.ValidateAll(); err != nil {
errors = append(errors, CreateCommentResponseValidationError{
field: "Comment",
reason: "embedded message failed validation",
cause: err,
})
}
case interface{ Validate() error }:
if err := v.Validate(); err != nil {
errors = append(errors, CreateCommentResponseValidationError{
field: "Comment",
reason: "embedded message failed validation",
cause: err,
})
}
}
} else if v, ok := interface{}(m.GetComment()).(interface{ Validate() error }); ok {
if err := v.Validate(); err != nil {
return CreateCommentResponseValidationError{
field: "Comment",
reason: "embedded message failed validation",
cause: err,
}
}
}
// no validation rules for Id
if len(errors) > 0 {
return CreateCommentResponseMultiError(errors)
@@ -689,41 +689,30 @@ var _ interface {
ErrorName() string
} = GetCommentResponseValidationError{}
// Validate checks the field values on UpdateCommentRequest with the rules
// Validate checks the field values on EditCommentRequest with the rules
// defined in the proto definition for this message. If any rules are
// violated, the first error encountered is returned, or nil if there are no violations.
func (m *UpdateCommentRequest) Validate() error {
func (m *EditCommentRequest) Validate() error {
return m.validate(false)
}
// ValidateAll checks the field values on UpdateCommentRequest with the rules
// ValidateAll checks the field values on EditCommentRequest with the rules
// defined in the proto definition for this message. If any rules are
// violated, the result is a list of violation errors wrapped in
// UpdateCommentRequestMultiError, or nil if none found.
func (m *UpdateCommentRequest) ValidateAll() error {
// EditCommentRequestMultiError, or nil if none found.
func (m *EditCommentRequest) ValidateAll() error {
return m.validate(true)
}
func (m *UpdateCommentRequest) validate(all bool) error {
func (m *EditCommentRequest) validate(all bool) error {
if m == nil {
return nil
}
var errors []error
if m.GetId() <= 0 {
err := UpdateCommentRequestValidationError{
field: "Id",
reason: "value must be greater than 0",
}
if !all {
return err
}
errors = append(errors, err)
}
if m.GetUserId() <= 0 {
err := UpdateCommentRequestValidationError{
err := EditCommentRequestValidationError{
field: "UserId",
reason: "value must be greater than 0",
}
@@ -733,9 +722,20 @@ func (m *UpdateCommentRequest) validate(all bool) error {
errors = append(errors, err)
}
if l := utf8.RuneCountInString(m.GetText()); l < 1 || l > 255 {
err := UpdateCommentRequestValidationError{
field: "Text",
if m.GetCommentId() <= 0 {
err := EditCommentRequestValidationError{
field: "CommentId",
reason: "value must be greater than 0",
}
if !all {
return err
}
errors = append(errors, err)
}
if l := utf8.RuneCountInString(m.GetNewComment()); l < 1 || l > 255 {
err := EditCommentRequestValidationError{
field: "NewComment",
reason: "value length must be between 1 and 255 runes, inclusive",
}
if !all {
@@ -745,19 +745,19 @@ func (m *UpdateCommentRequest) validate(all bool) error {
}
if len(errors) > 0 {
return UpdateCommentRequestMultiError(errors)
return EditCommentRequestMultiError(errors)
}
return nil
}
// UpdateCommentRequestMultiError is an error wrapping multiple validation
// errors returned by UpdateCommentRequest.ValidateAll() if the designated
// constraints aren't met.
type UpdateCommentRequestMultiError []error
// EditCommentRequestMultiError is an error wrapping multiple validation errors
// returned by EditCommentRequest.ValidateAll() if the designated constraints
// aren't met.
type EditCommentRequestMultiError []error
// Error returns a concatenation of all the error messages it wraps.
func (m UpdateCommentRequestMultiError) Error() string {
func (m EditCommentRequestMultiError) Error() string {
var msgs []string
for _, err := range m {
msgs = append(msgs, err.Error())
@@ -766,11 +766,11 @@ func (m UpdateCommentRequestMultiError) Error() string {
}
// AllErrors returns a list of validation violation errors.
func (m UpdateCommentRequestMultiError) AllErrors() []error { return m }
func (m EditCommentRequestMultiError) AllErrors() []error { return m }
// UpdateCommentRequestValidationError is the validation error returned by
// UpdateCommentRequest.Validate if the designated constraints aren't met.
type UpdateCommentRequestValidationError struct {
// EditCommentRequestValidationError is the validation error returned by
// EditCommentRequest.Validate if the designated constraints aren't met.
type EditCommentRequestValidationError struct {
field string
reason string
cause error
@@ -778,24 +778,24 @@ type UpdateCommentRequestValidationError struct {
}
// Field function returns field value.
func (e UpdateCommentRequestValidationError) Field() string { return e.field }
func (e EditCommentRequestValidationError) Field() string { return e.field }
// Reason function returns reason value.
func (e UpdateCommentRequestValidationError) Reason() string { return e.reason }
func (e EditCommentRequestValidationError) Reason() string { return e.reason }
// Cause function returns cause value.
func (e UpdateCommentRequestValidationError) Cause() error { return e.cause }
func (e EditCommentRequestValidationError) Cause() error { return e.cause }
// Key function returns key value.
func (e UpdateCommentRequestValidationError) Key() bool { return e.key }
func (e EditCommentRequestValidationError) Key() bool { return e.key }
// ErrorName returns error name.
func (e UpdateCommentRequestValidationError) ErrorName() string {
return "UpdateCommentRequestValidationError"
func (e EditCommentRequestValidationError) ErrorName() string {
return "EditCommentRequestValidationError"
}
// Error satisfies the builtin error interface
func (e UpdateCommentRequestValidationError) Error() string {
func (e EditCommentRequestValidationError) Error() string {
cause := ""
if e.cause != nil {
cause = fmt.Sprintf(" | caused by: %v", e.cause)
@@ -807,14 +807,14 @@ func (e UpdateCommentRequestValidationError) Error() string {
}
return fmt.Sprintf(
"invalid %sUpdateCommentRequest.%s: %s%s",
"invalid %sEditCommentRequest.%s: %s%s",
key,
e.field,
e.reason,
cause)
}
var _ error = UpdateCommentRequestValidationError{}
var _ error = EditCommentRequestValidationError{}
var _ interface {
Field() string
@@ -822,138 +822,7 @@ var _ interface {
Key() bool
Cause() error
ErrorName() string
} = UpdateCommentRequestValidationError{}
// Validate checks the field values on UpdateCommentResponse with the rules
// defined in the proto definition for this message. If any rules are
// violated, the first error encountered is returned, or nil if there are no violations.
func (m *UpdateCommentResponse) Validate() error {
return m.validate(false)
}
// ValidateAll checks the field values on UpdateCommentResponse with the rules
// defined in the proto definition for this message. If any rules are
// violated, the result is a list of violation errors wrapped in
// UpdateCommentResponseMultiError, or nil if none found.
func (m *UpdateCommentResponse) ValidateAll() error {
return m.validate(true)
}
func (m *UpdateCommentResponse) validate(all bool) error {
if m == nil {
return nil
}
var errors []error
if all {
switch v := interface{}(m.GetComment()).(type) {
case interface{ ValidateAll() error }:
if err := v.ValidateAll(); err != nil {
errors = append(errors, UpdateCommentResponseValidationError{
field: "Comment",
reason: "embedded message failed validation",
cause: err,
})
}
case interface{ Validate() error }:
if err := v.Validate(); err != nil {
errors = append(errors, UpdateCommentResponseValidationError{
field: "Comment",
reason: "embedded message failed validation",
cause: err,
})
}
}
} else if v, ok := interface{}(m.GetComment()).(interface{ Validate() error }); ok {
if err := v.Validate(); err != nil {
return UpdateCommentResponseValidationError{
field: "Comment",
reason: "embedded message failed validation",
cause: err,
}
}
}
if len(errors) > 0 {
return UpdateCommentResponseMultiError(errors)
}
return nil
}
// UpdateCommentResponseMultiError is an error wrapping multiple validation
// errors returned by UpdateCommentResponse.ValidateAll() if the designated
// constraints aren't met.
type UpdateCommentResponseMultiError []error
// Error returns a concatenation of all the error messages it wraps.
func (m UpdateCommentResponseMultiError) Error() string {
var msgs []string
for _, err := range m {
msgs = append(msgs, err.Error())
}
return strings.Join(msgs, "; ")
}
// AllErrors returns a list of validation violation errors.
func (m UpdateCommentResponseMultiError) AllErrors() []error { return m }
// UpdateCommentResponseValidationError is the validation error returned by
// UpdateCommentResponse.Validate if the designated constraints aren't met.
type UpdateCommentResponseValidationError struct {
field string
reason string
cause error
key bool
}
// Field function returns field value.
func (e UpdateCommentResponseValidationError) Field() string { return e.field }
// Reason function returns reason value.
func (e UpdateCommentResponseValidationError) Reason() string { return e.reason }
// Cause function returns cause value.
func (e UpdateCommentResponseValidationError) Cause() error { return e.cause }
// Key function returns key value.
func (e UpdateCommentResponseValidationError) Key() bool { return e.key }
// ErrorName returns error name.
func (e UpdateCommentResponseValidationError) ErrorName() string {
return "UpdateCommentResponseValidationError"
}
// Error satisfies the builtin error interface
func (e UpdateCommentResponseValidationError) Error() string {
cause := ""
if e.cause != nil {
cause = fmt.Sprintf(" | caused by: %v", e.cause)
}
key := ""
if e.key {
key = "key for "
}
return fmt.Sprintf(
"invalid %sUpdateCommentResponse.%s: %s%s",
key,
e.field,
e.reason,
cause)
}
var _ error = UpdateCommentResponseValidationError{}
var _ interface {
Field() string
Reason() string
Key() bool
Cause() error
ErrorName() string
} = UpdateCommentResponseValidationError{}
} = EditCommentRequestValidationError{}
// Validate checks the field values on ListBySkuRequest with the rules defined
// in the proto definition for this message. If any rules are violated, the