Skip to content

Commit

Permalink
🎨 URL handling to use lib.refToGoURLValue
Browse files Browse the repository at this point in the history
also lib.refToGoUserinfoValue
  • Loading branch information
pikachu0310 committed Oct 1, 2024
1 parent 2ce32a6 commit 38a3d88
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions grpc/federation/cel/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (lib *URLLibrary) refToGoURLValue(v ref.Val) url.URL {
return v.Value().(*URL).GoURL()
}

func (lib *URLLibrary) refToGoUserinfoValue(v ref.Val) *url.Userinfo {
return v.Value().(*Userinfo).GoUserinfo()
}

func (lib *URLLibrary) toURLValue(v url.URL) ref.Val {
var userinfo *Userinfo
if v.User != nil {
Expand Down Expand Up @@ -204,7 +208,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"opaque",
MemberOverloadFunc(createURLID("opaque_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Opaque)
},
),
Expand All @@ -213,7 +217,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"userinfo", // user is not a valid field name
MemberOverloadFunc(createURLID("user_url_userinfo"), URLType, []*cel.Type{}, UserinfoType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
password, hasPassword := v.User.Password()
return lib.toUserinfoValue(v.User.Username(), password, hasPassword)
},
Expand All @@ -223,7 +227,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"host",
MemberOverloadFunc(createURLID("host_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Host)
},
),
Expand All @@ -232,7 +236,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"path",
MemberOverloadFunc(createURLID("path_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Path)
},
),
Expand All @@ -241,7 +245,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"rawPath",
MemberOverloadFunc(createURLID("rawPath_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.RawPath)
},
),
Expand All @@ -250,7 +254,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"omitHost",
MemberOverloadFunc(createURLID("omitHost_url_bool"), URLType, []*cel.Type{}, cel.BoolType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.Bool(v.OmitHost)
},
),
Expand All @@ -259,7 +263,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"forceQuery",
MemberOverloadFunc(createURLID("forceQuery_url_bool"), URLType, []*cel.Type{}, cel.BoolType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.Bool(v.ForceQuery)
},
),
Expand All @@ -268,7 +272,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"rawQuery",
MemberOverloadFunc(createURLID("rawQuery_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.RawQuery)
},
),
Expand All @@ -277,7 +281,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"fragment",
MemberOverloadFunc(createURLID("fragment_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Fragment)
},
),
Expand All @@ -286,7 +290,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"rawFragment",
MemberOverloadFunc(createURLID("rawFragment_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.RawFragment)
},
),
Expand All @@ -295,7 +299,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"escapedFragment",
MemberOverloadFunc(createURLID("escapedFragment_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.EscapedFragment())
},
),
Expand All @@ -304,7 +308,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"escapedPath",
MemberOverloadFunc(createURLID("escapedPath_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.EscapedPath())
},
),
Expand All @@ -313,7 +317,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"hostname",
MemberOverloadFunc(createURLID("hostname_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Hostname())
},
),
Expand All @@ -322,7 +326,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"isAbs",
MemberOverloadFunc(createURLID("isAbs_url_bool"), URLType, []*cel.Type{}, cel.BoolType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.Bool(v.IsAbs())
},
),
Expand All @@ -331,7 +335,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"joinPath",
MemberOverloadFunc(createURLID("joinPath_url_strings_url"), URLType, []*cel.Type{cel.ListType(cel.StringType)}, URLType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)

elems := args[0].(traits.Lister)
var paths []string
Expand All @@ -354,7 +358,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"marshalBinary",
MemberOverloadFunc(createURLID("MarshalBinary_url_bytes"), URLType, []*cel.Type{}, cel.BytesType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)

b, err := v.MarshalBinary()
if err != nil {
Expand All @@ -369,7 +373,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"parse",
MemberOverloadFunc(createURLID("parse_url_string_url"), URLType, []*cel.Type{cel.StringType}, URLType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)

u, err := v.Parse(string(args[0].(types.String)))
if err != nil {
Expand All @@ -384,7 +388,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"port",
MemberOverloadFunc(createURLID("port_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Port())
},
),
Expand All @@ -394,7 +398,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"query",
MemberOverloadFunc(createURLID("query_url_map"), URLType, []*cel.Type{}, cel.MapType(cel.StringType, cel.ListType(cel.StringType)),
func(ctx context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)

adapter := types.DefaultTypeAdapter
queryParams := v.Query()
Expand All @@ -411,7 +415,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"redacted",
MemberOverloadFunc(createURLID("redacted_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.Redacted())
},
),
Expand All @@ -420,7 +424,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"requestURI",
MemberOverloadFunc(createURLID("requestURI_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.RequestURI())
},
),
Expand All @@ -429,7 +433,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"resolveReference",
MemberOverloadFunc(createURLID("resolveReference_url_url_url"), URLType, []*cel.Type{URLType}, URLType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)

r := args[0].Value().(*URL).GoURL()
u := v.ResolveReference(&r)
Expand All @@ -442,7 +446,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"string",
MemberOverloadFunc(createURLID("string_url_string"), URLType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*URL).GoURL()
v := lib.refToGoURLValue(self)
return types.String(v.String())
},
),
Expand Down Expand Up @@ -482,7 +486,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"username",
MemberOverloadFunc(createURLID("username_userinfo_string"), UserinfoType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*Userinfo).GoUserinfo()
v := lib.refToGoUserinfoValue(self)
return types.String(v.Username())
},
),
Expand All @@ -491,7 +495,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"password",
MemberOverloadFunc(createURLID("password_userinfo_string"), UserinfoType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*Userinfo).GoUserinfo()
v := lib.refToGoUserinfoValue(self)
password, _ := v.Password()
return types.String(password)
},
Expand All @@ -501,7 +505,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"passwordSet",
MemberOverloadFunc(createURLID("passwordSet_userinfo_bool"), UserinfoType, []*cel.Type{}, cel.BoolType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*Userinfo).GoUserinfo()
v := lib.refToGoUserinfoValue(self)
_, hasPassword := v.Password()
return types.Bool(hasPassword)
},
Expand All @@ -511,7 +515,7 @@ func (lib *URLLibrary) CompileOptions() []cel.EnvOption {
"string",
MemberOverloadFunc(createURLID("string_userinfo_string"), UserinfoType, []*cel.Type{}, cel.StringType,
func(_ context.Context, self ref.Val, args ...ref.Val) ref.Val {
v := self.Value().(*Userinfo).GoUserinfo()
v := lib.refToGoUserinfoValue(self)
return types.String(v.String())
},
),
Expand Down

0 comments on commit 38a3d88

Please sign in to comment.