@@ -23,21 +23,26 @@ type cxRequestBeforeRequest struct {
2323
2424 method , url starlark.String
2525 headers * cxHead
26- body * structpb. Value //FIXME: starlark.Value + test that edits .body (as num and as dict/list, and as set)
26+ body starlark.Value
2727}
2828
2929func newCxRequestBeforeRequest (input * fm.Srv_Call_Input ) * cxRequestBeforeRequest {
3030 switch x := input .GetInput ().(type ) {
3131
3232 case * fm.Srv_Call_Input_HttpRequest_ :
3333 r := input .GetHttpRequest ()
34+ // var bodyValue starlark.Value = starlark.None
35+ var bodyValue starlark.Value = nil
36+ if body := r .GetBody (); body != nil {
37+ bodyValue = starlarkvalue .FromProtoValue (body )
38+ }
3439 return & cxRequestBeforeRequest {
3540 ty : cxRequestHttp ,
3641 method : starlark .String (r .GetMethod ()),
3742 url : starlark .String (r .GetUrl ()),
3843 headers : newcxHead (r .GetHeaders ()),
3944 //content: absent as encoding will only happen later
40- body : r . GetBody () ,
45+ body : bodyValue ,
4146 }
4247
4348 default :
@@ -55,24 +60,25 @@ func (cr *cxRequestBeforeRequest) IntoProto(err error) *fm.Clt_CallRequestRaw {
5560 switch cr .ty {
5661 case cxRequestHttp :
5762 var body []byte
63+ var bodyDecoded * structpb.Value
5864 if cr .body != nil {
59- if body , err = protojson .Marshal (cr .body ); err != nil {
65+ bodyDecoded = starlarkvalue .ToProtoValue (cr .body )
66+ if body , err = protojson .Marshal (bodyDecoded ); err != nil {
6067 log .Println ("[ERR]" , err )
6168 // return after sending msg
6269 if len (reason ) == 0 && err != nil {
6370 reason = strings .Split (err .Error (), "\n " )
6471 }
6572 }
6673 }
67- //TODO: impl ToProtoValue
6874 return & fm.Clt_CallRequestRaw_Input {
6975 Input : & fm.Clt_CallRequestRaw_Input_HttpRequest_ {
7076 HttpRequest : & fm.Clt_CallRequestRaw_Input_HttpRequest {
7177 Method : string (cr .method ),
7278 Url : string (cr .url ),
7379 Headers : cr .headers .IntoProto (),
7480 Body : body ,
75- BodyDecoded : cr . body ,
81+ BodyDecoded : bodyDecoded ,
7682 }}}
7783
7884 default :
@@ -97,8 +103,10 @@ func (cr *cxRequestBeforeRequest) Truth() starlark.Bool { return true }
97103func (cr * cxRequestBeforeRequest ) Type () string { return cr .ty }
98104
99105func (cr * cxRequestBeforeRequest ) Freeze () {
100- // cr.body.Freeze() FIXME
106+ cr .body .Freeze ()
101107 cr .headers .Freeze ()
108+ cr .method .Freeze ()
109+ cr .url .Freeze ()
102110}
103111
104112func (cr * cxRequestBeforeRequest ) AttrNames () []string {
@@ -113,13 +121,10 @@ func (cr *cxRequestBeforeRequest) AttrNames() []string {
113121func (cr * cxRequestBeforeRequest ) Attr (name string ) (starlark.Value , error ) {
114122 switch name {
115123 case "body" :
116- var body starlark.Value = starlark .None
117124 if cr .body != nil {
118- body = starlarkvalue . FromProtoValue ( cr .body )
125+ return cr .body , nil
119126 }
120- body .Freeze ()
121- // TODO: call ProtoCompatible here
122- return body , nil
127+ return starlark .None , nil
123128 case "headers" :
124129 return cr .headers , nil
125130 case "method" :
@@ -130,3 +135,12 @@ func (cr *cxRequestBeforeRequest) Attr(name string) (starlark.Value, error) {
130135 return nil , nil // no such method
131136 }
132137}
138+
139+ var _ starlark.HasSetField = (* cxRequestBeforeRequest )(nil )
140+
141+ func (cr * cxRequestBeforeRequest ) SetField (name string , val starlark.Value ) error {
142+ if name == "body" && cr .body == nil {
143+ cr .body = val
144+ }
145+ return nil
146+ }
0 commit comments