Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composing getNodeType #139

Open
1 task done
Fer0x opened this issue Jul 11, 2022 · 0 comments
Open
1 task done

Composing getNodeType #139

Fer0x opened this issue Jul 11, 2022 · 0 comments
Labels
feature-request New feature or request

Comments

@Fer0x
Copy link
Contributor

Fer0x commented Jul 11, 2022

This is copy of internal issue created before autoviews was migrated to external github project

Is there an existing issue for this?

  • Yes, I have searched the existing issues

The problem

composeRepos is amazing for component records & wrappers, but it can't do much with getNodeType.
As example, here is implementation of getNodeType from two composed repos (real example from AutoCMS):

// filter-button repo
node => {
  if ('enum' in node) {
      return 'enum';
  }
  
  if (
      'additionalProperties' in node &&
      'wixDataType' in node.additionalProperties
  ) {
      return node.additionalProperties.wixDataType;
  }

  return node.type;
}
// subschemas-v1-compatible-repo
node => {
    switch (true) {
        // `$ref`, `oneOf` and `if` are in order as they were in AutoViews.render
        case '$ref' in node: return '$ref';
        case 'oneOf' in node: return 'oneOf';
        case 'if' in node: return 'if/then/else';
        case 'enum' in node: return 'enum';
        default: return node.type;
    }
}


And composed result expected to be like this:

```ts
node => {
    if ('oneOf' in node) {
        return 'oneOf';
    }

    if ('if' in node) {
        return 'if/then/else';
    }

    if ('enum' in node) {
        return 'enum';
    }

    if (
        'additionalProperties' in node &&
        'wixDataType' in node.additionalProperties
    ) {
        return node.additionalProperties.wixDataType;
    }

    return node.type;
}

Describe the solution you'd like

We may stop using default return in getNodeType, or/and change entire format.
For example, getNodeType might be some array with predicates and type values.

// Similar to repositories, huh?
const getNodeType = [
    {
        predicate: node => 'enum' in node,
        getType: () => 'enum'
    },
    {
        predicate: node => 'additionalProperties' in node && 'wixDataType' in node.additionalProperties,
        getType: node => node.additionalProperties.wixDataType
    },
    {
        predicate: node => '$ref' in node,
        getType: () => '$ref'
    },
    {
        // default predicate if needed
        predicate: () => true,
        getType: node => node.type
    }
];

Those allows to merge multiple different getNodeTypes objects. However it doesn't cover cases, when complex merge should be applied, like switching order of predicates or like ignoring duplicates.

Describe alternatives you've considered

No response

@Fer0x Fer0x added the feature-request New feature or request label Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant