Skip to content

Composing getNodeType #139

@Fer0x

Description

@Fer0x

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions