github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/organization.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fftypes
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"unicode"
    24  
    25  	"github.com/kaleido-io/firefly/internal/i18n"
    26  )
    27  
    28  // Organization is a top-level identity in the network
    29  type Organization struct {
    30  	ID          *UUID      `json:"id"`
    31  	Message     *UUID      `json:"message,omitempty"`
    32  	Parent      string     `json:"parent,omitempty"`
    33  	Identity    string     `json:"identity,omitempty"`
    34  	Name        string     `json:"name,omitempty"`
    35  	Description string     `json:"description,omitempty"`
    36  	Profile     JSONObject `json:"profile,omitempty"`
    37  	Created     *FFTime    `json:"created,omitempty"`
    38  }
    39  
    40  func (org *Organization) Validate(ctx context.Context, existing bool) (err error) {
    41  	if err = ValidateFFNameField(ctx, org.Name, "name"); err != nil {
    42  		return err
    43  	}
    44  	if err = ValidateLength(ctx, org.Description, "description", 4096); err != nil {
    45  		return err
    46  	}
    47  	if existing {
    48  		if org.ID == nil {
    49  			return i18n.NewError(ctx, i18n.MsgNilID)
    50  		}
    51  	}
    52  	return nil
    53  }
    54  
    55  func orgTopic(orgIdentity string) string {
    56  	buf := strings.Builder{}
    57  	for _, r := range orgIdentity {
    58  		if buf.Len() > 64 {
    59  			break
    60  		}
    61  		if unicode.IsLetter(r) || unicode.IsNumber(r) {
    62  			buf.WriteRune(r)
    63  		} else {
    64  			buf.WriteRune('_')
    65  		}
    66  	}
    67  	return fmt.Sprintf("ff_org_%s", buf.String())
    68  }
    69  
    70  func (org *Organization) Topic() string {
    71  	return orgTopic(org.Identity)
    72  }
    73  
    74  func (org *Organization) SetBroadcastMessage(msgID *UUID) {
    75  	org.Message = msgID
    76  }