Skip to content

How about adding a helper for contains? #1047

Open

Description

@spacether

How about adding a helper for contains?
It would work for maps and sets and check for key presence.

{{#contains someMap "someKey"}}
{{/contains}}

{{#contains someSet "someKey"}}
{{/contains}}

Activity

  1. spacether commented on Feb 4, 2023

    @spacether
    Author

    This works:

    public enum ContainsHelper implements Helper<Object> {
    
        /**
         * Indicates the set or map contains the given key
         * For example:
         *
         * <pre>
         * {{#contains someMap "someKey"}}
         * {{/contains}}
         * </pre>
         *
         * <pre>
         * {{#contains someSet "someItem"}}
         * {{/contains}}
         * </pre>
         */
        contains {
            @Override public Object apply(final Object a, final Options options) throws IOException {
                Object b = options.param(0, null);
                boolean result;
                if (Map.class.isInstance(a)) {
                    Map mapA = (Map) a;
                    result = mapA.containsKey(b);
                } else if (Set.class.isInstance(a)) {
                    Set setA = (Set) a;
                    result = setA.contains(b);
                } else {
                    result = false;
                }
                if (options.tagType == TagType.SECTION) {
                    return result ? options.fn() : options.inverse();
                }
                return result
                        ? options.hash("yes", true)
                        : options.hash("no", false);
            }
        }
    }
    

    And I created it in openapi-json-schema-tools/openapi-json-schema-generator#130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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