github.com/projectcalico/api@v0.0.0-20231218190037-9183ab93f33e/pkg/lib/numorstring/asnumber.go (about)

     1  // Copyright (c) 2016 Tigera, Inc. All rights reserved.
     2  
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package numorstring
    16  
    17  import (
    18  	"encoding/json"
    19  	"errors"
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  type ASNumber uint32
    26  
    27  // ASNumberFromString creates an ASNumber struct from a string value.  The
    28  // string value may simply be a number or may be the ASN in dotted notation.
    29  func ASNumberFromString(s string) (ASNumber, error) {
    30  	if num, err := strconv.ParseUint(s, 10, 32); err == nil {
    31  		return ASNumber(num), nil
    32  	}
    33  
    34  	parts := strings.Split(s, ".")
    35  	if len(parts) != 2 {
    36  		msg := fmt.Sprintf("invalid AS Number format (%s)", s)
    37  		return 0, errors.New(msg)
    38  	}
    39  
    40  	if num1, err := strconv.ParseUint(parts[0], 10, 16); err != nil {
    41  		msg := fmt.Sprintf("invalid AS Number format (%s)", s)
    42  		return 0, errors.New(msg)
    43  	} else if num2, err := strconv.ParseUint(parts[1], 10, 16); err != nil {
    44  		msg := fmt.Sprintf("invalid AS Number format (%s)", s)
    45  		return 0, errors.New(msg)
    46  	} else {
    47  		return ASNumber((num1 << 16) + num2), nil
    48  	}
    49  }
    50  
    51  // UnmarshalJSON implements the json.Unmarshaller uinterface.
    52  func (a *ASNumber) UnmarshalJSON(b []byte) error {
    53  	if err := json.Unmarshal(b, (*uint32)(a)); err == nil {
    54  		return nil
    55  	} else {
    56  		var s string
    57  		if err := json.Unmarshal(b, &s); err != nil {
    58  			return err
    59  		}
    60  
    61  		if v, err := ASNumberFromString(s); err != nil {
    62  			return err
    63  		} else {
    64  			*a = v
    65  			return nil
    66  		}
    67  	}
    68  }
    69  
    70  // String returns the string value, or the Itoa of the uint value.
    71  func (a ASNumber) String() string {
    72  	return strconv.FormatUint(uint64(a), 10)
    73  }