-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daishan
committed
Feb 19, 2021
1 parent
63e1cb5
commit f6cec23
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package mapper | ||
|
||
import ( | ||
"github.com/rancher/wrangler/pkg/name" | ||
"strings" | ||
|
||
"github.com/rancher/norman/types" | ||
) | ||
|
||
type SelfLink struct { | ||
resource string | ||
} | ||
|
||
func (s *SelfLink) FromInternal(data map[string]interface{}) { | ||
sl, ok := data["selfLink"].(string) | ||
if !ok || sl == "" { | ||
data["selfLink"] = s.selflink(data) | ||
} | ||
} | ||
|
||
func (s *SelfLink) ToInternal(data map[string]interface{}) error { | ||
return nil | ||
} | ||
|
||
func (s *SelfLink) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { | ||
s.resource = name.GuessPluralName(schema.ID) | ||
return nil | ||
} | ||
|
||
func (s *SelfLink) selflink(data map[string]interface{}) string { | ||
buf := &strings.Builder{} | ||
name := data["name"].(string) | ||
apiVersion, ok := data["apiVersion"].(string) | ||
if !ok || apiVersion == "v1" { | ||
buf.WriteString("/api/v1/") | ||
} else { | ||
buf.WriteString("/apis/") | ||
buf.WriteString(apiVersion) | ||
buf.WriteString("/") | ||
} | ||
namespace, ok := data["namespace"].(string) | ||
if !ok || namespace != "" { | ||
buf.WriteString("namespaces/") | ||
buf.WriteString(namespace) | ||
buf.WriteString("/") | ||
} | ||
buf.WriteString(s.resource) | ||
buf.WriteString("/") | ||
buf.WriteString(name) | ||
return buf.String() | ||
} | ||
|