github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/desc/parser_test.go (about)

     1  package desc_test
     2  
     3  import (
     4  	"github.com/clubpay/ronykit/kit"
     5  	"github.com/clubpay/ronykit/kit/desc"
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  type dummyRESTSelector struct {
    11  	enc    kit.Encoding
    12  	path   string
    13  	method string
    14  }
    15  
    16  func newREST(enc kit.Encoding, path, method string) dummyRESTSelector {
    17  	return dummyRESTSelector{
    18  		enc:    enc,
    19  		path:   path,
    20  		method: method,
    21  	}
    22  }
    23  
    24  func (d dummyRESTSelector) Query(_ string) any {
    25  	return nil
    26  }
    27  
    28  func (d dummyRESTSelector) GetEncoding() kit.Encoding {
    29  	return d.enc
    30  }
    31  
    32  func (d dummyRESTSelector) GetMethod() string {
    33  	return d.method
    34  }
    35  
    36  func (d dummyRESTSelector) GetPath() string {
    37  	return d.path
    38  }
    39  
    40  type FlatMessage struct {
    41  	A string                       `json:"a"`
    42  	B int64                        `json:"b"`
    43  	C map[string]string            `json:"c"`
    44  	D map[string]int64             `json:"d"`
    45  	E map[int64]string             `json:"e"`
    46  	G [][]string                   `json:"g"`
    47  	M map[string]map[string]string `json:"m"`
    48  }
    49  
    50  type NestedMessage struct {
    51  	A    string                  `json:"a"`
    52  	B    FlatMessage             `json:"b"`
    53  	BA   []FlatMessage           `json:"ba,omitempty"`
    54  	BM   map[string]FlatMessage  `json:"bm"`
    55  	C    *FlatMessage            `json:"c"`
    56  	PA   []*FlatMessage          `json:"pa"`
    57  	PMap map[string]*FlatMessage `json:"pmap"`
    58  }
    59  
    60  var _ = Describe("DescParser", func() {
    61  	d := desc.NewService("sample").
    62  		AddContract(
    63  			desc.NewContract().
    64  				SetName("c1").
    65  				NamedSelector("s1", newREST(kit.JSON, "/path1", "GET")).
    66  				NamedSelector("s2", newREST(kit.JSON, "/path2", "POST")).
    67  				In(&NestedMessage{}).
    68  				Out(&FlatMessage{}),
    69  		)
    70  
    71  	It("should parse the description", func() {
    72  		pd := desc.ParseService(d)
    73  		contract0 := pd.Contracts[0]
    74  		contract1 := pd.Contracts[1]
    75  		Expect(pd.Contracts).To(HaveLen(2))
    76  
    77  		Expect(contract0.Name).To(Equal("s1"))
    78  		Expect(contract0.Type).To(Equal(desc.REST))
    79  		Expect(contract0.Encoding).To(Equal(kit.JSON.Tag()))
    80  		Expect(contract0.Path).To(Equal("/path1"))
    81  		Expect(contract0.Method).To(Equal("GET"))
    82  		Expect(contract0.GroupName).To(Equal("c1"))
    83  		Expect(contract0.Request.Message.Name).To(Equal("NestedMessage"))
    84  		Expect(contract0.Request.Message.Fields).To(HaveLen(7))
    85  		Expect(contract0.Responses[0].Message.Name).To(Equal("FlatMessage"))
    86  		Expect(contract0.Responses[0].Message.Fields).To(HaveLen(6))
    87  
    88  		Expect(contract1.Name).To(Equal("s2"))
    89  		Expect(contract1.Type).To(Equal(desc.REST))
    90  		Expect(contract1.Encoding).To(Equal(kit.JSON.Tag()))
    91  		Expect(contract1.Path).To(Equal("/path2"))
    92  		Expect(contract1.Method).To(Equal("POST"))
    93  		Expect(contract1.GroupName).To(Equal("c1"))
    94  
    95  		Expect(contract0.Request.Message.Name).To(Equal("NestedMessage"))
    96  		Expect(contract0.Request.Message.Fields).To(HaveLen(7))
    97  		Expect(contract0.Request.Message.Fields[0].Name).To(Equal("a"))
    98  		Expect(contract0.Request.Message.Fields[0].Kind).To(Equal(desc.String))
    99  		Expect(contract0.Request.Message.Fields[1].Name).To(Equal("b"))
   100  		Expect(contract0.Request.Message.Fields[1].Kind).To(Equal(desc.Object))
   101  		Expect(contract0.Request.Message.Fields[1].Message.Name).To(Equal("FlatMessage"))
   102  		Expect(contract0.Request.Message.Fields[1].Optional).To(BeFalse())
   103  		Expect(contract0.Request.Message.Fields[4].Name).To(Equal("c"))
   104  		Expect(contract0.Request.Message.Fields[4].Kind).To(Equal(desc.Object))
   105  		Expect(contract0.Request.Message.Fields[4].Message.Name).To(Equal("FlatMessage"))
   106  		Expect(contract0.Request.Message.Fields[4].Optional).To(BeTrue())
   107  		Expect(contract0.Request.Message.FieldByName("ba").Kind).To(Equal(desc.Array))
   108  		Expect(contract0.Request.Message.FieldByGoName("BA").Element.Kind).To(Equal(desc.Object))
   109  		Expect(contract0.Request.Message.FieldByGoName("PA").Kind).To(Equal(desc.Array))
   110  		Expect(contract0.Request.Message.FieldByGoName("PA").Element.Kind).To(Equal(desc.Object))
   111  		Expect(contract0.Request.Message.FieldByGoName("PA").Element.Message.Name).To(Equal("FlatMessage"))
   112  		Expect(contract0.Request.Message.FieldByGoName("PMap").Kind).To(Equal(desc.Map))
   113  		Expect(contract0.Request.Message.FieldByGoName("PMap").Element.Kind).To(Equal(desc.Object))
   114  		Expect(contract0.Request.Message.FieldByGoName("PMap").Element.Message.Name).To(Equal("FlatMessage"))
   115  
   116  		b := contract0.Request.Message.Fields[1]
   117  		Expect(b.Name).To(Equal("b"))
   118  		Expect(b.Kind).To(Equal(desc.Object))
   119  		Expect(b.Message.Name).To(Equal("FlatMessage"))
   120  		Expect(b.Message.Fields).To(HaveLen(6))
   121  		Expect(b.Message.Fields[0].Name).To(Equal("a"))
   122  		Expect(b.Message.Fields[0].Kind).To(Equal(desc.String))
   123  
   124  		ba := contract0.Request.Message.Fields[2]
   125  		Expect(ba.Name).To(Equal("ba"))
   126  		Expect(ba.Kind).To(Equal(desc.Array))
   127  		Expect(ba.Message).To(BeNil())
   128  		Expect(ba.Element.Kind).To(Equal(desc.Object))
   129  		Expect(ba.Element.Message.Name).To(Equal("FlatMessage"))
   130  		Expect(ba.Element.Message.Fields).To(HaveLen(6))
   131  		Expect(ba.Element.Message.Fields[0].Name).To(Equal("a"))
   132  		Expect(ba.Element.Message.Fields[0].Kind).To(Equal(desc.String))
   133  
   134  		g := contract0.Responses[0].Message.Fields[4]
   135  		Expect(g.Name).To(Equal("g"))
   136  		Expect(g.Kind).To(Equal(desc.Array))
   137  		Expect(g.Element.Kind).To(Equal(desc.Array))
   138  		Expect(g.Element.Element.Kind).To(Equal(desc.String))
   139  		Expect(g.Element.Element.Message).To(BeNil())
   140  		Expect(g.Element.Message).To(BeNil())
   141  
   142  		m := contract0.Responses[0].Message.Fields[5]
   143  		Expect(m.Name).To(Equal("m"))
   144  		Expect(m.Kind).To(Equal(desc.Map))
   145  		Expect(m.Element.Kind).To(Equal(desc.Map))
   146  		Expect(m.Element.Message).To(BeNil())
   147  		Expect(m.Element.Element.Kind).To(Equal(desc.String))
   148  		Expect(m.Element.Element.Message).To(BeNil())
   149  	})
   150  })
   151  
   152  var _ = Describe("ParseMessage.JSON()", func() {
   153  	d := desc.NewService("sample").
   154  		AddContract(
   155  			desc.NewContract().
   156  				SetInputHeader(
   157  					desc.RequiredHeader("hdr1"),
   158  					desc.OptionalHeader("optionalHdr1"),
   159  				).
   160  				SetName("c1").
   161  				NamedSelector("s1", newREST(kit.JSON, "/path1", "GET")).
   162  				NamedSelector("s2", newREST(kit.JSON, "/path2", "POST")).
   163  				In(&NestedMessage{}).
   164  				Out(&FlatMessage{}),
   165  		)
   166  
   167  	It("Parse Service", func() {
   168  		ps := desc.ParseService(d)
   169  		Expect(ps.Messages()).To(HaveLen(2))
   170  		Expect(ps.Contracts).To(HaveLen(2))
   171  		Expect(ps.Contracts[0].Type).To(Equal(desc.REST))
   172  		Expect(ps.Contracts[0].Request.Headers).To(HaveLen(2))
   173  		Expect(ps.Contracts[0].Request.Headers[0].Required).To(BeTrue())
   174  		Expect(ps.Contracts[0].Request.Headers[0].Name).To(Equal("hdr1"))
   175  		Expect(ps.Contracts[0].Request.Headers[1].Required).To(BeFalse())
   176  		Expect(ps.Contracts[0].Request.Headers[1].Name).To(Equal("optionalHdr1"))
   177  	})
   178  })