From 673152bc0d2e38571e9d43b18d2314718fb15fa4 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 13 Dec 2024 22:43:32 -0800 Subject: [PATCH] openapi: list filter belongs in request body --- openapi.yaml | 48 +- pkg/api/oas_client_gen.go | 92 +--- pkg/api/oas_handlers_gen.go | 72 ++- pkg/api/oas_json_gen.go | 662 ++++++++++++++++++++++++++++ pkg/api/oas_parameters_gen.go | 222 ---------- pkg/api/oas_request_decoders_gen.go | 142 ++++++ pkg/api/oas_request_encoders_gen.go | 28 ++ pkg/api/oas_schemas_gen.go | 50 +++ pkg/api/oas_server_gen.go | 4 +- pkg/api/oas_unimplemented_gen.go | 4 +- pkg/api/oas_uri_gen.go | 464 ------------------- pkg/api/oas_validators_gen.go | 82 ++++ pkg/service/script_policy.go | 14 +- pkg/service/submissions.go | 2 +- 14 files changed, 1048 insertions(+), 838 deletions(-) delete mode 100644 pkg/api/oas_uri_gen.go diff --git a/openapi.yaml b/openapi.yaml index e7a8b43..c31dd1c 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -19,17 +19,19 @@ paths: operationId: listSubmissions tags: - Submissions - parameters: - - name: page - in: query - required: true - schema: - $ref: "#/components/schemas/Pagination" - - name: filter - in: query - required: false - schema: - $ref: "#/components/schemas/SubmissionFilter" + requestBody: + required: true + content: + application/json: + schema: + required: + - Page + type: object + properties: + Page: + $ref: "#/components/schemas/Pagination" + Filter: + $ref: "#/components/schemas/SubmissionFilter" security: - cookieAuth: [] responses: @@ -300,17 +302,19 @@ paths: operationId: listScriptPolicy tags: - ScriptPolicy - parameters: - - name: page - in: query - required: true - schema: - $ref: "#/components/schemas/Pagination" - - name: filter - in: query - required: false - schema: - $ref: "#/components/schemas/ScriptPolicyFilter" + requestBody: + required: true + content: + application/json: + schema: + required: + - Page + type: object + properties: + Page: + $ref: "#/components/schemas/Pagination" + Filter: + $ref: "#/components/schemas/ScriptPolicyFilter" security: - cookieAuth: [] responses: diff --git a/pkg/api/oas_client_gen.go b/pkg/api/oas_client_gen.go index 12f56b1..7c34512 100644 --- a/pkg/api/oas_client_gen.go +++ b/pkg/api/oas_client_gen.go @@ -131,13 +131,13 @@ type Invoker interface { // Get list of script policies. // // GET /script-policy - ListScriptPolicy(ctx context.Context, params ListScriptPolicyParams) ([]ScriptPolicy, error) + ListScriptPolicy(ctx context.Context, request *ListScriptPolicyReq) ([]ScriptPolicy, error) // ListSubmissions invokes listSubmissions operation. // // Get list of submissions. // // GET /submissions - ListSubmissions(ctx context.Context, params ListSubmissionsParams) ([]Submission, error) + ListSubmissions(ctx context.Context, request *ListSubmissionsReq) ([]Submission, error) // SetSubmissionCompleted invokes setSubmissionCompleted operation. // // Retrieve map with ID. @@ -2211,12 +2211,12 @@ func (c *Client) sendGetSubmission(ctx context.Context, params GetSubmissionPara // Get list of script policies. // // GET /script-policy -func (c *Client) ListScriptPolicy(ctx context.Context, params ListScriptPolicyParams) ([]ScriptPolicy, error) { - res, err := c.sendListScriptPolicy(ctx, params) +func (c *Client) ListScriptPolicy(ctx context.Context, request *ListScriptPolicyReq) ([]ScriptPolicy, error) { + res, err := c.sendListScriptPolicy(ctx, request) return res, err } -func (c *Client) sendListScriptPolicy(ctx context.Context, params ListScriptPolicyParams) (res []ScriptPolicy, err error) { +func (c *Client) sendListScriptPolicy(ctx context.Context, request *ListScriptPolicyReq) (res []ScriptPolicy, err error) { otelAttrs := []attribute.KeyValue{ otelogen.OperationID("listScriptPolicy"), semconv.HTTPRequestMethodKey.String("GET"), @@ -2256,46 +2256,14 @@ func (c *Client) sendListScriptPolicy(ctx context.Context, params ListScriptPoli pathParts[0] = "/script-policy" uri.AddPathParts(u, pathParts[:]...) - stage = "EncodeQueryParams" - q := uri.NewQueryEncoder() - { - // Encode "page" parameter. - cfg := uri.QueryParameterEncodingConfig{ - Name: "page", - Style: uri.QueryStyleForm, - Explode: true, - } - - if err := q.EncodeParam(cfg, func(e uri.Encoder) error { - return params.Page.EncodeURI(e) - }); err != nil { - return res, errors.Wrap(err, "encode query") - } - } - { - // Encode "filter" parameter. - cfg := uri.QueryParameterEncodingConfig{ - Name: "filter", - Style: uri.QueryStyleForm, - Explode: true, - } - - if err := q.EncodeParam(cfg, func(e uri.Encoder) error { - if val, ok := params.Filter.Get(); ok { - return val.EncodeURI(e) - } - return nil - }); err != nil { - return res, errors.Wrap(err, "encode query") - } - } - u.RawQuery = q.Values().Encode() - stage = "EncodeRequest" r, err := ht.NewRequest(ctx, "GET", u) if err != nil { return res, errors.Wrap(err, "create request") } + if err := encodeListScriptPolicyRequest(request, r); err != nil { + return res, errors.Wrap(err, "encode request") + } { type bitset = [1]uint8 @@ -2351,12 +2319,12 @@ func (c *Client) sendListScriptPolicy(ctx context.Context, params ListScriptPoli // Get list of submissions. // // GET /submissions -func (c *Client) ListSubmissions(ctx context.Context, params ListSubmissionsParams) ([]Submission, error) { - res, err := c.sendListSubmissions(ctx, params) +func (c *Client) ListSubmissions(ctx context.Context, request *ListSubmissionsReq) ([]Submission, error) { + res, err := c.sendListSubmissions(ctx, request) return res, err } -func (c *Client) sendListSubmissions(ctx context.Context, params ListSubmissionsParams) (res []Submission, err error) { +func (c *Client) sendListSubmissions(ctx context.Context, request *ListSubmissionsReq) (res []Submission, err error) { otelAttrs := []attribute.KeyValue{ otelogen.OperationID("listSubmissions"), semconv.HTTPRequestMethodKey.String("GET"), @@ -2396,46 +2364,14 @@ func (c *Client) sendListSubmissions(ctx context.Context, params ListSubmissions pathParts[0] = "/submissions" uri.AddPathParts(u, pathParts[:]...) - stage = "EncodeQueryParams" - q := uri.NewQueryEncoder() - { - // Encode "page" parameter. - cfg := uri.QueryParameterEncodingConfig{ - Name: "page", - Style: uri.QueryStyleForm, - Explode: true, - } - - if err := q.EncodeParam(cfg, func(e uri.Encoder) error { - return params.Page.EncodeURI(e) - }); err != nil { - return res, errors.Wrap(err, "encode query") - } - } - { - // Encode "filter" parameter. - cfg := uri.QueryParameterEncodingConfig{ - Name: "filter", - Style: uri.QueryStyleForm, - Explode: true, - } - - if err := q.EncodeParam(cfg, func(e uri.Encoder) error { - if val, ok := params.Filter.Get(); ok { - return val.EncodeURI(e) - } - return nil - }); err != nil { - return res, errors.Wrap(err, "encode query") - } - } - u.RawQuery = q.Values().Encode() - stage = "EncodeRequest" r, err := ht.NewRequest(ctx, "GET", u) if err != nil { return res, errors.Wrap(err, "create request") } + if err := encodeListSubmissionsRequest(request, r); err != nil { + return res, errors.Wrap(err, "encode request") + } { type bitset = [1]uint8 diff --git a/pkg/api/oas_handlers_gen.go b/pkg/api/oas_handlers_gen.go index 4ca3aa3..f1e1ece 100644 --- a/pkg/api/oas_handlers_gen.go +++ b/pkg/api/oas_handlers_gen.go @@ -3373,16 +3373,21 @@ func (s *Server) handleListScriptPolicyRequest(args [0]string, argsEscaped bool, return } } - params, err := decodeListScriptPolicyParams(args, argsEscaped, r) + request, close, err := s.decodeListScriptPolicyRequest(r) if err != nil { - err = &ogenerrors.DecodeParamsError{ + err = &ogenerrors.DecodeRequestError{ OperationContext: opErrContext, Err: err, } - defer recordError("DecodeParams", err) + defer recordError("DecodeRequest", err) s.cfg.ErrorHandler(ctx, w, r, err) return } + defer func() { + if err := close(); err != nil { + recordError("CloseRequest", err) + } + }() var response []ScriptPolicy if m := s.cfg.Middleware; m != nil { @@ -3391,23 +3396,14 @@ func (s *Server) handleListScriptPolicyRequest(args [0]string, argsEscaped bool, OperationName: ListScriptPolicyOperation, OperationSummary: "Get list of script policies", OperationID: "listScriptPolicy", - Body: nil, - Params: middleware.Parameters{ - { - Name: "page", - In: "query", - }: params.Page, - { - Name: "filter", - In: "query", - }: params.Filter, - }, - Raw: r, + Body: request, + Params: middleware.Parameters{}, + Raw: r, } type ( - Request = struct{} - Params = ListScriptPolicyParams + Request = *ListScriptPolicyReq + Params = struct{} Response = []ScriptPolicy ) response, err = middleware.HookMiddleware[ @@ -3417,14 +3413,14 @@ func (s *Server) handleListScriptPolicyRequest(args [0]string, argsEscaped bool, ]( m, mreq, - unpackListScriptPolicyParams, + nil, func(ctx context.Context, request Request, params Params) (response Response, err error) { - response, err = s.h.ListScriptPolicy(ctx, params) + response, err = s.h.ListScriptPolicy(ctx, request) return response, err }, ) } else { - response, err = s.h.ListScriptPolicy(ctx, params) + response, err = s.h.ListScriptPolicy(ctx, request) } if err != nil { if errRes, ok := errors.Into[*ErrorStatusCode](err); ok { @@ -3572,16 +3568,21 @@ func (s *Server) handleListSubmissionsRequest(args [0]string, argsEscaped bool, return } } - params, err := decodeListSubmissionsParams(args, argsEscaped, r) + request, close, err := s.decodeListSubmissionsRequest(r) if err != nil { - err = &ogenerrors.DecodeParamsError{ + err = &ogenerrors.DecodeRequestError{ OperationContext: opErrContext, Err: err, } - defer recordError("DecodeParams", err) + defer recordError("DecodeRequest", err) s.cfg.ErrorHandler(ctx, w, r, err) return } + defer func() { + if err := close(); err != nil { + recordError("CloseRequest", err) + } + }() var response []Submission if m := s.cfg.Middleware; m != nil { @@ -3590,23 +3591,14 @@ func (s *Server) handleListSubmissionsRequest(args [0]string, argsEscaped bool, OperationName: ListSubmissionsOperation, OperationSummary: "Get list of submissions", OperationID: "listSubmissions", - Body: nil, - Params: middleware.Parameters{ - { - Name: "page", - In: "query", - }: params.Page, - { - Name: "filter", - In: "query", - }: params.Filter, - }, - Raw: r, + Body: request, + Params: middleware.Parameters{}, + Raw: r, } type ( - Request = struct{} - Params = ListSubmissionsParams + Request = *ListSubmissionsReq + Params = struct{} Response = []Submission ) response, err = middleware.HookMiddleware[ @@ -3616,14 +3608,14 @@ func (s *Server) handleListSubmissionsRequest(args [0]string, argsEscaped bool, ]( m, mreq, - unpackListSubmissionsParams, + nil, func(ctx context.Context, request Request, params Params) (response Response, err error) { - response, err = s.h.ListSubmissions(ctx, params) + response, err = s.h.ListSubmissions(ctx, request) return response, err }, ) } else { - response, err = s.h.ListSubmissions(ctx, params) + response, err = s.h.ListSubmissions(ctx, request) } if err != nil { if errRes, ok := errors.Into[*ErrorStatusCode](err); ok { diff --git a/pkg/api/oas_json_gen.go b/pkg/api/oas_json_gen.go index 3e51c3a..1ddcba3 100644 --- a/pkg/api/oas_json_gen.go +++ b/pkg/api/oas_json_gen.go @@ -221,6 +221,228 @@ func (s *ID) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode implements json.Marshaler. +func (s *ListScriptPolicyReq) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *ListScriptPolicyReq) encodeFields(e *jx.Encoder) { + { + e.FieldStart("Page") + s.Page.Encode(e) + } + { + if s.Filter.Set { + e.FieldStart("Filter") + s.Filter.Encode(e) + } + } +} + +var jsonFieldsNameOfListScriptPolicyReq = [2]string{ + 0: "Page", + 1: "Filter", +} + +// Decode decodes ListScriptPolicyReq from json. +func (s *ListScriptPolicyReq) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode ListScriptPolicyReq to nil") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "Page": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + if err := s.Page.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Page\"") + } + case "Filter": + if err := func() error { + s.Filter.Reset() + if err := s.Filter.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Filter\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode ListScriptPolicyReq") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfListScriptPolicyReq) { + name = jsonFieldsNameOfListScriptPolicyReq[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *ListScriptPolicyReq) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ListScriptPolicyReq) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *ListSubmissionsReq) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *ListSubmissionsReq) encodeFields(e *jx.Encoder) { + { + e.FieldStart("Page") + s.Page.Encode(e) + } + { + if s.Filter.Set { + e.FieldStart("Filter") + s.Filter.Encode(e) + } + } +} + +var jsonFieldsNameOfListSubmissionsReq = [2]string{ + 0: "Page", + 1: "Filter", +} + +// Decode decodes ListSubmissionsReq from json. +func (s *ListSubmissionsReq) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode ListSubmissionsReq to nil") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "Page": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + if err := s.Page.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Page\"") + } + case "Filter": + if err := func() error { + s.Filter.Reset() + if err := s.Filter.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Filter\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode ListSubmissionsReq") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfListSubmissionsReq) { + name = jsonFieldsNameOfListSubmissionsReq[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *ListSubmissionsReq) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ListSubmissionsReq) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes int32 as json. func (o OptInt32) Encode(e *jx.Encoder) { if !o.Set { @@ -291,6 +513,39 @@ func (s *OptInt64) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes ScriptPolicyFilter as json. +func (o OptScriptPolicyFilter) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes ScriptPolicyFilter from json. +func (o *OptScriptPolicyFilter) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptScriptPolicyFilter to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptScriptPolicyFilter) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptScriptPolicyFilter) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode encodes string as json. func (o OptString) Encode(e *jx.Encoder) { if !o.Set { @@ -326,6 +581,152 @@ func (s *OptString) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode encodes SubmissionFilter as json. +func (o OptSubmissionFilter) Encode(e *jx.Encoder) { + if !o.Set { + return + } + o.Value.Encode(e) +} + +// Decode decodes SubmissionFilter from json. +func (o *OptSubmissionFilter) Decode(d *jx.Decoder) error { + if o == nil { + return errors.New("invalid: unable to decode OptSubmissionFilter to nil") + } + o.Set = true + if err := o.Value.Decode(d); err != nil { + return err + } + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s OptSubmissionFilter) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *OptSubmissionFilter) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + +// Encode implements json.Marshaler. +func (s *Pagination) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *Pagination) encodeFields(e *jx.Encoder) { + { + e.FieldStart("Page") + e.Int32(s.Page) + } + { + e.FieldStart("Limit") + e.Int32(s.Limit) + } +} + +var jsonFieldsNameOfPagination = [2]string{ + 0: "Page", + 1: "Limit", +} + +// Decode decodes Pagination from json. +func (s *Pagination) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode Pagination to nil") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "Page": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Int32() + s.Page = int32(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Page\"") + } + case "Limit": + requiredBitSet[0] |= 1 << 1 + if err := func() error { + v, err := d.Int32() + s.Limit = int32(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Limit\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode Pagination") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000011, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfPagination) { + name = jsonFieldsNameOfPagination[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *Pagination) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *Pagination) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode implements json.Marshaler. func (s *Script) Encode(e *jx.Encoder) { e.ObjStart() @@ -863,6 +1264,120 @@ func (s *ScriptPolicyCreate) UnmarshalJSON(data []byte) error { return s.Decode(d) } +// Encode implements json.Marshaler. +func (s *ScriptPolicyFilter) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *ScriptPolicyFilter) encodeFields(e *jx.Encoder) { + { + if s.ID.Set { + e.FieldStart("ID") + s.ID.Encode(e) + } + } + { + if s.FromScriptHash.Set { + e.FieldStart("FromScriptHash") + s.FromScriptHash.Encode(e) + } + } + { + if s.ToScriptID.Set { + e.FieldStart("ToScriptID") + s.ToScriptID.Encode(e) + } + } + { + if s.Policy.Set { + e.FieldStart("Policy") + s.Policy.Encode(e) + } + } +} + +var jsonFieldsNameOfScriptPolicyFilter = [4]string{ + 0: "ID", + 1: "FromScriptHash", + 2: "ToScriptID", + 3: "Policy", +} + +// Decode decodes ScriptPolicyFilter from json. +func (s *ScriptPolicyFilter) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode ScriptPolicyFilter to nil") + } + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "ID": + if err := func() error { + s.ID.Reset() + if err := s.ID.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"ID\"") + } + case "FromScriptHash": + if err := func() error { + s.FromScriptHash.Reset() + if err := s.FromScriptHash.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"FromScriptHash\"") + } + case "ToScriptID": + if err := func() error { + s.ToScriptID.Reset() + if err := s.ToScriptID.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"ToScriptID\"") + } + case "Policy": + if err := func() error { + s.Policy.Reset() + if err := s.Policy.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Policy\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode ScriptPolicyFilter") + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *ScriptPolicyFilter) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *ScriptPolicyFilter) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} + // Encode implements json.Marshaler. func (s *ScriptPolicyUpdate) Encode(e *jx.Encoder) { e.ObjStart() @@ -1621,3 +2136,150 @@ func (s *SubmissionCreate) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } + +// Encode implements json.Marshaler. +func (s *SubmissionFilter) Encode(e *jx.Encoder) { + e.ObjStart() + s.encodeFields(e) + e.ObjEnd() +} + +// encodeFields encodes fields. +func (s *SubmissionFilter) encodeFields(e *jx.Encoder) { + { + e.FieldStart("ID") + e.Int64(s.ID) + } + { + if s.DisplayName.Set { + e.FieldStart("DisplayName") + s.DisplayName.Encode(e) + } + } + { + if s.Creator.Set { + e.FieldStart("Creator") + s.Creator.Encode(e) + } + } + { + if s.GameID.Set { + e.FieldStart("GameID") + s.GameID.Encode(e) + } + } +} + +var jsonFieldsNameOfSubmissionFilter = [4]string{ + 0: "ID", + 1: "DisplayName", + 2: "Creator", + 3: "GameID", +} + +// Decode decodes SubmissionFilter from json. +func (s *SubmissionFilter) Decode(d *jx.Decoder) error { + if s == nil { + return errors.New("invalid: unable to decode SubmissionFilter to nil") + } + var requiredBitSet [1]uint8 + + if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { + switch string(k) { + case "ID": + requiredBitSet[0] |= 1 << 0 + if err := func() error { + v, err := d.Int64() + s.ID = int64(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"ID\"") + } + case "DisplayName": + if err := func() error { + s.DisplayName.Reset() + if err := s.DisplayName.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"DisplayName\"") + } + case "Creator": + if err := func() error { + s.Creator.Reset() + if err := s.Creator.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"Creator\"") + } + case "GameID": + if err := func() error { + s.GameID.Reset() + if err := s.GameID.Decode(d); err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"GameID\"") + } + default: + return d.Skip() + } + return nil + }); err != nil { + return errors.Wrap(err, "decode SubmissionFilter") + } + // Validate required fields. + var failures []validate.FieldError + for i, mask := range [1]uint8{ + 0b00000001, + } { + if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { + // Mask only required fields and check equality to mask using XOR. + // + // If XOR result is not zero, result is not equal to expected, so some fields are missed. + // Bits of fields which would be set are actually bits of missed fields. + missed := bits.OnesCount8(result) + for bitN := 0; bitN < missed; bitN++ { + bitIdx := bits.TrailingZeros8(result) + fieldIdx := i*8 + bitIdx + var name string + if fieldIdx < len(jsonFieldsNameOfSubmissionFilter) { + name = jsonFieldsNameOfSubmissionFilter[fieldIdx] + } else { + name = strconv.Itoa(fieldIdx) + } + failures = append(failures, validate.FieldError{ + Name: name, + Error: validate.ErrFieldRequired, + }) + // Reset bit. + result &^= 1 << bitIdx + } + } + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + + return nil +} + +// MarshalJSON implements stdjson.Marshaler. +func (s *SubmissionFilter) MarshalJSON() ([]byte, error) { + e := jx.Encoder{} + s.Encode(&e) + return e.Bytes(), nil +} + +// UnmarshalJSON implements stdjson.Unmarshaler. +func (s *SubmissionFilter) UnmarshalJSON(data []byte) error { + d := jx.DecodeBytes(data) + return s.Decode(d) +} diff --git a/pkg/api/oas_parameters_gen.go b/pkg/api/oas_parameters_gen.go index 8928941..a250aaa 100644 --- a/pkg/api/oas_parameters_gen.go +++ b/pkg/api/oas_parameters_gen.go @@ -954,228 +954,6 @@ func decodeGetSubmissionParams(args [1]string, argsEscaped bool, r *http.Request return params, nil } -// ListScriptPolicyParams is parameters of listScriptPolicy operation. -type ListScriptPolicyParams struct { - Page Pagination - Filter OptScriptPolicyFilter -} - -func unpackListScriptPolicyParams(packed middleware.Parameters) (params ListScriptPolicyParams) { - { - key := middleware.ParameterKey{ - Name: "page", - In: "query", - } - params.Page = packed[key].(Pagination) - } - { - key := middleware.ParameterKey{ - Name: "filter", - In: "query", - } - if v, ok := packed[key]; ok { - params.Filter = v.(OptScriptPolicyFilter) - } - } - return params -} - -func decodeListScriptPolicyParams(args [0]string, argsEscaped bool, r *http.Request) (params ListScriptPolicyParams, _ error) { - q := uri.NewQueryDecoder(r.URL.Query()) - // Decode query: page. - if err := func() error { - cfg := uri.QueryParameterDecodingConfig{ - Name: "page", - Style: uri.QueryStyleForm, - Explode: true, - Fields: []uri.QueryParameterObjectField{{Name: "Page", Required: true}, {Name: "Limit", Required: true}}, - } - - if err := q.HasParam(cfg); err == nil { - if err := q.DecodeParam(cfg, func(d uri.Decoder) error { - return params.Page.DecodeURI(d) - }); err != nil { - return err - } - if err := func() error { - if err := params.Page.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - return err - } - } else { - return validate.ErrFieldRequired - } - return nil - }(); err != nil { - return params, &ogenerrors.DecodeParamError{ - Name: "page", - In: "query", - Err: err, - } - } - // Decode query: filter. - if err := func() error { - cfg := uri.QueryParameterDecodingConfig{ - Name: "filter", - Style: uri.QueryStyleForm, - Explode: true, - Fields: []uri.QueryParameterObjectField{{Name: "ID", Required: false}, {Name: "FromScriptHash", Required: false}, {Name: "ToScriptID", Required: false}, {Name: "Policy", Required: false}}, - } - - if err := q.HasParam(cfg); err == nil { - if err := q.DecodeParam(cfg, func(d uri.Decoder) error { - var paramsDotFilterVal ScriptPolicyFilter - if err := func() error { - return paramsDotFilterVal.DecodeURI(d) - }(); err != nil { - return err - } - params.Filter.SetTo(paramsDotFilterVal) - return nil - }); err != nil { - return err - } - if err := func() error { - if value, ok := params.Filter.Get(); ok { - if err := func() error { - if err := value.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - return err - } - } - return nil - }(); err != nil { - return err - } - } - return nil - }(); err != nil { - return params, &ogenerrors.DecodeParamError{ - Name: "filter", - In: "query", - Err: err, - } - } - return params, nil -} - -// ListSubmissionsParams is parameters of listSubmissions operation. -type ListSubmissionsParams struct { - Page Pagination - Filter OptSubmissionFilter -} - -func unpackListSubmissionsParams(packed middleware.Parameters) (params ListSubmissionsParams) { - { - key := middleware.ParameterKey{ - Name: "page", - In: "query", - } - params.Page = packed[key].(Pagination) - } - { - key := middleware.ParameterKey{ - Name: "filter", - In: "query", - } - if v, ok := packed[key]; ok { - params.Filter = v.(OptSubmissionFilter) - } - } - return params -} - -func decodeListSubmissionsParams(args [0]string, argsEscaped bool, r *http.Request) (params ListSubmissionsParams, _ error) { - q := uri.NewQueryDecoder(r.URL.Query()) - // Decode query: page. - if err := func() error { - cfg := uri.QueryParameterDecodingConfig{ - Name: "page", - Style: uri.QueryStyleForm, - Explode: true, - Fields: []uri.QueryParameterObjectField{{Name: "Page", Required: true}, {Name: "Limit", Required: true}}, - } - - if err := q.HasParam(cfg); err == nil { - if err := q.DecodeParam(cfg, func(d uri.Decoder) error { - return params.Page.DecodeURI(d) - }); err != nil { - return err - } - if err := func() error { - if err := params.Page.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - return err - } - } else { - return validate.ErrFieldRequired - } - return nil - }(); err != nil { - return params, &ogenerrors.DecodeParamError{ - Name: "page", - In: "query", - Err: err, - } - } - // Decode query: filter. - if err := func() error { - cfg := uri.QueryParameterDecodingConfig{ - Name: "filter", - Style: uri.QueryStyleForm, - Explode: true, - Fields: []uri.QueryParameterObjectField{{Name: "ID", Required: true}, {Name: "DisplayName", Required: false}, {Name: "Creator", Required: false}, {Name: "GameID", Required: false}}, - } - - if err := q.HasParam(cfg); err == nil { - if err := q.DecodeParam(cfg, func(d uri.Decoder) error { - var paramsDotFilterVal SubmissionFilter - if err := func() error { - return paramsDotFilterVal.DecodeURI(d) - }(); err != nil { - return err - } - params.Filter.SetTo(paramsDotFilterVal) - return nil - }); err != nil { - return err - } - if err := func() error { - if value, ok := params.Filter.Get(); ok { - if err := func() error { - if err := value.Validate(); err != nil { - return err - } - return nil - }(); err != nil { - return err - } - } - return nil - }(); err != nil { - return err - } - } - return nil - }(); err != nil { - return params, &ogenerrors.DecodeParamError{ - Name: "filter", - In: "query", - Err: err, - } - } - return params, nil -} - // SetSubmissionCompletedParams is parameters of setSubmissionCompleted operation. type SetSubmissionCompletedParams struct { // The unique identifier for a submission. diff --git a/pkg/api/oas_request_decoders_gen.go b/pkg/api/oas_request_decoders_gen.go index ab831ed..7c481ad 100644 --- a/pkg/api/oas_request_decoders_gen.go +++ b/pkg/api/oas_request_decoders_gen.go @@ -220,6 +220,148 @@ func (s *Server) decodeCreateSubmissionRequest(r *http.Request) ( } } +func (s *Server) decodeListScriptPolicyRequest(r *http.Request) ( + req *ListScriptPolicyReq, + close func() error, + rerr error, +) { + var closers []func() error + close = func() error { + var merr error + // Close in reverse order, to match defer behavior. + for i := len(closers) - 1; i >= 0; i-- { + c := closers[i] + merr = multierr.Append(merr, c()) + } + return merr + } + defer func() { + if rerr != nil { + rerr = multierr.Append(rerr, close()) + } + }() + ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) + if err != nil { + return req, close, errors.Wrap(err, "parse media type") + } + switch { + case ct == "application/json": + if r.ContentLength == 0 { + return req, close, validate.ErrBodyRequired + } + buf, err := io.ReadAll(r.Body) + if err != nil { + return req, close, err + } + + if len(buf) == 0 { + return req, close, validate.ErrBodyRequired + } + + d := jx.DecodeBytes(buf) + + var request ListScriptPolicyReq + if err := func() error { + if err := request.Decode(d); err != nil { + return err + } + if err := d.Skip(); err != io.EOF { + return errors.New("unexpected trailing data") + } + return nil + }(); err != nil { + err = &ogenerrors.DecodeBodyError{ + ContentType: ct, + Body: buf, + Err: err, + } + return req, close, err + } + if err := func() error { + if err := request.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + return req, close, errors.Wrap(err, "validate") + } + return &request, close, nil + default: + return req, close, validate.InvalidContentType(ct) + } +} + +func (s *Server) decodeListSubmissionsRequest(r *http.Request) ( + req *ListSubmissionsReq, + close func() error, + rerr error, +) { + var closers []func() error + close = func() error { + var merr error + // Close in reverse order, to match defer behavior. + for i := len(closers) - 1; i >= 0; i-- { + c := closers[i] + merr = multierr.Append(merr, c()) + } + return merr + } + defer func() { + if rerr != nil { + rerr = multierr.Append(rerr, close()) + } + }() + ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) + if err != nil { + return req, close, errors.Wrap(err, "parse media type") + } + switch { + case ct == "application/json": + if r.ContentLength == 0 { + return req, close, validate.ErrBodyRequired + } + buf, err := io.ReadAll(r.Body) + if err != nil { + return req, close, err + } + + if len(buf) == 0 { + return req, close, validate.ErrBodyRequired + } + + d := jx.DecodeBytes(buf) + + var request ListSubmissionsReq + if err := func() error { + if err := request.Decode(d); err != nil { + return err + } + if err := d.Skip(); err != io.EOF { + return errors.New("unexpected trailing data") + } + return nil + }(); err != nil { + err = &ogenerrors.DecodeBodyError{ + ContentType: ct, + Body: buf, + Err: err, + } + return req, close, err + } + if err := func() error { + if err := request.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + return req, close, errors.Wrap(err, "validate") + } + return &request, close, nil + default: + return req, close, validate.InvalidContentType(ct) + } +} + func (s *Server) decodeUpdateScriptRequest(r *http.Request) ( req *ScriptUpdate, close func() error, diff --git a/pkg/api/oas_request_encoders_gen.go b/pkg/api/oas_request_encoders_gen.go index 237b177..87fe78a 100644 --- a/pkg/api/oas_request_encoders_gen.go +++ b/pkg/api/oas_request_encoders_gen.go @@ -53,6 +53,34 @@ func encodeCreateSubmissionRequest( return nil } +func encodeListScriptPolicyRequest( + req *ListScriptPolicyReq, + r *http.Request, +) error { + const contentType = "application/json" + e := new(jx.Encoder) + { + req.Encode(e) + } + encoded := e.Bytes() + ht.SetBody(r, bytes.NewReader(encoded), contentType) + return nil +} + +func encodeListSubmissionsRequest( + req *ListSubmissionsReq, + r *http.Request, +) error { + const contentType = "application/json" + e := new(jx.Encoder) + { + req.Encode(e) + } + encoded := e.Bytes() + ht.SetBody(r, bytes.NewReader(encoded), contentType) + return nil +} + func encodeUpdateScriptRequest( req *ScriptUpdate, r *http.Request, diff --git a/pkg/api/oas_schemas_gen.go b/pkg/api/oas_schemas_gen.go index 5fa2e91..19f088c 100644 --- a/pkg/api/oas_schemas_gen.go +++ b/pkg/api/oas_schemas_gen.go @@ -122,6 +122,56 @@ func (s *ID) SetID(val int64) { s.ID = val } +type ListScriptPolicyReq struct { + Page Pagination `json:"Page"` + Filter OptScriptPolicyFilter `json:"Filter"` +} + +// GetPage returns the value of Page. +func (s *ListScriptPolicyReq) GetPage() Pagination { + return s.Page +} + +// GetFilter returns the value of Filter. +func (s *ListScriptPolicyReq) GetFilter() OptScriptPolicyFilter { + return s.Filter +} + +// SetPage sets the value of Page. +func (s *ListScriptPolicyReq) SetPage(val Pagination) { + s.Page = val +} + +// SetFilter sets the value of Filter. +func (s *ListScriptPolicyReq) SetFilter(val OptScriptPolicyFilter) { + s.Filter = val +} + +type ListSubmissionsReq struct { + Page Pagination `json:"Page"` + Filter OptSubmissionFilter `json:"Filter"` +} + +// GetPage returns the value of Page. +func (s *ListSubmissionsReq) GetPage() Pagination { + return s.Page +} + +// GetFilter returns the value of Filter. +func (s *ListSubmissionsReq) GetFilter() OptSubmissionFilter { + return s.Filter +} + +// SetPage sets the value of Page. +func (s *ListSubmissionsReq) SetPage(val Pagination) { + s.Page = val +} + +// SetFilter sets the value of Filter. +func (s *ListSubmissionsReq) SetFilter(val OptSubmissionFilter) { + s.Filter = val +} + // NewOptInt32 returns new OptInt32 with value set to v. func NewOptInt32(v int32) OptInt32 { return OptInt32{ diff --git a/pkg/api/oas_server_gen.go b/pkg/api/oas_server_gen.go index 36c6a21..e5dc939 100644 --- a/pkg/api/oas_server_gen.go +++ b/pkg/api/oas_server_gen.go @@ -115,13 +115,13 @@ type Handler interface { // Get list of script policies. // // GET /script-policy - ListScriptPolicy(ctx context.Context, params ListScriptPolicyParams) ([]ScriptPolicy, error) + ListScriptPolicy(ctx context.Context, req *ListScriptPolicyReq) ([]ScriptPolicy, error) // ListSubmissions implements listSubmissions operation. // // Get list of submissions. // // GET /submissions - ListSubmissions(ctx context.Context, params ListSubmissionsParams) ([]Submission, error) + ListSubmissions(ctx context.Context, req *ListSubmissionsReq) ([]Submission, error) // SetSubmissionCompleted implements setSubmissionCompleted operation. // // Retrieve map with ID. diff --git a/pkg/api/oas_unimplemented_gen.go b/pkg/api/oas_unimplemented_gen.go index 5b5295c..80bfc46 100644 --- a/pkg/api/oas_unimplemented_gen.go +++ b/pkg/api/oas_unimplemented_gen.go @@ -171,7 +171,7 @@ func (UnimplementedHandler) GetSubmission(ctx context.Context, params GetSubmiss // Get list of script policies. // // GET /script-policy -func (UnimplementedHandler) ListScriptPolicy(ctx context.Context, params ListScriptPolicyParams) (r []ScriptPolicy, _ error) { +func (UnimplementedHandler) ListScriptPolicy(ctx context.Context, req *ListScriptPolicyReq) (r []ScriptPolicy, _ error) { return r, ht.ErrNotImplemented } @@ -180,7 +180,7 @@ func (UnimplementedHandler) ListScriptPolicy(ctx context.Context, params ListScr // Get list of submissions. // // GET /submissions -func (UnimplementedHandler) ListSubmissions(ctx context.Context, params ListSubmissionsParams) (r []Submission, _ error) { +func (UnimplementedHandler) ListSubmissions(ctx context.Context, req *ListSubmissionsReq) (r []Submission, _ error) { return r, ht.ErrNotImplemented } diff --git a/pkg/api/oas_uri_gen.go b/pkg/api/oas_uri_gen.go deleted file mode 100644 index 83d6123..0000000 --- a/pkg/api/oas_uri_gen.go +++ /dev/null @@ -1,464 +0,0 @@ -// Code generated by ogen, DO NOT EDIT. - -package api - -import ( - "math/bits" - "strconv" - - "github.com/go-faster/errors" - - "github.com/ogen-go/ogen/conv" - "github.com/ogen-go/ogen/uri" - "github.com/ogen-go/ogen/validate" -) - -// EncodeURI encodes Pagination as URI form. -func (s *Pagination) EncodeURI(e uri.Encoder) error { - if err := e.EncodeField("Page", func(e uri.Encoder) error { - return e.EncodeValue(conv.Int32ToString(s.Page)) - }); err != nil { - return errors.Wrap(err, "encode field \"Page\"") - } - if err := e.EncodeField("Limit", func(e uri.Encoder) error { - return e.EncodeValue(conv.Int32ToString(s.Limit)) - }); err != nil { - return errors.Wrap(err, "encode field \"Limit\"") - } - return nil -} - -var uriFieldsNameOfPagination = [2]string{ - 0: "Page", - 1: "Limit", -} - -// DecodeURI decodes Pagination from URI form. -func (s *Pagination) DecodeURI(d uri.Decoder) error { - if s == nil { - return errors.New("invalid: unable to decode Pagination to nil") - } - var requiredBitSet [1]uint8 - - if err := d.DecodeFields(func(k string, d uri.Decoder) error { - switch k { - case "Page": - requiredBitSet[0] |= 1 << 0 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt32(val) - if err != nil { - return err - } - - s.Page = c - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"Page\"") - } - case "Limit": - requiredBitSet[0] |= 1 << 1 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt32(val) - if err != nil { - return err - } - - s.Limit = c - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"Limit\"") - } - default: - return nil - } - return nil - }); err != nil { - return errors.Wrap(err, "decode Pagination") - } - // Validate required fields. - var failures []validate.FieldError - for i, mask := range [1]uint8{ - 0b00000011, - } { - if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { - // Mask only required fields and check equality to mask using XOR. - // - // If XOR result is not zero, result is not equal to expected, so some fields are missed. - // Bits of fields which would be set are actually bits of missed fields. - missed := bits.OnesCount8(result) - for bitN := 0; bitN < missed; bitN++ { - bitIdx := bits.TrailingZeros8(result) - fieldIdx := i*8 + bitIdx - var name string - if fieldIdx < len(uriFieldsNameOfPagination) { - name = uriFieldsNameOfPagination[fieldIdx] - } else { - name = strconv.Itoa(fieldIdx) - } - failures = append(failures, validate.FieldError{ - Name: name, - Error: validate.ErrFieldRequired, - }) - // Reset bit. - result &^= 1 << bitIdx - } - } - } - if len(failures) > 0 { - return &validate.Error{Fields: failures} - } - - return nil -} - -// EncodeURI encodes ScriptPolicyFilter as URI form. -func (s *ScriptPolicyFilter) EncodeURI(e uri.Encoder) error { - if err := e.EncodeField("ID", func(e uri.Encoder) error { - if val, ok := s.ID.Get(); ok { - return e.EncodeValue(conv.Int64ToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"ID\"") - } - if err := e.EncodeField("FromScriptHash", func(e uri.Encoder) error { - if val, ok := s.FromScriptHash.Get(); ok { - return e.EncodeValue(conv.StringToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"FromScriptHash\"") - } - if err := e.EncodeField("ToScriptID", func(e uri.Encoder) error { - if val, ok := s.ToScriptID.Get(); ok { - return e.EncodeValue(conv.Int64ToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"ToScriptID\"") - } - if err := e.EncodeField("Policy", func(e uri.Encoder) error { - if val, ok := s.Policy.Get(); ok { - return e.EncodeValue(conv.Int32ToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"Policy\"") - } - return nil -} - -var uriFieldsNameOfScriptPolicyFilter = [4]string{ - 0: "ID", - 1: "FromScriptHash", - 2: "ToScriptID", - 3: "Policy", -} - -// DecodeURI decodes ScriptPolicyFilter from URI form. -func (s *ScriptPolicyFilter) DecodeURI(d uri.Decoder) error { - if s == nil { - return errors.New("invalid: unable to decode ScriptPolicyFilter to nil") - } - - if err := d.DecodeFields(func(k string, d uri.Decoder) error { - switch k { - case "ID": - if err := func() error { - var sDotIDVal int64 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt64(val) - if err != nil { - return err - } - - sDotIDVal = c - return nil - }(); err != nil { - return err - } - s.ID.SetTo(sDotIDVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"ID\"") - } - case "FromScriptHash": - if err := func() error { - var sDotFromScriptHashVal string - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToString(val) - if err != nil { - return err - } - - sDotFromScriptHashVal = c - return nil - }(); err != nil { - return err - } - s.FromScriptHash.SetTo(sDotFromScriptHashVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"FromScriptHash\"") - } - case "ToScriptID": - if err := func() error { - var sDotToScriptIDVal int64 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt64(val) - if err != nil { - return err - } - - sDotToScriptIDVal = c - return nil - }(); err != nil { - return err - } - s.ToScriptID.SetTo(sDotToScriptIDVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"ToScriptID\"") - } - case "Policy": - if err := func() error { - var sDotPolicyVal int32 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt32(val) - if err != nil { - return err - } - - sDotPolicyVal = c - return nil - }(); err != nil { - return err - } - s.Policy.SetTo(sDotPolicyVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"Policy\"") - } - default: - return nil - } - return nil - }); err != nil { - return errors.Wrap(err, "decode ScriptPolicyFilter") - } - - return nil -} - -// EncodeURI encodes SubmissionFilter as URI form. -func (s *SubmissionFilter) EncodeURI(e uri.Encoder) error { - if err := e.EncodeField("ID", func(e uri.Encoder) error { - return e.EncodeValue(conv.Int64ToString(s.ID)) - }); err != nil { - return errors.Wrap(err, "encode field \"ID\"") - } - if err := e.EncodeField("DisplayName", func(e uri.Encoder) error { - if val, ok := s.DisplayName.Get(); ok { - return e.EncodeValue(conv.StringToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"DisplayName\"") - } - if err := e.EncodeField("Creator", func(e uri.Encoder) error { - if val, ok := s.Creator.Get(); ok { - return e.EncodeValue(conv.StringToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"Creator\"") - } - if err := e.EncodeField("GameID", func(e uri.Encoder) error { - if val, ok := s.GameID.Get(); ok { - return e.EncodeValue(conv.Int32ToString(val)) - } - return nil - }); err != nil { - return errors.Wrap(err, "encode field \"GameID\"") - } - return nil -} - -var uriFieldsNameOfSubmissionFilter = [4]string{ - 0: "ID", - 1: "DisplayName", - 2: "Creator", - 3: "GameID", -} - -// DecodeURI decodes SubmissionFilter from URI form. -func (s *SubmissionFilter) DecodeURI(d uri.Decoder) error { - if s == nil { - return errors.New("invalid: unable to decode SubmissionFilter to nil") - } - var requiredBitSet [1]uint8 - - if err := d.DecodeFields(func(k string, d uri.Decoder) error { - switch k { - case "ID": - requiredBitSet[0] |= 1 << 0 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt64(val) - if err != nil { - return err - } - - s.ID = c - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"ID\"") - } - case "DisplayName": - if err := func() error { - var sDotDisplayNameVal string - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToString(val) - if err != nil { - return err - } - - sDotDisplayNameVal = c - return nil - }(); err != nil { - return err - } - s.DisplayName.SetTo(sDotDisplayNameVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"DisplayName\"") - } - case "Creator": - if err := func() error { - var sDotCreatorVal string - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToString(val) - if err != nil { - return err - } - - sDotCreatorVal = c - return nil - }(); err != nil { - return err - } - s.Creator.SetTo(sDotCreatorVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"Creator\"") - } - case "GameID": - if err := func() error { - var sDotGameIDVal int32 - if err := func() error { - val, err := d.DecodeValue() - if err != nil { - return err - } - - c, err := conv.ToInt32(val) - if err != nil { - return err - } - - sDotGameIDVal = c - return nil - }(); err != nil { - return err - } - s.GameID.SetTo(sDotGameIDVal) - return nil - }(); err != nil { - return errors.Wrap(err, "decode field \"GameID\"") - } - default: - return nil - } - return nil - }); err != nil { - return errors.Wrap(err, "decode SubmissionFilter") - } - // Validate required fields. - var failures []validate.FieldError - for i, mask := range [1]uint8{ - 0b00000001, - } { - if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { - // Mask only required fields and check equality to mask using XOR. - // - // If XOR result is not zero, result is not equal to expected, so some fields are missed. - // Bits of fields which would be set are actually bits of missed fields. - missed := bits.OnesCount8(result) - for bitN := 0; bitN < missed; bitN++ { - bitIdx := bits.TrailingZeros8(result) - fieldIdx := i*8 + bitIdx - var name string - if fieldIdx < len(uriFieldsNameOfSubmissionFilter) { - name = uriFieldsNameOfSubmissionFilter[fieldIdx] - } else { - name = strconv.Itoa(fieldIdx) - } - failures = append(failures, validate.FieldError{ - Name: name, - Error: validate.ErrFieldRequired, - }) - // Reset bit. - result &^= 1 << bitIdx - } - } - } - if len(failures) > 0 { - return &validate.Error{Fields: failures} - } - - return nil -} diff --git a/pkg/api/oas_validators_gen.go b/pkg/api/oas_validators_gen.go index 5ec5638..fd31926 100644 --- a/pkg/api/oas_validators_gen.go +++ b/pkg/api/oas_validators_gen.go @@ -8,6 +8,88 @@ import ( "github.com/ogen-go/ogen/validate" ) +func (s *ListScriptPolicyReq) Validate() error { + if s == nil { + return validate.ErrNilPointer + } + + var failures []validate.FieldError + if err := func() error { + if err := s.Page.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + failures = append(failures, validate.FieldError{ + Name: "Page", + Error: err, + }) + } + if err := func() error { + if value, ok := s.Filter.Get(); ok { + if err := func() error { + if err := value.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + return err + } + } + return nil + }(); err != nil { + failures = append(failures, validate.FieldError{ + Name: "Filter", + Error: err, + }) + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + return nil +} + +func (s *ListSubmissionsReq) Validate() error { + if s == nil { + return validate.ErrNilPointer + } + + var failures []validate.FieldError + if err := func() error { + if err := s.Page.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + failures = append(failures, validate.FieldError{ + Name: "Page", + Error: err, + }) + } + if err := func() error { + if value, ok := s.Filter.Get(); ok { + if err := func() error { + if err := value.Validate(); err != nil { + return err + } + return nil + }(); err != nil { + return err + } + } + return nil + }(); err != nil { + failures = append(failures, validate.FieldError{ + Name: "Filter", + Error: err, + }) + } + if len(failures) > 0 { + return &validate.Error{Fields: failures} + } + return nil +} + func (s *Pagination) Validate() error { if s == nil { return validate.ErrNilPointer diff --git a/pkg/service/script_policy.go b/pkg/service/script_policy.go index 855e79b..8c33d64 100644 --- a/pkg/service/script_policy.go +++ b/pkg/service/script_policy.go @@ -53,18 +53,18 @@ func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolic // Get list of script policies. // // GET /script-policy -func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptPolicyParams) ([]api.ScriptPolicy, error) { +func (svc *Service) ListScriptPolicy(ctx context.Context, request *api.ListScriptPolicyReq) ([]api.ScriptPolicy, error) { filter := datastore.Optional() //fmt.Println(request) - if params.Filter.IsSet() { - filter.AddNotNil("from_script_hash", params.Filter.Value.FromScriptHash) - filter.AddNotNil("to_script_id", params.Filter.Value.ToScriptID) - filter.AddNotNil("policy", params.Filter.Value.Policy) + if request.Filter.IsSet() { + filter.AddNotNil("from_script_hash", request.Filter.Value.FromScriptHash) + filter.AddNotNil("to_script_id", request.Filter.Value.ToScriptID) + filter.AddNotNil("policy", request.Filter.Value.Policy) } items, err := svc.DB.ScriptPolicy().List(ctx, filter, model.Page{ - Number: params.Page.GetPage(), - Size: params.Page.GetLimit(), + Number: request.Page.GetPage(), + Size: request.Page.GetLimit(), }) if err != nil { return nil, err diff --git a/pkg/service/submissions.go b/pkg/service/submissions.go index ce14685..c2af56d 100644 --- a/pkg/service/submissions.go +++ b/pkg/service/submissions.go @@ -67,7 +67,7 @@ func (svc *Service) GetSubmission(ctx context.Context, params api.GetSubmissionP // Get list of submissions. // // GET /submissions -func (svc *Service) ListSubmissions(ctx context.Context, request api.ListSubmissionsParams) ([]api.Submission, error) { +func (svc *Service) ListSubmissions(ctx context.Context, request *api.ListSubmissionsReq) ([]api.Submission, error) { filter := datastore.Optional() //fmt.Println(request) if request.Filter.IsSet() {