Spec change - merge() function

Đang mở
#4 2 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, typescript
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu với phần merge() trong đặc tả và phần triển khai được đề xuất trong jmespath.ts. Xác định liệu merge() nên chấp nhận các object, các array hay một kiểu array-of-objects bị giới hạn, bao gồm cả hành vi đã được ghi lại của các phần tử array. Công việc được xem là hoàn tất khi các ví dụ trong đặc tả cùng với chữ ký runtime và phần triển khai trong jmespath.ts khớp với ngữ nghĩa được chấp nhận.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Background

In all the example given in the spec page for the merge() functionLITERAL types are provided as arguments to the function. While this demonstrates the behaviour it is not at all useful in practical terms. Literals can only be explicitly defined by the editor of an expression. Much more useful would be to merge on results of expressions or on projections.

Examples:
search([{a: "AY", x: "EX"}, {a: "EY", b: "BEE"}], 'merge(@)')
// OUTPUTS: {"a": "EY", "b": "BEE", "x": "EX"}

search([{a: "AY", x: "EX"}, {a: "EY", b: "BEE"}], 'merge([{FED: @[0].x}, {BAR: @[1].b}])')
// OUTPUTS: {"BAR": "BEE", "FED": "EX"}

In order to do this merge would have to accept either TYPE_OBJECT or TYPE_ARRAY to work and the function would need to be able to switch on either type to handle this.

Discussion

Array can contain any value and ideally we may want to restrict it to a new TYPE_ARRAY_OBJECT type so that we can guarantee only objects can be used in the merge. One interesting side effect of this in javascript is that merging arrays results in array members being keyed in their index. This means we can do the following with very little code change:

search([{"A": "ONE"}, "TWO", {"C": "THREE"}], 'merge(@)')
// OUTPUTS: {"0": "ZERO", "A": "ONE", "C": "THREE"}

// With all JSON array member types:

search([{"A": "ONE"}, true, null, "BAR", ["ZERO"], 666], 'merge(@)')
// OUTPUTS: {"0": "ZERO", "1": "A", "2": "R", "A": "ONE"}

Implementation in jmespath.ts

// Runtime
  private functionMerge: RuntimeFunction<JSONObject[], JSONObject> = resolvedArgs => {
    let merged = {};
    for (let i = 0; i < resolvedArgs.length; i += 1) {
      const current = resolvedArgs[i];
      if (Array.isArray(current)) {
        merged = Object.assign(merged, ...current);
      } else {
        merged = Object.assign(merged, current);
      }
    }
    return merged;
  };

// and in the function table

    merge: {
      _func: this.functionMerge,
      _signature: [
        {
          types: [InputArgument.TYPE_OBJECT, InputArgument.TYPE_ARRAY],
          variadic: true,
        },
      ],
    },

Ngôn ngữ chính
Python
Star
10
Fork
3
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của jmespath/jmespath.jep

Tất cả issue của jmespath/jmespath.jep

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.