...

Source file src/github.com/leodido/go-urn/scim.go

Documentation: github.com/leodido/go-urn

     1  package urn
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	scimschema "github.com/leodido/go-urn/scim/schema"
     8  )
     9  
    10  const errInvalidSCIMURN = "invalid SCIM URN: %s"
    11  
    12  type SCIM struct {
    13  	Type  scimschema.Type
    14  	Name  string
    15  	Other string
    16  	pos   int
    17  }
    18  
    19  func (s SCIM) MarshalJSON() ([]byte, error) {
    20  	return json.Marshal(s.String())
    21  }
    22  
    23  func (s *SCIM) UnmarshalJSON(bytes []byte) error {
    24  	var str string
    25  	if err := json.Unmarshal(bytes, &str); err != nil {
    26  		return err
    27  	}
    28  	// Parse as SCIM
    29  	value, ok := Parse([]byte(str), WithParsingMode(RFC7643Only))
    30  	if !ok {
    31  		return fmt.Errorf(errInvalidSCIMURN, str)
    32  	}
    33  	if value.RFC() != RFC7643 {
    34  		return fmt.Errorf(errInvalidSCIMURN, str)
    35  	}
    36  	*s = *value.SCIM()
    37  
    38  	return nil
    39  }
    40  
    41  func (s *SCIM) String() string {
    42  	ret := fmt.Sprintf("urn:ietf:params:scim:%s:%s", s.Type.String(), s.Name)
    43  	if s.Other != "" {
    44  		ret += fmt.Sprintf(":%s", s.Other)
    45  	}
    46  
    47  	return ret
    48  }
    49  

View as plain text