podhmo / reflect-openapi

Define OpenAPI with reflect package
Apache License 2.0
3 stars 0 forks source link

Visitor order #64

Closed podhmo closed 3 years ago

podhmo commented 3 years ago

changes to use RegisterFunc() with After() instead of Visitor.VisitFunc()

before

    c.EmitDoc(func(m *reflectopenapi.Manager) {
        {
            m.Visitor.VisitType(SortOrderAsc, func(schema *openapi3.Schema) {
                schema.Enum = []interface{}{
                    SortOrderDesc,
                    SortOrderAsc,
                }
            })
        }
        // WARNING: currently, it is ignored that the VisitType() effect after VisitFunc() .
        // So, please calling VisitType() before VisitType().
        {
            op := m.Visitor.VisitFunc(ListTodo)
            m.Doc.AddOperation("/todo", "GET", op)
        }
        {
            op := m.Visitor.VisitFunc(GetTodo)
            m.Doc.AddOperation("/todo/{id}", "GET", op)
        }
    })

after

    c.EmitDoc(func(m *reflectopenapi.Manager) {
        m.RegisterFunc(ListTodo).After(func(op *openapi3.Operation) {
            m.Doc.AddOperation("/todo", "GET", op)
        })
        m.RegisterType(SortOrderAsc, func(schema *openapi3.Schema) {
            schema.Enum = []interface{}{
                SortOrderDesc,
                SortOrderAsc,
            }
        })
        m.RegisterFunc(GetTodo).After(func(op *openapi3.Operation) {
            m.Doc.AddOperation("/todo/{id}", "GET", op)
        })
    })