github.com/altipla-consulting/ravendb-go-client@v0.1.3/seed_identity_for_command.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  var (
     8  	_ RavenCommand = &SeedIdentityForCommand{}
     9  )
    10  
    11  type SeedIdentityForCommand struct {
    12  	RavenCommandBase
    13  
    14  	id     string
    15  	value  int64
    16  	forced bool
    17  
    18  	Result int
    19  }
    20  
    21  func NewSeedIdentityForCommand(id string, value int64, forced bool) (*SeedIdentityForCommand, error) {
    22  	if id == "" {
    23  		return nil, newIllegalArgumentError("Id cannot be null")
    24  	}
    25  
    26  	res := &SeedIdentityForCommand{
    27  		RavenCommandBase: NewRavenCommandBase(),
    28  
    29  		id:     id,
    30  		value:  value,
    31  		forced: forced,
    32  	}
    33  	return res, nil
    34  }
    35  
    36  func (c *SeedIdentityForCommand) createRequest(node *ServerNode) (*http.Request, error) {
    37  	err := ensureIsNotNullOrString(c.id, "ID")
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	url := node.URL + "/databases/" + node.Database + "/identity/seed?name=" + urlEncode(c.id) + "&value=" + i64toa(c.value)
    43  
    44  	if c.forced {
    45  		url += "&force=true"
    46  	}
    47  
    48  	return newHttpPost(url, nil)
    49  }
    50  
    51  func (c *SeedIdentityForCommand) setResponse(response []byte, fromCache bool) error {
    52  	if len(response) == 0 {
    53  		return throwInvalidResponse()
    54  	}
    55  
    56  	var jsonNode map[string]interface{}
    57  	err := jsonUnmarshal(response, &jsonNode)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	n, ok := jsonGetAsInt(jsonNode, "NewSeedValue")
    62  	if !ok {
    63  		return throwInvalidResponse()
    64  	}
    65  	c.Result = n
    66  	return nil
    67  }