From edca49eef2db824469a289d06e22b21ac286cee5 Mon Sep 17 00:00:00 2001 From: Jeffy Mathew Date: Fri, 10 Jan 2025 11:45:39 +0100 Subject: [PATCH] refactor IPAccessControl fill and extractTo as helper functions --- apidef/oas/server.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/apidef/oas/server.go b/apidef/oas/server.go index 78e0e06a3fe..b497dffbff4 100644 --- a/apidef/oas/server.go +++ b/apidef/oas/server.go @@ -100,14 +100,7 @@ func (s *Server) Fill(api apidef.APIDefinition) { s.EventHandlers = nil } - if s.IPAccessControl == nil { - s.IPAccessControl = &IPAccessControl{} - } - - s.IPAccessControl.Fill(api) - if ShouldOmit(s.IPAccessControl) { - s.IPAccessControl = nil - } + s.fillIPAccessControl(api) } // ExtractTo extracts *Server into *apidef.APIDefinition. @@ -168,14 +161,7 @@ func (s *Server) ExtractTo(api *apidef.APIDefinition) { s.EventHandlers.ExtractTo(api) - if s.IPAccessControl == nil { - s.IPAccessControl = &IPAccessControl{} - defer func() { - s.IPAccessControl = nil - }() - } - - s.IPAccessControl.ExtractTo(api) + s.extractIPAccessControlTo(api) } // ListenPath is the base path on Tyk to which requests for this API @@ -338,3 +324,25 @@ func (i *IPAccessControl) ExtractTo(api *apidef.APIDefinition) { api.BlacklistedIPs = i.Block api.AllowedIPs = i.Allow } + +func (s *Server) fillIPAccessControl(api apidef.APIDefinition) { + if s.IPAccessControl == nil { + s.IPAccessControl = &IPAccessControl{} + } + + s.IPAccessControl.Fill(api) + if ShouldOmit(s.IPAccessControl) { + s.IPAccessControl = nil + } +} + +func (s *Server) extractIPAccessControlTo(api *apidef.APIDefinition) { + if s.IPAccessControl == nil { + s.IPAccessControl = &IPAccessControl{} + defer func() { + s.IPAccessControl = nil + }() + } + + s.IPAccessControl.ExtractTo(api) +}